WNetAddConnection2 error

captkirk1001

New member
Joined
Jun 20, 2006
Messages
3
Programming Experience
10+
When I am receiving a PinvokeStackImbalance error when I execute the following code. I can not find the error. Any help would greatly be appreciated.

Public Class MapDrive
Private Structure NETRESOURCE
Dim dwScope As Long
Dim dwType As Long
Dim dwDisplayType As Long
Dim dwUsage As Long
Dim lpLocalName As String
Dim lpRemoteName As String
Dim lpComment As String
Dim lpProvider As String
End Structure

Private Const ERROR_SUCCESS = 0
Private Const CONNECT_UPDATE_PROFILE As Long = &H0
Private Const RESOURCETYPE_DISK As Long = &H1
Private Const RESOURCETYPE_PRINT As Long = &H2
Private Const RESOURCETYPE_ANY As Long = &H0
Private Const RESOURCE_GLOBALNET As Long = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE As Long = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1
Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2A" _
(
ByRef lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long
Private Declare Function WNetAddConnection Lib "mpr.dll" _
Alias "WNetAddConnectionA" _
(
ByVal lpszNetPath As String, _
ByVal lpszPassword As String, _
ByVal lpszLocalName As String) As Integer



Private Sub MapCMM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MapCMM.Click
Dim NETR As NETRESOURCE
Dim errInfo As Long
Dim errInfoInt As Integer
Dim sServer As String
Dim sUsername As String
Dim sPassword As String
Dim sDriveLtr As String
sPassword = UserPass.Text
sUsername = UserName.Text
sServer =
"\\server1\toolin"
sDriveLtr = "j:"
NETR.dwScope = RESOURCE_GLOBALNET
NETR.dwType = RESOURCETYPE_DISK
NETR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NETR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NETR.lpRemoteName = sServer
NETR.lpLocalName = sDriveLtr
NETR.lpComment = vbNullString
NETR.lpProvider = vbNullString
errInfo = WNetAddConnection2(NETR, sPassword, sUsername, CONNECT_UPDATE_PROFILE)
MsgBox(errInfo)
'errInfoInt = WNetAddConnection(sServer, sPassword, sDriveLtr)
'MsgBox(errInfoInt)


End Sub
End
Class

Carol K
 
This is just a stab in the dark, am i may well be totally of game here. But it may be that you need to change the variable assignments and the function return values from 'long' to 'integer'
 
Interesting, are you coming from vb background? it's just that in vb.net an integer is equivalent to a vb long. I ran the api you are using through my old trusty API viewer and after setting the compatibility to vb.net this was the function that was returned...

VB.NET:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" 
(ByVal lpszNetPath As String, ByVal lpszPassword As String, _
ByVal lpszLocalName As String) As Int32

And structure returned as....

VB.NET:
<StructLayout(LayoutKind.Sequential)> _ 
Private Structure NETRESOURCE
 Public dwScope As Int32
 Public dwType As Int32
 Public dwDisplayType As Int32
 Public dwUsage As Int32
 Public lpLocalName As String
 Public lpRemoteName As String
 Public lpComment As String
 Public lpProvider As String
End Structure
 
Thanks

I am new to VB programming. That is very good information. Where do I access the API broswer and complete the comparision that you do? Thanks
:rolleyes:
 
Back
Top