Solved

Drive Mapping

Posted on 1998-07-23
7
1,893 Views
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.

ORIGINAL QUESTION >>>>
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)


ANSWER FROM ALAMO >>>>>
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!


MY NEXT RESPONSE  >>>>>
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.  

. NEXT?


AND ALAMO's NEXT RESPONSE >>>>>>
>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?

FINALLY, MY LATEST  >>>>>
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.
Public Const ERROR_INVALID_ADDRESS = 487&

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

Accepted Solution

by:
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

Private Const ERROR_NO_CONNECTION = 8
Private Const ERROR_NO_DISCONNECT = 9

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

Err_Connect:
   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
Err_DisConnect:
   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


0
 
LVL 6

Expert Comment

by:alamo
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.
0
 

Author Comment

by:DavePlumb
ID: 1466507
BINGO !!!

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.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:DavePlumb
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.
0
 
LVL 14

Expert Comment

by:waty
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.

Thierry
0
 
LVL 6

Expert Comment

by:alamo
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 :-)
0
 
LVL 7

Expert Comment

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

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

OPTION AT QUESTION WITH ANSWER:
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now