With the rapid development of the Internet, programming now often requires direct networking in the program to handle some matters, such as online registration and online help, which requires us to establish certain connections in the program. Many softwares start the browser to search for the URL without knowing whether the user is connected to the Internet. After a lot of effort, they can only find an error page (of course, there is no good result). If we can automatically determine whether the user is connected to the Internet when writing the program, and if so, open the connection, and if not, start the default dial-up connection, will this make people feel that your software is better than others? Determining whether you are connected to the Internet is introduced in many places. Here we only introduce how to start the default dial-up connection.
----Before the introduction, let us first take a look at how to open dial-up networking. Since Dial-Up Networking is not an executable file, it cannot be opened using the "Shell executable file" method. To start dial-up networking, you need to use Explorer. The method is as follows:
Shell"Explorer::{20D04FE0-3AEA-1069-A2D8-08002B30309D}/"&"::{992CFFA0-F557-101A-88EC-00DD010CCC48}",vbNormalFocus
----But if you want to start a connection in the dial-up network, you need to use rundll.exe and rnaui.dll to start it. The method is as follows (assuming the connection name is 163):
Shell"rundllrnaui.dll,RnaDial163",vbNormalFocus
----Note: In the above description, do not insert extra spaces in the ",RnaDial163" part, and do not change the case arbitrarily.
----The above only assumes the connection name, but in actual programming we don't know its name. How to get the default connection name and start it? Here we can use the registry to achieve our goal. The complete procedure is as follows:
----Place a command button (named cmdCallConnect) on the form. The following is the code part:
OptionExplicit
'API statement about registration
PRivateDeclareFunctionRegOpenKeyExLib"advapi32"Alias"RegOpenKeyExA"(ByValhKeyAsLong,ByVallpSubKeyAsString,ByValulOptionsAsLong,ByValsamDesiredAsLong,phkResultAsLong)AsLong
PrivateDeclareFunctionRegQueryValueExLib"advapi32"Alias"RegQueryValueExA"(ByValhKeyAsLong,ByVallpValueNameAsString,ByVallpReservedAsLong,ByReflpTypeAsLong,ByValszDataAsString,ByReflpcbDataAsLong)AsLong
PrivateDeclareFunctionRegCloseKeyLib"advapi32"(ByValhKeyAsLong)AsLong
'constant
ConstHKEY_CURRENT_USER=&H80000001
ConstERROR_SUCCESS=0&
PrivateSubcmdCallConnect_Click()
'Start the default dial-up connection
Shell"rundllrnaui.dll,RnaDial" GetConnect,vbNormalFocus
EndSub
PublicFunctionGetConnect()AsString
DimhKeyAsLong
DimSubKeyAsString
hKey=HKEY_CURRENT_USER'primary key
SubKey="Remoteaccess"'subkey
'Get the default connection name
GetConnect=GetRegValue(hKey,SubKey,"Default")
EndFunction
PublicFunctionGetRegValue(hKeyAsLong,lpszSubKeyAsString,szKeyAsString)AsVariant
OnErrorGoToErrorRoutineErr:
DimphkResultAsLong
DimlResultAsLong
DimszBufferAsString
DimlBuffSizeAsLong
'Create buffer
szBuffer=Space(255)
lBuffSize=Len(szBuffer)
'Open the registration key
RegOpenKeyExhKey,lpszSubKey,0,1,phkResult
'Query results
lResult=RegQueryValueEx(phkResult,szKey,0,0,szBuffer,lBuffSize)
'Close registration key
RegCloseKeyphkResult
'return result
IflResult=ERROR_SUCCESSThen
GetRegValue=Left(szBuffer,lBuffSize-1)
Else
GetRegValue=""
EndIf
ExitFunction
ErrorRoutineErr:
GetRegValue=""
EndFunction
The above program has been debugged under WIN98 and VB6.0. ->