Automatic dialing

zhaozj2021-02-16  65

Automatic dialing

Sunhai

Development Tools: Microsoft Visual Studio .NET 2003 operating system: Windows XP

Automatic dialing is likely to use when writing a network program. For example, the mail group sending software software is replaced with the native IP address with automatic disconnection and dialing. To achieve automatic dial, divided into two steps: read the native dial-up link name from the registry; automatic dialing.

Call the API function Read the local dial-up link from the registry

Previously, I called the API function to read the native dial link name from the registry. Use the following code in Module:

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias ​​"RegOpenKeyA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByRef phkResult As Integer) As IntegerPrivate Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Integer) As IntegerPrivate Declare Function RegEnumKey Lib "advapi32.dll" Alias ​​"RegEnumKeyA" (ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpName As String, ByVal cbName As Integer) As Integer Const HKEY_CURRENT_USER As Integer = & H80000001 Const ERROR_NO_MORE_ITEMS As Short = 259 Const ERROR_SUCCESS As Short = 0 Dim hKey As Integer Dim i As Integer Dim astr As String = New String ( "", 256) If RegOpenKey (HKEY_CURRENT_USER, "RemoteAccess / Profile", hKey) = ERROR_SUCCESS Then While RegEnumKey (hKey, i, astr, 256 ) = Error_success msgbox (astr) 'link name i = 1 End while regclosekey (HKEY) End IF

Read the link name with RegistryKey class

The RegistryKey class represents an item-level node in the Windows registry. This type is a registry package.

This article mainly uses: registryKey.opensubkey method: Retrieve the specified child with the specified write access.

Retrieve the subkey in read-only mode.

[Visual Basic] Overloads Public Function OpenSubkey (String) AS RegistryKey RegistryKey.getsubKeynames method: Retrieves an array of strings containing all child names. Public function getSubkeyNames () AS String () Using the registryKey read link name can be said to be very simple:

Dim rk As RegistryKey = _Registry.CurrentUser.OpenSubKey ( "RemoteAccess / Profile", True) 'Get the data from a specified item in the key.Dim s As String () = rk.GetSubKeyNames () For num As Integer = 0 To S.Length - 1 msgbox (s.getvalue (num)) 'This is that the link name next NEXT automatically dials the automatic dial-up reading link, the automatic dialing is very simple. Open the Control Panel, Network Connections, Connect the Properties, Options, Remove the "Prompt Name, Password, Certificate, etc. (P)".

Shell ("rasphone.exe -d" & linksname, appwinstyle.hide, true, -1)

Start the "Command Prompt", type the Rasphone, enter, populate the Dial-up Network dialog box. If you type rasphone -h, enter, populate the "Dial-up Network Command Line", list the use of Rasphone, for example, rasphone -d represents the pop-up number project dialog box. RASPHONE -LX executes a command 'x' shell function description in dial-up shortcut: AppWinStyle.hide is the parameter of Shell, indicates the hidden window and passes the focus to the window. True means waiting for dialing to complete. -1 indicates that shell is returned until the program is completed. It can also be the case:

Shell ("rasdial.exe" & linksname, appwinstyle.hide, true, -1)

About various command parameters, search for "command line" in OS's "Help and Support" search. The command line combines the shell function to achieve great features, you can try it.

SHELL function instructions:

Public Function Shell (_ ByVal Pathname As String, _ Optional ByVal Style As AppWinStyle = AppWinStyle.MinimizedFocus, _ Optional ByVal Wait As Boolean = False, _ Optional ByVal Timeout As Integer = -1 _) As Integer

The return value of the shell function depends on whether the program specified in Pathname is still executed when shell returns. If Wait is set to true and the program ends before the timeout expires, the shell returns zero. If the timeout expires or omits WAIT or set it to false, the shell returns the process ID of the program. The process ID is the only number identifies the running program.

If the shell function can't start the specified program, the System.IO.FileNotFoundException error occurs. This may happen when trying to run 16-bit programs (such as Command.com) from an application using System.Windows.Forms. The solution is to run the 32-bit program that will call the 16-bit programs required. If it is Command.com, you can run cmd.exe as another option.

By default, the shell function is asynchronous. This means that the program started with the shell function may not end before the statement behind the shell function. If you want to wait for the end, continue, set WAIT to True. The entire path and file specification should always be generated by quotation marks, as shown in the following example: ID = shell ("" "c: / program files / myfile.exe" "-a -q", TRUE, 100000)

Each pair of adjacent dual quotes ("") in the string is interpreted as a double quotes in the string. Therefore, the previous example represents the following string of the shell function: "c: / program files / myfile.exe" -a -q

If the path does not use quotation marks, Windows looks for files named Program.exe in the C: / Directory instead of finding myFile.exe files in the C: / Program Files directory.

Safety Instructions If the path and file specification are not induced by quotation, the security risk will occur when the file name or path node contains spaces. In the example above, the path node / program files contains a space. If the specification is not in the quotation, the program named Program.exe (eg, by illegal damage) is installed in C: /, Windows executes the Program.exe program instead of myFile.exe.

May 7, 2004

My QQ: 26624998 My website: http://blog.9cbs.net/2066/

转载请注明原文地址:https://www.9cbs.com/read-22808.html

New Post(0)