Get Connected Wizard API for Windows Vista

Share your advanced PureBasic knowledge/code with the community.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Get Connected Wizard API for Windows Vista

Post 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)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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()
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Huh!

works fine here in unicode. Hmmm....
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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?
I may look like a mule, but I'm not a complete ass.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

He's probably one of those API purists. *gets beaten to a pulp* :lol:
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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)
Post Reply