📄 csdn_文档中心_用程序实现自动拨号.htm
字号:
<TD align=middle bgColor=#003399 height=10><FONT
color=#ffffff>标题</FONT></TD>
<TD><B> 用程序实现自动拨号</B> 2066(原作)
</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
<TD width=500> vs.net,vb.net,拨号,Microsoft
Visual Studio .NET,shell,注册表,命令行</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
<TABLE border=0 width=600>
<TBODY>
<TR>
<TD align=left><BR>
<P align=center class="style1 style18">用程序实现自动拨号</P>
<P class=style1> <BR> <SPAN
class=style15><STRONG>屠恩海(</STRONG><FONT
face="Times New Roman, Times, serif"><EM>SunHai</EM></FONT><STRONG>)</STRONG></SPAN>
</P>
<P> 开发工具:Microsoft Visual Studio .NET 2003<BR> 操作系统:Windows
XP</P>
<P> </P>
<P> <SPAN
class=style18>在</SPAN>编写网络程序时很可能用到自动拨号。比如,邮件群发软件用自动断线和拨号实现本机IP地址的更换。<BR> 要实现自动拨号,分两步:<BR> 从注册表读取本机拨号链接名称;<BR> 自动拨号。 <BR></P>
<P> <SPAN class=style10> 调用API函数从注册表读取本机拨号链接名</SPAN></P>
<P> 以前,我调用API函数从注册表中读取本机拨号链接名称。<BR> 在Module中用如下代码:</P>
<TABLE bgColor=#cccccc width="100%">
<TBODY>
<TR>
<TD>
<P>Private Declare Function RegOpenKey Lib "advapi32.dll"
Alias "RegOpenKeyA" (ByVal hKey As Integer, ByVal lpSubKey
As String, ByRef phkResult As Integer) As Integer<BR>Private
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As
Integer) As Integer<BR>Private 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<BR> Const HKEY_CURRENT_USER As
Integer = &H80000001<BR> Const ERROR_NO_MORE_ITEMS As
Short = 259<BR> Const ERROR_SUCCESS As Short = 0</P>
<P> Dim hKey As Integer<BR> Dim i As Integer<BR> Dim astr
As String = New String("", 256)<BR> If
RegOpenKey(HKEY_CURRENT_USER, "RemoteAccess\Profile", hKey) =
ERROR_SUCCESS Then<BR> While RegEnumKey(hKey, i, astr, 256)
= ERROR_SUCCESS<BR> MsgBox(astr) '链接名称<BR> i +=
1<BR> End While<BR> RegCloseKey(hKey)<BR> End
If</P></TD></TR></TBODY></TABLE>
<P><BR> <SPAN class=style16> </SPAN></P>
<P><BR> <SPAN class=style10> <STRONG>用RegistryKey
类读取链接名</STRONG></SPAN></P> <STRONG> RegistryKey 类</STRONG>表示
Windows 注册表中的项级节点。此类是注册表封装。 <BR>
<P> 本文主要用到:<BR><BR> <STRONG>RegistryKey.OpenSubKey
方法:用指定的写访问权限检索指定的子项。</STRONG> </P>
<P> 以只读方式检索子项。 </P>
<P> [Visual Basic] Overloads Public Function OpenSubKey(String) As
RegistryKey <BR><BR><BR> <STRONG>RegistryKey.GetSubKeyNames 方法:
检索包含所有子项名称的字符串数组。<BR></STRONG><BR><STRONG> </STRONG>Public Function
GetSubKeyNames() As String ()
<BR><BR> 用RegistryKey读取链接名可以说是非常简单:</P>
<TABLE bgColor=#cccccc width="100%">
<TBODY>
<TR>
<TD>
<P>Dim rk As RegistryKey =
_<BR>Registry.CurrentUser.OpenSubKey("RemoteAccess\Profile",
True)<BR>' Get the data from a specified item in the
key.<BR>Dim s As String() = rk.GetSubKeyNames()<BR><BR>For num
As Integer = 0 To s.Length -
1<BR> MsgBox(s.GetValue(num)) '这就是链接名<BR>Next</P></TD></TR></TBODY></TABLE>
<P></P>
<P> </P>
<P> <SPAN
class=style10>用Shell实现自动拨号<BR><BR> </SPAN>读取链接名后,自动拨号就非常简单了。 <SPAN
class=style10>
<BR> </SPAN>打开“控制面板”,“网络连接”,连接“属性”,“选项”,去掉“提示名称、密码和证书等(P)”前面的对勾。</P>
<TABLE bgColor=#cccccc width="100%">
<TBODY>
<TR>
<TD>
<P>shell("rasphone.exe -d" & linksName, AppWinStyle.Hide,
True, -1)</P></TD></TR></TBODY></TABLE>
<P> 启动“命令提示符”,键入 rasphone,回车,就弹出“拨号网络对话框”。<BR> 如果键入 rasphone
-h,回车,弹出“拨号网络命令行”,列出rasphone的用法,比如,<BR> rasphone
-d 表示弹出弹号项目对话框。<BR> rasphone -lx 在拨号快捷方式上执行命令'x'
<BR><BR> Shell函数说明:<BR> AppWinStyle.Hide是sheell的参数,表示隐藏窗口并将焦点传到该窗口。<BR> True表示等待拨号完成。<BR>
-1表示Shell直到程序完成才返回。<BR><BR><BR> 也可以这样:</P>
<TABLE bgColor=#cccccc width="100%">
<TBODY>
<TR>
<TD>
<P>shell("rasdial.exe " & linksName, AppWinStyle.Hide,
True, -1)</P></TD></TR></TBODY></TABLE>
<P> <STRONG> 关于各种命令参数,在OS的“帮助和支持”搜索“命令行”即可查到有关说明。命令行结合Shell函数可实现极多功能,你可试一试。</STRONG><BR><BR></P>
<P> Shell 函数使用说明:</P>
<P>Public Function Shell ( _ ByVal <EM>Pathname </EM>As String, _
<BR> Optional ByVal <EM>Style </EM>As AppWinStyle =
AppWinStyle.MinimizedFocus, _ <BR> Optional ByVal <EM>Wait </EM>As
Boolean = False, _ <BR> Optional ByVal <EM>Timeout </EM>As Integer
= -1 _ <BR> ) As Integer </P>
<P><STRONG> Shell </STRONG>函数的返回值取决于 <EM>Pathname </EM>中指定的程序在
<STRONG>Shell </STRONG>返回时是否仍在执行。如果将 <EM>Wait </EM>设置为 <STRONG>True
</STRONG>并且程序在超时过期前结束, <STRONG>Shell </STRONG>返回零。如果超时过期或者省略
<EM>Wait </EM>或将它设置为 <STRONG>False </STRONG>,则 <STRONG>Shell
</STRONG>返回程序的进程 ID。进程 ID 是标识正在运行的程序的唯一数字。 </P>
<P> 如果 <STRONG>Shell </STRONG>函数无法启动指定的程序,则出现
<STRONG>System.IO.FileNotFoundException </STRONG>错误。例如,当试图从使用
<STRONG>System.Windows.Forms </STRONG>的应用程序运行 16 位程序(如 command.com
)时,可能会发生这种情况。解决办法是运行将调用所需的 16 位程序的 32 位程序。如果是 command.com ,则可以将运行
cmd.exe 作为另一种选择。 </P>
<P> 默认情况下, <STRONG>Shell </STRONG>函数异步运行程序。这意味着用 <STRONG>Shell
</STRONG>函数启动的程序在 <STRONG>Shell
</STRONG>函数后面的语句执行前可能没有结束执行。如果想等待程序结束后再继续,请将 <EM>Wait </EM>设置为
<STRONG>True </STRONG>。 </P>
<P> 整个路径和文件规范应该始终用引号引起来,如以下示例所示: </P>ID = Shell ("""C:\Program
Files\MyFile.exe"" -a -q", , True, 100000)
<P> 字符串内每对相邻的双引号 ( <STRONG>"" </STRONG>)
被解释为字符串中的一个双引号字符。因此,前面的示例表示 <STRONG>Shell </STRONG>函数的下列字符串:
</P>"C:\Program Files\MyFile.exe" -a -q
<P> 如果路径没有用引号引起来,Windows 就会在 C:\ 目录中查找名为 Program.exe 的文件,而不是在
C:\Program Files 目录中查找 MyFile.exe 文件。 </P><STRONG> 安全说明
</STRONG>如果路径和文件规范没有用引号引起来,那么当文件名或路径节点包含空格时,就会出现安全风险。在上面的示例中,路径节点
\Program Files 包含一个空格。如果规范不在引号中,并且名为 Program.exe 的程序(例如通过非法的破坏)安装在
C:\ 中,Windows 就会执行 Program.exe 程序而不是 MyFile.exe 。<BR>
<P> <SPAN class=style2><SPAN
class=style13>2004年5月7日</SPAN></SPAN></P>
<P></P><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文档中心_用程序实现自动拨号.files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#006699 id=white><FONT
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD>你没有登陆,无法发表评论。 请先<A
href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=27616">登陆</A>
<A
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
<TBODY>
<TR align=middle>
<TD height=10 vAlign=bottom><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
border=0 height=48 src="CSDN_文档中心_用程序实现自动拨号.files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -