WNetAddConnection2 call: "PInvokeStackImbalance was detected"

Discussion in 'ASP .Net' started by John M. Gamble, Jun 8, 2007.

  1. I'm getting this message in Visual Studio 2005:
    PInvokeStackImbalance was detected

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

    My goal is to connect to a remote filesystem. I don't want to
    create a mapping to the filesystem. I just want to connect using the
    login name and password that I already have. The web sites that I
    googled around at stated that WNetAddConnection2() could be used for
    this purpose, particularly

    <http://groups.google.com/group/microsoft.public.vb.winapi.networks/browse_t
    hread/thread/f61e50f43601670b/005f911e0da84edc?lnk=st&q=LOGON+VPN+VB&rnum=7&
    hl=en#>

    The code in question is this:


    ' dwFlags values.
    Private Const CONNECT_UPDATE_PROFILE As Long = &H1
    Private Const CONNECT_INTERACTIVE As Long = &H8
    Private Const CONNECT_PROMPT As Long = &H10

    ' dwType values.
    Private Const RESOURCETYPE_ANY As Long = &H0
    Private Const RESOURCETYPE_DISK As Long = &H1
    Private Const RESOURCETYPE_PRINT As Long = &H2
    Private Const RESOURCETYPE_RESERVED As Long = &H8
    Private Const RESOURCETYPE_UNKNOWN As Long = &HFFFFFFFF

    ' dwScope values.
    Private Const RESOURCE_CONNECTED As Long = &H1
    Private Const RESOURCE_GLOBALNET As Long = &H2
    Private Const RESOURCE_REMEMBERED As Long = &H3
    Private Const RESOURCE_RECENT As Long = &H4
    Private Const RESOURCE_CONTEXT As Long = &H5

    ' dwDisplayType values.
    Private Const RESOURCEDISPLAYTYPE_DOMAIN As Long = &H1
    Private Const RESOURCEDISPLAYTYPE_GENERIC As Long = &H0
    Private Const RESOURCEDISPLAYTYPE_SERVER As Long = &H2
    Private Const RESOURCEDISPLAYTYPE_SHARE As Long = &H3

    Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1
    Private Const SW_SHOWNORMAL As Long = 1

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

    ' The examples used Private Type but Visual Studio complained
    ' and said that Structure was now preferred.

    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


    ' Function code here.
    Dim netstruct As New NETRESOURCE
    Dim errcode As Long

    With netstruct
    .dwScope = RESOURCE_GLOBALNET
    .dwType = RESOURCETYPE_ANY
    .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    .dwUsage = RESOURCEUSAGE_CONNECTABLE
    .lpRemoteName = backup_base_dir
    .lpLocalName = vbNullString
    .lpProvider = vbNullString
    End With

    errcode = WNetAddConnection2(netstruct, _
    strLogPass, strLogName, _
    CONNECT_INTERACTIVE Or CONNECT_UPDATE_PROFILE)

    Am I using contradictory options? Or am I using the wrong function
    to begin with?

    Thanks,

    --
    -john

    February 28 1997: Last day libraries could order catalogue cards
    from the Library of Congress.
     
    John M. Gamble, Jun 8, 2007
    #1
    1. Advertising

  2. "John M. Gamble" <> schrieb:
    > I'm getting this message in Visual Studio 2005:
    > PInvokeStackImbalance was detected
    >
    > Message: A call to PInvoke function
    > 'Refresh!Refresh.Main::WNetAddConnection2' has unbalanced the stack.
    >
    > [...]


    I assume you took the sample from a VB6 projekt or code repository. Note
    that VB 2005 is not a technical successor of VB6 and code is generally not
    compatible between the two programming languages. Whereas 'Long' was a
    32-bit data type in VB6, it's a 64-bit type in VB.NET. Thus you'll have to
    modify your declarations, constants, and structures accordingly (this means,
    changing 'Long' to 'Integer' or 'Int32', with some exceptions). Note that
    some additional modifications of the source code may be necessary in order
    to make the code work.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
     
    Herfried K. Wagner [MVP], Jun 8, 2007
    #2
    1. Advertising

  3. In article <>,
    Herfried K. Wagner [MVP] <> wrote:
    >"John M. Gamble" <> schrieb:
    >> I'm getting this message in Visual Studio 2005:
    >> PInvokeStackImbalance was detected
    >>
    >> Message: A call to PInvoke function
    >> 'Refresh!Refresh.Main::WNetAddConnection2' has unbalanced the stack.
    >>
    >> [...]

    >
    >I assume you took the sample from a VB6 projekt or code repository. Note


    Hmm. The claim was that it was from a dotnet source, but given the other
    change I had to make I am suspicious.

    >that VB 2005 is not a technical successor of VB6 and code is generally not
    >compatible between the two programming languages. Whereas 'Long' was a
    >32-bit data type in VB6, it's a 64-bit type in VB.NET. Thus you'll have to
    >modify your declarations, constants, and structures accordingly (this means,
    >changing 'Long' to 'Integer' or 'Int32', with some exceptions). Note that
    >some additional modifications of the source code may be necessary in order
    >to make the code work.
    >


    Thanks, I'll start with that.

    --
    -john

    February 28 1997: Last day libraries could order catalogue cards
    from the Library of Congress.
     
    John M. Gamble, Jun 8, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bill sandner
    Replies:
    1
    Views:
    903
    iksrazal
    Jul 1, 2004
  2. Buu Nguyen

    RMI: remote call or local call

    Buu Nguyen, Sep 9, 2004, in forum: Java
    Replies:
    5
    Views:
    818
    Esmond Pitt
    Sep 17, 2004
  3. hasho

    call by address vs. call by value

    hasho, Feb 10, 2004, in forum: C Programming
    Replies:
    35
    Views:
    3,105
    E. Robert Tisdale
    Feb 11, 2004
  4. Sean DiZazzo
    Replies:
    2
    Views:
    1,128
    Sean DiZazzo
    Apr 15, 2010
  5. varnk
    Replies:
    2
    Views:
    1,433
    varnk
    Jul 28, 2010
Loading...

Share This Page