如何使用C#连接XP或7中的默认拨号?

发布于 2024-11-16 21:32:16 字数 65 浏览 4 评论 0原文

我的系统中有一个拨号连接,即“Personal Internet Movil”,我需要使用 C# 连接和断开此连接。

I have a dialup connection in my system namely "Personal Internet Movil" and i need to connect and disconnect this connection using C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-11-23 21:32:16

您可以使用此代码,这些导入语句和枚举会检测连接,启动拨号框,然后在完成后挂断。如果您不希望用户选择拨号连接,则可以使用 InternetAutodial/InternetAutodialHangup 对。但请注意,这对将仅使用默认设置的任何拨号。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//BEGIN
InternetConnectionState flags = 0;
int nConnection = 0;
bool bInternet = InternetGetConnectedState(ref flags,0);
if (bInternet == false)
{
 //Not Connected
 if (flags == 0)
 {
   //Do something to tell the user to install some internet connection
 }
 int nResult = InternetDial(IntPtr.Zero,"",(int)InternetDialFlags.INTERNETDIALFORCEPROMPT , ref nConnection , 0);
 switch(nResult)
 {
   case 87://Bad Parameter for InternetDial - Couldn't Connect";
     break;
   case 668://No Connection for InternetDial - Couldn't Connect";
     break;
   case 631://User Cancelled Dialup
     break;
   default://Unknown InternetDial Error
     break;
   case 0://Connection Succeeded
     break;
 }
 //Do whatever you need to do on the internet (send mail etc)
 InternetHangUp(nConnection,0);
 //DONE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved);
[Flags]
enum InternetConnectionState: int
{
   INTERNETCONNECTIONMODEM = 0x1,
   INTERNETCONNECTIONLAN = 0x2,
   INTERNETCONNECTIONPROXY = 0x4,
   INTERNETRASINSTALLED = 0x10,
   INTERNETCONNECTIONOFFLINE = 0x20,
   INTERNETCONNECTIONCONFIGURED = 0x40
}
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
static extern int InternetDial(IntPtr hwndParent,[MarshalAs(UnmanagedType.LPStr)] string strConnection,[MarshalAs(UnmanagedType.U4)]int dwFlags,ref int dwConnection,int dwReserved);
[Flags]
enum InternetDialFlags: int
{
   INTERNETDIALFORCEPROMPT    =0x2000,
   INTERNETDIALSHOWOFFLINE    =0x4000,
   INTERNETDIAL_UNATTENDED      =0x8000
}
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
static extern int InternetHangUp([MarshalAs(UnmanagedType.U4)] int nConnection,[MarshalAs(UnmanagedType.U4)]int dwReserved);       

You can use this code, and these import statements and enums do detect a connection, fire up the Dialup box, and then hangup when you're done. If you don't want the user to select the dialup connection then user InternetAutodial/InternetAutodialHangup pair rather. But Please note that this pair will ONLY use whatever dialup is set as the default.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//BEGIN
InternetConnectionState flags = 0;
int nConnection = 0;
bool bInternet = InternetGetConnectedState(ref flags,0);
if (bInternet == false)
{
 //Not Connected
 if (flags == 0)
 {
   //Do something to tell the user to install some internet connection
 }
 int nResult = InternetDial(IntPtr.Zero,"",(int)InternetDialFlags.INTERNETDIALFORCEPROMPT , ref nConnection , 0);
 switch(nResult)
 {
   case 87://Bad Parameter for InternetDial - Couldn't Connect";
     break;
   case 668://No Connection for InternetDial - Couldn't Connect";
     break;
   case 631://User Cancelled Dialup
     break;
   default://Unknown InternetDial Error
     break;
   case 0://Connection Succeeded
     break;
 }
 //Do whatever you need to do on the internet (send mail etc)
 InternetHangUp(nConnection,0);
 //DONE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved);
[Flags]
enum InternetConnectionState: int
{
   INTERNETCONNECTIONMODEM = 0x1,
   INTERNETCONNECTIONLAN = 0x2,
   INTERNETCONNECTIONPROXY = 0x4,
   INTERNETRASINSTALLED = 0x10,
   INTERNETCONNECTIONOFFLINE = 0x20,
   INTERNETCONNECTIONCONFIGURED = 0x40
}
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
static extern int InternetDial(IntPtr hwndParent,[MarshalAs(UnmanagedType.LPStr)] string strConnection,[MarshalAs(UnmanagedType.U4)]int dwFlags,ref int dwConnection,int dwReserved);
[Flags]
enum InternetDialFlags: int
{
   INTERNETDIALFORCEPROMPT    =0x2000,
   INTERNETDIALSHOWOFFLINE    =0x4000,
   INTERNETDIAL_UNATTENDED      =0x8000
}
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
static extern int InternetHangUp([MarshalAs(UnmanagedType.U4)] int nConnection,[MarshalAs(UnmanagedType.U4)]int dwReserved);       
倾听心声的旋律 2024-11-23 21:32:16

看看 DotRas

这是一个快速演示:http://www.youtube.com/user/DotRasProject

您基本上需要使用RasDialRasHangUp WinAPI方法。您可以执行 PInvoke 或仅使用已包装 API 的 DotRas .NET 类。

Take a look at DotRas.

Here is a quick demo: http://www.youtube.com/user/DotRasProject

You basically need to use RasDial and RasHangUp WinAPI methods. You can do PInvoke or simply use DotRas .NET classes that already wrapped the API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文