Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Drive Mapping

Posted on 1998-07-23
7
1,901 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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