Results 1 to 2 of 2

Thread: PInvokeStackImbalance using WNetAddConnection2

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    2

    PInvokeStackImbalance using WNetAddConnection2

    Hi,

    I'm a newbie to the world of VB and VB 2005 Express Edition and I've stumbled across an error I can not resolve myself.

    I'm basically trying to map a drive to a remote server via Windows API. User credentials are passed via a simple form. The script falls down when calling WNetAddConnection2 i.e.

    returnValue = (WNetAddConnection2(NetResource, Username, password, CONNECT_UPDATE_PROFILE) = 0)

    Error Message: Message: A call to PInvoke function 'ReconnectCassPrinters!ReconnectCassPrinters.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.

    The signitaure has been declared as follows:

    Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _ (ByVal lpNetResource As NETCONNECT, ByVal lpPassword As String, ByVal lpUserName As String, _
    ByVal dwFlags As Long) As Long

    No one else seems to have reported such an error so I'm certain i've overlooked something simple.

    Any help would be much appreciated. The code has been appended below.

    Thanks in advance.

    Marcus.

    Code

    Option Explicit On

    Public Structure NETCONNECT
    Public dwScope As Long
    Public dwType As Long
    Public dwDisplayType As Long
    Public dwUsage As Long
    Public lpLocalName As String
    Public lpRemoteName As String
    Public lpComment As String
    Public lpProvider As String
    End Structure

    Public Class Form1

    Private Const CONNECT_UPDATE_PROFILE = &H1&
    Public Const RESOURCETYPE_DISK = &H1
    Public Const RESOURCETYPE_PRINT = &H2
    Public Const RESOURCETYPE_ANY = &H0
    Public Const RESOURCE_CONNECTED = &H1
    Public Const RESOURCE_REMEMBERED = &H3
    Public Const RESOURCE_GLOBALNET = &H2
    Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
    Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
    Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
    Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Public Const RESOURCEUSAGE_CONNECTABLE = &H1
    Public Const RESOURCEUSAGE_CONTAINER = &H2
    ' Error Constants:
    Public Const ERROR_ACCESS_DENIED = 5&
    Public Const ERROR_ALREADY_ASSIGNED = 85&
    Public Const ERROR_BAD_DEV_TYPE = 66&
    Public Const ERROR_BAD_DEVICE = 1200&
    Public Const ERROR_BAD_NET_NAME = 67&
    Public Const ERROR_BAD_PROFILE = 1206&
    Public Const ERROR_BAD_PROVIDER = 1204&
    Public Const ERROR_BUSY = 170&
    Public Const ERROR_CANCELLED = 1223&
    Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
    Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
    Public Const ERROR_EXTENDED_ERROR = 1208&
    Public Const ERROR_INVALID_PASSWORD = 86&
    Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&

    Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
    (ByVal lpNetResource As NETCONNECT, ByVal lpPassword As String, ByVal lpUserName As String, _
    ByVal dwFlags As Long) As Long


    Private Sub btnConnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConnect.Click

    Dim username As String
    Dim password As String

    username = txtUsername.Text
    password = txtPassword.Text

    MapDrive("Z:", "\\myserver", username, password)

    End Sub


    Public Function MapDrive(ByVal LocalDrive As String, _
    ByVal RemoteDrive As String, ByVal Username As String, _
    ByVal password As String) As Boolean

    'Example:
    'MapDrive "Q:", "\\RemoteMachine\RemoteDirectory", _
    '"MyLoginName", "MyPassword"

    Dim NetResource As NETCONNECT
    NetResource.dwScope = RESOURCE_GLOBALNET
    NetResource.dwType = RESOURCETYPE_DISK
    NetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    NetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE
    NetResource.lpLocalName = LocalDrive
    NetResource.lpRemoteName = RemoteDrive
    NetResource.lpComment = Nothing
    NetResource.lpProvider = Nothing

    Dim returnValue As Integer
    returnValue = (WNetAddConnection2(NetResource, Username, password, CONNECT_UPDATE_PROFILE) = 0)

    If returnValue <> 0 Then
    Dim errorM = New System.ComponentModel.Win32Exception(returnValue).Message
    MsgBox("Could not connect to share. The server said this: " & vbNewLine & vbNewLine & "(Error " & returnValue & ") " & errorM)
    End If

    End Function


    End Class

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    2

    Re: PInvokeStackImbalance using WNetAddConnection2


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width