Page 1 of 1

Get Connected Wizard API for Windows Vista

Posted: Sun Aug 03, 2008 3:04 pm
by SFSxOI
This is for Windows Vista only.

Recently had a small project that needed me to launch a connection wizard from a small application. There are a few different ways to launch the wizard on Vista manually so this was a "Gee Whiz" creature comfort feature. Started digging thru the MSDN and found my answer. Its as simple as pie, so if anyone wants to know how to do this:


Code: Select all

;Get Connected Wizard API for PureBasic
;see for details: http://msdn.microsoft.com/en-us/library/aa364732(VS.85).aspx
;Windows Vista only !

;//////////////////////////////////////////
; returns if connected to internet or not
;/////////////////////////////////////////
Procedure Internet_Connected()
ret.l
Libef = LoadLibrary_("Connect.dll")
  If Libef
    *Inet_Connected = GetProcAddress_(Libef, "IsInternetConnected")
    If *Inet_Connected
    If CallFunctionFast(*Inet_Connected) = #S_OK
    ret = 1
    EndIf
    EndIf
  EndIf
  FreeLibrary_(Libef)
  ProcedureReturn ret    
EndProcedure

Debug Internet_Connected()


;///////////////////////////
; Creates a VPN connection
;///////////////////////////

Procedure Create_VPNConnect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine)

ret.l
Libef = LoadLibrary_("Connect.dll")
  If Libef
    *VPNConnect = GetProcAddress_(Libef, "CreateVPNConnection")
    If *VPNConnect
    If CallFunctionFast(*VPNConnect, in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK
    ret = 1
    EndIf
    EndIf
  EndIf
  FreeLibrary_(Libef)
  ProcedureReturn ret    
EndProcedure

Debug Create_VPNConnect(hwndParent.l, dwWizardType.l, dwContextFlags.l, dwUserFlags.l, hUserContext.l, opt_pszCommandLine)

;////////////////////////////////////////////////////////////
;Below launches Internet, Network, and VPN connection wizards
;////////////////////////////////////////////////////////////

;Internet connection wizard
Procedure GetInet_Connect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine)

ret.l
Libef = LoadLibrary_("Connect.dll")
  If Libef
    *G_Inet_Connect = GetProcAddress_(Libef, "GetInternetConnected")
    If *G_Inet_Connect
    If CallFunctionFast(*G_Inet_Connect, in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK
    ret = 1
    EndIf
    EndIf
  EndIf
  FreeLibrary_(Libef)
  ProcedureReturn ret    
EndProcedure

Debug GetInet_Connect(#Null, 0, 0, 0, #Null, #Null)

;network connection wizard
Procedure GetNetwork_Connect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine)

ret.l
Libef = LoadLibrary_("Connect.dll")
  If Libef
    *G_Net_Connect = GetProcAddress_(Libef, "GetNetworkConnected")
    If *G_Net_Connect
    If CallFunctionFast(*G_Net_Connect, in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK
    ret = 1
    EndIf
    EndIf
  EndIf
  FreeLibrary_(Libef)
  ProcedureReturn ret    
EndProcedure

Debug GetNetwork_Connect(#Null, 0, 0, 0, #Null, #Null)

;VPN connection wizard
Procedure GetVPN_Connect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine)

ret.l
Libef = LoadLibrary_("Connect.dll")
  If Libef
    *G_VPN_Connect = GetProcAddress_(Libef, "GetVPNConnected")
    If *G_VPN_Connect
    If CallFunctionFast(*G_VPN_Connect, in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK
    ret = 1
    EndIf
    EndIf
  EndIf
  FreeLibrary_(Libef)
  ProcedureReturn ret    
EndProcedure

Debug GetVPN_Connect(#Null, 0, 0, 0, #Null, #Null)

Posted: Thu Aug 07, 2008 6:46 pm
by Rescator
Cool, but it fails if compiled with Unicode. and considering that NT, 2000, XP, 2003 and Vista and 2008 are all Unicode. Well. kinda silly ain't it :P

This works fine, uses PB native code, less lines of code to type. Fixing the rest is just as easy.

Code: Select all

EnableExplicit

Procedure.l IsInternetConnected()
 Protected result.l=#False,dll.l=#Null
 dll=OpenLibrary(#PB_Any,"connect.dll")
 If dll
  If CallFunction(dll,"IsInternetConnected")=#S_OK
   result=#True
  EndIf
  CloseLibrary(dll)
 EndIf
 ProcedureReturn result
EndProcedure ;#True if connected, #False if not or error.

Debug IsInternetConnected()

Posted: Thu Aug 07, 2008 7:16 pm
by SFSxOI
Huh!

works fine here in unicode. Hmmm....

Posted: Thu Aug 07, 2008 10:56 pm
by Rescator
Well! It could be a Vista + IDE + that source, causing a issue.
Tested it again and the IDE crashed :shock:

I'm pondering if it could be the API loadlib or the API getproc,
don't recall if they are ANSI only or have Uni versions.

Posted: Thu Aug 07, 2008 11:06 pm
by srod
Yes, GetProcAddress_() requires ascii strings. Convert to ascii and it ran fine here in unicode mode.

@SFSxOI : why not use the PB library functions as they do all necessary unicode/ascii conversions?

Posted: Thu Aug 07, 2008 11:14 pm
by Rescator
He's probably one of those API purists. *gets beaten to a pulp* :lol:

Posted: Fri Aug 08, 2008 7:20 pm
by SFSxOI
LoL :)

Nope, not a purist. I just have the LoadLibrary and GetProcAddress thing memorized so its most prevelant in my projects because its just comfortable to use.

OK, here it is with just the PB library functions:

Code: Select all

;Get Connected Wizard API for PureBasic 
;see for details: http://msdn.microsoft.com/en-us/library/aa364732(VS.85).aspx 
;Windows Vista only !

;////////////////////////////////////////// 
; returns if connected to internet or not 
;/////////////////////////////////////////

 
Procedure Internet_Connected() 
Protected result.l=#False,Libef.l=#Null

Libef = OpenLibrary(#PB_Any,"Connect.dll") 
  If Libef
    If CallFunction(Libef, "IsInternetConnected") = #S_OK 
    result.l=#True
    MessageRequester("Information", "You are connected to the Internet", #PB_MessageRequester_Ok)
    Else
    MessageRequester("Information", "You are NOT connected to the Internet", #PB_MessageRequester_Ok) 
    EndIf 
  EndIf 
  CloseLibrary(Libef) 
  ProcedureReturn result    
EndProcedure 

Debug Internet_Connected() 


;/////////////////////////// 
; Creates a VPN connection 
;/////////////////////////// 

Procedure Create_VPNConnect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) 
Protected result.l=#False,Libef.l=#Null

Libef = OpenLibrary(#PB_Any,"Connect.dll") 
  If Libef 
    If CallFunction(Libef, "CreateVPNConnection", in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK 
    result.l=#True 
    EndIf 
  EndIf 
  CloseLibrary(Libef) 
  ProcedureReturn result    
EndProcedure 

Debug Create_VPNConnect(hwndParent.l, dwWizardType.l, dwContextFlags.l, dwUserFlags.l, hUserContext.l, opt_pszCommandLine) 

;//////////////////////////////////////////////////////////// 
;Below launches Internet, Network, and VPN connection wizards 
;//////////////////////////////////////////////////////////// 

;Internet connection wizard 
Procedure GetInet_Connect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) 
Protected result.l=#False,Libef.l=#Null

Libef = OpenLibrary(#PB_Any,"Connect.dll") 
  If Libef 
    If CallFunction(Libef, "GetInternetConnected", in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK 
    result.l=#True 
    EndIf 
  EndIf 
  CloseLibrary(Libef) 
  ProcedureReturn result    
EndProcedure 

Debug GetInet_Connect(#Null, 0, 0, 0, #Null, #Null) 

;network connection wizard 
Procedure GetNetwork_Connect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) 
Protected result.l=#False,Libef.l=#Null

Libef = OpenLibrary(#PB_Any,"Connect.dll") 
  If Libef 
    If CallFunction(Libef, "GetNetworkConnected", in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK 
    result.l=#True
    EndIf 
  EndIf 
  CloseLibrary(Libef) 
  ProcedureReturn result    
EndProcedure

Debug GetNetwork_Connect(#Null, 0, 0, 0, #Null, #Null)

;VPN connection wizard
Procedure GetVPN_Connect(in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine)

Protected result.l=#False,Libef.l=#Null  

Libef = OpenLibrary(#PB_Any,"Connect.dll") 
  If Libef 
    If CallFunction(Libef, "GetVPNConnected", in_hwndParent.l, in_dwWizardType.l, in_dwContextFlags.l, in_dwUserFlags.l, in_hUserContext.l, in_opt_pszCommandLine) = #S_OK 
    result.l=#True
    EndIf 
  EndIf 
  CloseLibrary(Libef) 
  ProcedureReturn result
    
EndProcedure 

Debug GetVPN_Connect(#Null, 0, 0, 0, #Null, #Null)