我一直在尝试将远程PC(具有已知凭据并在同一网络上(连接到我的 vb.net PC,但遇到以下错误:
托管调试助手 'PInvokeStackImbalance' : 'A Call to PInvoke function 'WindowsApp1!WindowsApp1.Form1::WNetAddConnection2' 使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。
而相同的代码在 Vb6 中完美执行。
法典:
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
Private Structure NETRESOURCE
Dim dwType As Long
Dim lpRemoteName As String
End Structure
Private Const RESOURCETYPE_DISK = &H1
Private Sub ConnectToPC()
Dim networkResource As New NETRESOURCE
Dim lon As Long
With networkResource
.dwType = RESOURCETYPE_DISK
.lpRemoteName = "\192.168.1.1"
End With
lon = WNetAddConnection2(networkResource, "123", "ADMIN", 0)
End Sub
在 lon引发异常,代码无法进一步执行。
我是 VB.net 语言的新手。任何帮助都将非常有帮助。
来自 JQSOFT 的评论:
在签名中替换 (
lpNetResource As NETRESOURCE, ...
跟
(
ByRef lpNetResource As NETRESOURCE, ...
因为这里的网络资源是一个 结构而不是类。此外,将任何Long
替换为Integer