Results 1 to 4 of 4

Thread: [RESOLVED] Help Troubleshooting network drive mapping

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    5

    Resolved [RESOLVED] Help Troubleshooting network drive mapping

    Hi guys, I was hoping you could help me out with a problem I'm having.

    I'm a fairly novice VB programmer, I know my way around and I can read the code but I'm very far from being an expert. However, we have an internal program that needs a little work, the original programmer has been gone for about 2 years and all of the more seasoned programmers in the office work primarily in C and are unwilling/too busy to take on a VB project, so I decided to take it on.

    Well, I believe I have fixed the first bug in my program, but I have hit a snag. It seems the code the original programmer used won't compile at all, the section of code that maps one of the network drives calls too many arguments. Here is the code to see what I mean:

    This is the code that calls the functions to map the drives in the main form (I've taken out the irrelevant bits). .Drive, .StatusDrive, and .StatusShare are set in another module, sUNCShare is a separate module from those three, and they all call up the propper information when I put a break in and check the values before the code to map the drives are run:
    Code:
            lErrorCode = mn.DisconnectNetworkDrive(.Drive)
            lErrorCode = mn.DisconnectNetworkDrive(.StatusDrive)
        ' -> First try
        ' try connecting using default credentials that the user is logged in as
        '
            lErrorCode = mn.ConnectNetworkDrive(.Drive, sUNCShare, vbNullString, vbNullString)
            
            If lErrorCode = 0 Then GoTo Success
            If lErrorCode <> ERROR_LOGON_FAILURE Then GoTo Errorhandler
    
    Success:
        lErrorCode = mn.ConnectNetworkDrive(.StatusDrive, .StatusShare, vbNullString, vbNullString, UNCReturnFull)
    The ConnectNetworkDrive is a function in a module called MyNetwork:
    Code:
        '* API function: ConnectNetworkDrive, DisconnectNetworkDrive
        '*
            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
    
    ...
    
    ' ConnectNetworkDrive property
    '
    Public Property Get ConnectNetworkDrive(LocalDrive As String, PATH As String, UserName As String, Password As String) As Long
    
            Dim NetR As NETRESOURCE
    
        ' setup NetR structure for drive connection
        '
            With NetR
                .dwScope = RESOURCE_GLOBALNET
                .dwType = RESOURCETYPE_DISK
                .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
                .dwUsage = RESOURCEUSAGE_CONNECTABLE
                .lpLocalName = LocalDrive
                .lpRemoteName = ms.ExtractUNC(PATH, UNCReturnShare)
            End With
    
        ' Non-Persistant connection: Default (do not reconnect after reboot)
        '
            ConnectNetworkDrive = WNetAddConnection2(NetR, Password, UserName, 0)
    
        ' Persistant connection (connect again after reboot)
        '
            'ConnectNetworkDrive = WNetAddConnection2(NetR, Password, UserName, CONNECT_UPDATE_PROFILE)
    
    End Property
    
    Public Property Get DisconnectNetworkDrive(LocalDrive As String) As Long
            DisconnectNetworkDrive = WNetCancelConnection2(LocalDrive, CONNECT_UPDATE_PROFILE, True)
    End Property
    Here's the thing, as you may have noticed, the ConnectNetworkDrive function only has four parameters. The code:
    Code:
        lErrorCode = mn.ConnectNetworkDrive(.StatusDrive, .StatusShare, vbNullString, vbNullString, UNCReturnFull)
    has five, and so obviously it blows up when I try to run it. The apparent extraneous argument, UNCReturnFull, returns a 0. When I put a ) after vbNullString and comment out the the rest of the line (the last comma onward) it will compile but neither of the ConnectNetworkDrive functions operate completely correctly.

    I get two mapped drives like I am supposed to, the information is being correctly pulled from ini files like it is supposed to, the code for connectnetworkdrive is the same as several other programs use, and it is same as what the original executeable was built with. None of that has changed. But when I run the code, the drives are mapped to the DFS share (even though they may say they mapped to the correct location) instead of what is passed in those arguments - it is supposed to be mapping to \\server\share\folder\ but what I get is \\domain\dfs\.

    The entire operation hinges on getting at some ini files that are updated regularly on the server share. What am I missing? How the heck was the code compiled in the first place? I checked the timestamps, it's definitely the code he used, but even with no changes it won't compile for me. What am I missing?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Help Troubleshooting network drive mapping

    I don't know anything about either API but WNetAddConnectionA (note no "2") contains only three parameters.


    From the API Guide:

    Code:
    Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    · lpRemoteName
    Points to a null-terminated string that specifies the network resource to connect to.

    · lpPassword
    Points to a null-terminated string that specifies the password to be used to make a connection. This parameter is usually the password associated with the current user.
    If this parameter is NULL, the default password is used. If the string is empty, no password is used.

    · lpLocalName
    Points to a null-terminated string that specifies the name of a local device to be redirected, such as F: or LPT1. The case of the characters in the string is not important. If the string is NULL, a connection to the network resource is made without redirecting the local device.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    5

    Re: Help Troubleshooting network drive mapping

    WNetAddConnection2 is definitely a legit API call from mpr.dll, see here for more info on it: http://allapi.mentalis.org/apilist/W...nection2.shtml

    And it is working to a degree, it does map a share to the correct drive letters, but as I dug a little more I noticed that both servers have DFS shares on them, and instead of mapping to either the root of the drives or the correct folders (which, again, the correct values do make it into the correct variables before the code in question is run) they map to the dfs root.

    In other words, both servers have dfs shares - \\server1\dfs and \\server2\dfs. The code should be mapping a drive to \\server1\folder1 and \\server2\dfs\folder2, but instead they both map to the \dfs shares on each machine.

    Furthermore, I still can't understand how he got the code to compile in the first place, as it is I can't get it past the second connectnetworkdrive function call without removing the fifth variable.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    5

    Re: Help Troubleshooting network drive mapping

    Ok, with a little more digging, here is what I believe is happening.

    First, WNetAddConnection2 has four parameters, .lpNetResource .lpPassword .lpUsername and .dwFlags.

    Second, it recognizes only six members of the NETRESOURCE structure for the first variable, that's what the With NetR ... End With section is about - setting those variables within the .lpNetResource variable.

    And as a test, I replaced this line:
    Code:
    .lpRemoteName = ms.ExtractUNC(PATH, UNCReturnShare)
    with this line:
    Code:
    .lpRemoteName = PATH
    Lo and behold, it works flawlessly.

    Now now I have to troubleshoot ms.ExtractUNC(PATH, UNCReturnShare) to figure out what is wrong with it. I don't even know why it is set that way, and it is in a class module that several other programs use, so I can't just change it. I can at least get a working executeable however.

    Thanks for helping me work through this, I hadn't thought to investigate the API calls.

    Still though, how the heck did he get it to compile in the first place, if this stuff was wrong? As far as I know nobody changed the class modules since my predecessor came through, but I can't say for sure. I suppose he could have been using customized libraries and didn't update the shared libraries too.
    Last edited by bigjeff5; Jul 19th, 2009 at 08:37 PM.

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