使用wininet.dll Windows API问题拨号(C#)

时间:2012-11-28 11:39:11

标签: c# winapi wininet dial-up

我在wininet.dll上调用以下函数才能上线(C#pinvoke):

[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int InternetDial(IntPtr hWndParent, string pszEntryName,
                                   int dwFlags, ref int lpdwConnection,
                                   int ReservedValue);

 public static bool GetOnline()
 {
        int connectionID = 0;
        InternetDial(IntPtr.Zero, "DefaultDialUp", INTERNET_AUTODIAL_FORCE_UNATTENDED, ref connectionID, 0);
        return (connectionID != 0);
 }

问题是拨号过程中出现错误,例如硬件故障,
Windows显示一个阻止对话框,询问用户如何进行,
并且我的功能一直停留,直到用户关闭对话框为止:
enter image description here 此代码应部署在自动系统上,因此这对我来说是一个主要问题......

我正在寻找一种方法来抑制Windows API级别的错误对话框。

提前致谢, 埃坦。

1 个答案:

答案 0 :(得分:0)

我设法找到了解决这个问题的工作方法,但这非常令人讨厌 更好的解决方案仍然欢迎......

    const int WM_CLOSE = 0x10;
    const int INTERNET_AUTODIAL_FORCE_UNATTENDED = 0x2;

    [DllImport("User32.dll", EntryPoint = "PostMessage")]
    public static extern int PostMessage(int hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern int InternetDial(IntPtr hWndParent, string pszEntryName, int dwFlags, ref int lpdwConnection, int ReservedValue);

        public static bool GetOnline()
        {
            connectionID = 0;
            Form f = null;
            var t = new Thread((ParameterizedThreadStart)delegate
            {
                f = new Form();                
                InternetDial(f.Handle, "DefaultDialUp", INTERNET_AUTODIAL_FORCE_UNATTENDED, ref connectionID, 0);
            });
            t.Start();
            t.Join(23000); //wait 23 seconds before closing the window
            f.Invoke((EventHandler)delegate {
                PostMessage(f.Handle.ToInt32(), WM_CLOSE, IntPtr.Zero, IntPtr.Zero);            
            });
            t.Join();            
            return (connectionID != 0);            
        }