Kharanshu
Kharanshu

Reputation: 31

Connect remote PC with credentials

I have been trying to connect remote PC(with known credentials and on the same network) to my PC with vb.net but struck at following Error:

Managed Debugging Assistant 'PInvokeStackImbalance' : 'A call to PInvoke function 'WindowsApp1!WindowsApp1.Form1::WNetAddConnection2' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.'

Whereas same code has been executed flawlessly in Vb6.

Code:

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

Exception is thrown at lon and code coudn't execute further.

I am new to VB.net language.Any assistance would be very helpful.

Upvotes: 1

Views: 406

Answers (1)

Caius Jard
Caius Jard

Reputation: 74660

From JQSOFT's comment:

In the signature replace (lpNetResource As NETRESOURCE, ...

with

(ByRef lpNetResource As NETRESOURCE, ... because the NETRESOURCE here is a structure and not a class. Also, replace any Long with Integer

Upvotes: 1

Related Questions