Drive Mapping

Posted on 1998-07-23
Last Modified: 2008-02-01
This s a re-post of a question from Tuesday that got in the wrong category.  I've tried to include the responses as best I could.

I've got a VB program that reads a database to find the location of a project, and then maps a Drive letter to an NT share.  It works great in 95 (clients are 95, servers are NT), but I'm getting an error code 1326 when I try to run it on an NT client.  I'm pretty sure I've narrowed it down to the way the password is handled, but I don't see any way around it.  According to the Win32API help, the WNetAddConnection call can accept a NULL for the password, and thereby "use the default password".  My problem is that the declare in the Library uses a String for the password, and I can't pass a NULL (which, of course is different than an empty string)  If I use an empty string, the WIN32API help file says wNetAddConnection  "uses no password"

The VB declare for WNetAddConnection:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As Variant, ByVal lpszLocalName As String) As Long

My call to WNetAddConnection:
lResult = WNetAddConnection(sProjectPath, "", csDRIVE)

If I could just use a Variant for the password, all would be well, but I can't change the Library.

P.S.  ( I am aware of the WNetAddConnection2 & 3 functions, but I don't think they would make any difference.  They still have the same declares for the password)

Rejected AnswerFrom: alamoDate: Tuesday, July 21 1998 - 11:12AM PDT Well, this should be in the VB area, but migth as well answer it here...

There are a number of ways to handle this. The easiest is to use vbNullString - this is a special value to signal VB to pass 0 rather than a real string.
So your call would be:
lResult = WNetAddConnection(sProjectPath, vbNullString, csDRIVE)
and your Declare would of course have the password as "ByVal lpszPassword As String" not as Variant.

The other way to handle this is to change your declaration to one VB will accept but which still calls the function in the identical manner:

Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As Any, ByVal lpszLocalName As String) As Long

Now you could call either of the following and it would work:
lResult = WNetAddConnection(sProjectPath, 0&, csDRIVE)
lResult = WNetAddConnection(sProjectPath, "password", csDRIVE)

Hope this helps!

Great answer.
However, it didn't fix my problem.  Now the error code I get is
#47 - Invalid address (I'm doing that from memory, so I may not have it right)

In any event, now my problem looks to be that VB won't accept a NULL for the password, which NT wants to use to just pick up the user's default password.  


>In any event, now my problem looks to be that VB won't accept a NULL for the password

Nope, the code I posted passes NULL for password. That's not your problem.

It's more likely that sProjectPath or  csDRIVE are wrong, but you didn't post the portion of code which sets them. What are they?

I should have worded my response more carefully.
I should have said:
It looks like the VB call to WNetAddConnection can't handle a NULL as password.  The  sProjectPath and  csDRIVE  have been working fine under 95 for months.  It's only when I mess with the password parameter that I run into these problems.
I also should not have tried to remeber the error code.  The actual error code is #487 ( I had guessed 47).  This WIN32API.TXT file I have describes this error as:
'   Attempt to access invalid address.

Sorry for the long post, and Thanks for any help.
Question by:DavePlumb
  • 2
  • 2
  • 2
  • +1
LVL 14

Accepted Solution

waty earned 100 total points
ID: 1466505
You should use the following class. I use it, and works perfectly.

Option Explicit
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) As Long

Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34

'-- Error number and message
Public ErrorNum         As Long
Public ErrorMsg         As String

Public rc               As Long


Public Sub Connect(sDrive As String, sService As String, Optional sPassword As String = "")
   On Error GoTo Err_Connect
   Me.ErrorNum = 0
   Me.ErrorMsg = ""
   rc = WNetAddConnection(sService & Chr(0), sPassword & Chr(0), sDrive & Chr(0))
   If rc <> 0 Then GoTo Err_Connect
   Exit Sub

   Me.ErrorNum = rc
   Me.ErrorMsg = WnetError(rc)

End Sub

Public Sub DisConnect(sDrive As String)
   On Error GoTo Err_DisConnect
   Me.ErrorNum = 0
   Me.ErrorMsg = ""
   rc = WNetCancelConnection(sDrive + Chr(0), 0)
   If rc <> 0 Then GoTo Err_DisConnect
   Exit Sub
   Me.ErrorNum = rc
   Me.ErrorMsg = WnetError(rc)

End Sub

Private Function WnetError(Errcode As Long) As String

   Select Case Errcode
      Case WN_Not_Supported:
         WnetError = "Function is not supported."
      Case WN_Out_Of_Memory:
         WnetError = "Out of Memory."
      Case WN_Net_Error:
         WnetError = "An error occurred on the network."
      Case WN_Bad_Pointer:
         WnetError = "The Pointer was Invalid."
      Case WN_Bad_NetName:
         WnetError = "Invalid Network Resource Name."
      Case WN_Bad_Password:
         WnetError = "The Password was Invalid."
      Case WN_Bad_Localname:
         WnetError = "The local device name was invalid."
      Case WN_Access_Denied:
         WnetError = "A security violation occurred."
      Case WN_Already_Connected:
         WnetError = "The local device was connected to a remote resource."
      Case Else:
         WnetError = "Unrecognized Error " + Str(Errcode) + "."
   End Select

End Function


Expert Comment

ID: 1466506
The class doesn't solve the basic problem - it doesn't allow a null password.

Dave, did you try both declares I posted before? It's the latter (declaring the parameter as byval any and passing 0&) that I consider more reliable - if you only tried the vbNullString one but somehow you don't have a definition for vbNullString it won't work.

Author Comment

ID: 1466507

I guess I need to attend that Thoroughness class, after all.
I looked at your first response and focused on the vbNullString part.  
I didn't catch  the declare "as Any" part until your latest post.

Once I changed my declare to what you had originally suggested, it worked great!!

Thanks a bunch.  I appreciate your sticking with this, even though it was posted in the wrong place.
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


Author Comment

ID: 1466508
Sorry about the points, alamo.
Since your name was on the bottom of the list, I assume you'd get credit.
I was quit surprised when I saw waty's namw flash by, but I didn't know how to recall it.  
I'll mail the sysops.
LVL 14

Expert Comment

ID: 1466509
Sorry guys,
I am new here (since 1 week).
I think, what I can do is posting an answer for Alamo and give the points.

Let me know if it possible.


Expert Comment

ID: 1466510
Dave, the person who has submitted an "Answer" rather than a comment gets the points - and there can be only one answer.

The easiest way to deal with this is for you to grade me on the question in Customer Service, accepting my answer there, instead of having that question deleted. You don't even have to email E-E for that. But it's up to you.

waty, thanks, that's Ok. Your answer was a good one, just not for this specific question. It's not uncommon that people get confused at the user interface here like Dave did, a lot of people have asked them to make it clearer but they never have. Just consider yourself lucky :-)

Expert Comment

ID: 1466511
I'll post a question in this topic are for alamo to award him points.

For your future information when using the site:
When you go to grade a question you have the following options:

1. Grade linda's answer:
   A: Excellent! B: Good! C: Average.
   D: Acceptable. E: Reopen question to other experts.
If the proposed answer is not the one which helped you the most, you choose the option to reopen the question to other experts. And a comment ask the person who you want to award the points to post an answer.  When they post an answer you can then grade them.

Linda Gardner
Customer Service @ Experts Exchange

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question