Pinvoke 堆栈不平衡发生,正在检查 Internet 连接

Pinvoke Stack Imbalance occured, Checking Internet Connection

我在 VB 中有一段代码是在 Microsoft Visual Studio 2015 中编码的,不幸的是我在 运行 程序时出错。

这是错误:

 Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\PC_6\Documents\Visual Studio 2015\Projects\Test\test\bin\Debug\Test.vshost.exe'.

 Additional information: A call to PInvoke function 'Test!Test.Form1::InternetGetConnectedState' has unbalanced the stack. 

这是我的代码。

 Imports System.Runtime.InteropServices

 Public Class Form1

 Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef conn As Long, ByVal val As Long) As Boolean

 Private Sub btnPay_Click(sender As Object, e As EventArgs) Handles btnPay.Click

  Select Case ListBox1.SelectedIndex
        Case 0
            MsgBox("Selected Payment is " + ListBox1.SelectedItem)
        Case 1
            MsgBox("Selected Payment is " + ListBox1.SelectedItem)
        Case 2

            btnPay.Text = ("Checking Connection")
            Dim Out As Integer
            If InternetGetConnectedState(Out, 0) = True Then
                MsgBox("Connected!")
            Else
                MsgBox("No Connection!")
            End If
        Case Else
            MsgBox("Please Select a Payment Type")
    End Select
End Sub

结束Class

InternetGetConnectedState ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa384702%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 ) 的文档显示它接受 LPDWORDDWORD 参数,在 .NET 中等价物是 out UInt32 (或 ref) 和 UInt32 分别。

将您的签名更改为:

Public Enum InternetConnection As UInt32
    None = 0,
    Configured = &H40,
    Lan = &H02,
    Modem = &H01,
    ModemBusy = &H08,
    Offline = &H20,
    Proxy = &H04,
    RasInstalled = &H10
End Enum

Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef flags As InternetConnection, ByVal reserved As UInt32) As Boolean

然后这样调用:

Dim result As Boolean
InternetConnection flags;
result = InternetGetConnectedState( flags, 0 );
If result = False Then
    ' TODO: Call GetLastError to eliminate other causes of failure before determining it is indeed due to a lack of an internet connection
End If