# Visual Basic > Visual Basic .NET >  PInvoke Unbalanced Stack Error

## dprontnicki

I found some code online to map and disconnect network drives and I am getting an error I am unfamiliar with. I am trying to disconnect a "M:" Drive from a local UNC server and then path it to a SharePoint Library path. The attached error occurs during the "Unmapping". Below is all the code being used. And then any help on renaming the newly mapped drive would be greatly appreciated as I can't seem to find much on that. 



```
Imports System.Runtime.InteropServices

Public Class FrmMain

    Public Const NO_ERROR As UInteger = 0
    Public Const RESOURCETYPE_DISK As UInteger = 1
    Public Const CONNECT_UPDATE_PROFILE As UInteger = 1

#Region "Windows API DLL Imports"

    <DllImportAttribute("mpr.dll",
      EntryPoint:="WNetAddConnection2W")>
    Public Shared Function WNetAddConnection2(ByRef lpNetResource _
      As NETRESOURCE, <InAttribute(),
      MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal _
      lpPassword As String, <InAttribute(),
      MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal _
      lpUserName As String, ByVal dwFlags As UInteger) As UInteger
    End Function

    <DllImportAttribute("mpr.dll",
      EntryPoint:="WNetCancelConnectionW")>
    Public Shared Function WNetCancelConnection(<InAttribute(),
      MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal _
      lpName As String, ByVal dwFlags As UInteger,
      <MarshalAsAttribute(UnmanagedType.Bool)> ByVal _
      fForce As Boolean) As UInteger
    End Function

    <StructLayoutAttribute(LayoutKind.Sequential)>
    Public Structure NETRESOURCE

        Public dwScope As UInteger
        Public dwType As UInteger
        Public dwDisplayType As UInteger
        Public dwUsage As UInteger

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpLocalName As String

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpRemoteName As String

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpComment As String

        <MarshalAsAttribute(UnmanagedType.LPWStr)>
        Public lpProvider As String

    End Structure

#End Region

#Region "Subs"

    Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim currentVersion As String = My.Application.Info.Version.ToString
        LblHeader.Text = My.Settings.programHeaderText & " - v." & currentVersion
        LlblSharePointLink.Links.Add(0, 10, "https://*****.sharepoint.com/sites/******/*****/Forms/AllItems.aspx")

    End Sub

    Public Sub Map(ByVal strPath As String,
                   ByVal strDrive As Char, ByVal blnPersist As Boolean,
                   Optional ByVal strUser As String = Nothing,
                   Optional ByVal strPassword As String = Nothing)

        Dim nrDrive As New NETRESOURCE

        With nrDrive

            .dwType = RESOURCETYPE_DISK
            .lpLocalName = strDrive & ":"
            .lpRemoteName = strPath

        End With

        Dim uiSet As UInteger = 0

        If blnPersist Then

            uiSet = &H1

        End If

        Dim uiRes As UInteger = WNetAddConnection2(nrDrive,
         strPassword, strUser, uiSet)

        If Not uiRes = NO_ERROR Then

            Throw New System.ComponentModel.Win32Exception _
            (CInt(uiRes))

        End If

    End Sub

    Public Sub Unmap(ByVal cDrive As Char)

        Dim uiRes As UInteger = WNetCancelConnection(cDrive & ":", CONNECT_UPDATE_PROFILE, True)

        If Not uiRes = NO_ERROR Then

            Throw New System.ComponentModel.Win32Exception(CInt(uiRes))

        End If

    End Sub

#End Region

#Region "Form Objects"

    Private Sub BtnYes_Click(sender As Object, e As EventArgs) Handles BtnYes.Click

        Try

            Unmap("M"c)

        Catch ex As Exception

            MessageServices.DisplayError("Error Un-Mapping Local M:\ Drive!", "Please Contact *****.", ex)

        End Try

        Try

            Map("\\localhost\c$\Users\" & Environment.UserName & "\********\*****", "M"c, True)

        Catch ex As Exception

            MessageServices.DisplayError("Error Mapping SharePoint M:\ Drive Library!", "Please Contact ******.", ex)

        End Try


    End Sub

    Private Sub LlblSharePointLink_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LlblSharePointLink.LinkClicked

        Process.Start(e.Link.LinkData.ToString())

    End Sub

    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click

        Close()

    End Sub

    Private Sub PicExit_Click(sender As Object, e As EventArgs) Handles PicExit.Click

        Close()

    End Sub

#End Region

End Class
```

----------


## sapator

First of use int32 or Integer, not Uinteger , if you still get an error, there is another version you might try:



```
 <DllImport("mpr.dll")> _
    Private Shared Function WNetCancelConnection2(ByVal lpName As String, _
    ByVal dwFlags As Integer, _
     ByVal fForce As Integer) As Integer
    End Function
```

I'm not sure you need to marshal known types so the above seems more simple if it works.

----------


## vbdotnut

seems a bit excessive. you could probably try a command line solution with net use <drive> /delete. Make a .bat file that check for the drive and delete it if its connected

----------


## dprontnicki

> First of use int32 or Integer, not Uinteger , if you still get an error, there is another version you might try:
> 
> 
> 
> ```
>  <DllImport("mpr.dll")> _
>     Private Shared Function WNetCancelConnection2(ByVal lpName As String, _
>     ByVal dwFlags As Integer, _
>      ByVal fForce As Integer) As Integer
> ...


Thank you for the reply but I am still getting the same error. I have tried every combination of my existing code with changing to int32 and integer as well as your shorter version with int32 and integer. Any other ideas?  :Smilie:

----------


## jmcilhinney

I don't know what mpr.dll is but looking at the Microsoft documentation for the functions with those names, WNetCancelConnectionW only has two parameters WNetCancelConnection2W has three. Are these the same functions you're calling or different?

Also, the documentation for the former says this:



> The WNetCancelConnection function is provided for compatibility with 16-bit versions of Windows. Other Windows-based applications should call the WNetCancelConnection2 function.


so it sounds like you shouldn't be calling the former at all. That might be your problem.

----------

