Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Map Network Drive Dialog Box

Status
Not open for further replies.

torturedmind

Programmer
Jan 31, 2002
1,052
0
0
PH
how can i activate the Map Network Drive dialog box or
show the Network Neighborhood window?

torturedmind [trooper]
 
Hi torturedmind,

There is a api function to open the dialog:

Declare Function WNetConnectionDialog Lib "mpr.dll" Alias "WNetConnectionDialog" (ByVal hwnd As Long, ByVal dwType As Long) As Long

Bye
LauDse
 
Related answer:

I also found the APIs to map and unmap drives directly, earlier today. See Microsoft's Knowledgebase:

Microsoft Knowledge Base Article - Q173011
HOWTO: Add and Remove Network Connections

{See the article mentioned in this one, about limitations. I ignored them, as they didn't apply to what *I* was doing, but it seems right to point it out.}


(There are multiple versions; I used these two. I could set the drive letter and path with strings -- which could be hard-coded, pulled from a form or inputbox, or ... which could be obtained any way you could obtain stings :) .)

This worked beautifully for me, using Access 97 (VBA). I tested the map and unmap buttons on a form with nothing else. This worked on Windows NT 4.0, 2000, and 98: Multiple (10 or 11) Windows NT 4.0 machines, 1 Windows 2000 machine, and 1 Windows 98 machine -- every PC I tried here.

-- CVigil =)
(I also Posted as JustPassingThrough and QuickieBoy -- as in "Just Quick Answers" -- before becoming a member.)


Text of article:
------------------------
HOWTO: Add and Remove Network Connections
The information in this article applies to:
Microsoft Visual Basic Learning Edition for Windows 6.0
Microsoft Visual Basic Professional Edition for Windows 6.0
Microsoft Visual Basic Enterprise Edition for Windows 6.0
Microsoft Visual Basic Control Creation Edition for Windows 5.0
Microsoft Visual Basic Learning Edition for Windows 5.0
Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Enterprise Edition for Windows 5.0
Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0

Summary
This article demonstrates how to programmatically create and remove network connections by using Windows API functions. The following example will add a connection to a network share and will disconnect from the same share.
More Information
NOTE: This code only includes minimal error trapping. It only reports success or failure of the API calls. Any production use of this code should examine the values returned by the API functions and handle the errors appropriately. The most common error constants are listed for this purpose.

Steps to Reproduce Behavior
Create a new Standard EXE project.
Add a module to the project.
Copy and paste the following Declares and Type into the module: (NOTE: If you add this to a Form module, make all entries 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

Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal fForce As Long) As Long

Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type

Public Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
' The following includes all the constants defined for NETRESOURCE,
' not just the ones used in this example.
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
' Error Constants:
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCELLED = 1223&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&

Add two CommandButtons to Form1. These will be Command1 and Command2 by default.
Add the following code to Form1, substituting a valid share name for "\\ServerName\ShareName":

Option Explicit

Private Sub Command1_Click()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
Dim MyPass As String, MyUser As String

NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = "X:" ' If undefined, Connect with no device
NetR.lpRemoteName = "\\ServerName\ShareName" ' Your valid share
'NetR.lpComment = "Optional Comment"
'NetR.lpProvider = ' Leave this undefined

' If the MyPass and MyUser arguments are null (use vbNullString), the
' user context for the process provides the default user name.
ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
MsgBox "Net Connection Successful!", vbInformation, _
"Share Connected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Connection Failed!", _
vbExclamation, "Share not Connected"
End If
End Sub

Private Sub Command2_Click()
Dim ErrInfo As Long
Dim strLocalName As String

' You may specify either the lpRemoteName or lpLocalName
'strLocalName = "\\ServerName\ShareName"
strLocalName = "X:"
ErrInfo = WNetCancelConnection2(strLocalName, _
CONNECT_UPDATE_PROFILE, False)
If ErrInfo = NO_ERROR Then
MsgBox "Net Disconnection Successful!", vbInformation, _
"Share Disconnected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Disconnection Failed!", _
vbExclamation, "Share not Disconnected"
End If
End Sub

Run the project and click on Command1. You will get a Message dialog indicating success or failure. If successful, you should be able to look in Windows Explorer and see the new connection (unless you left lpLocalName undefined, in which case the connection does not show in Explorer). Click Command2 and go to Explorer, where you should see that the connection has been removed.
References
For information about limitations using WNetAddConnection2 on different Windows platforms, please see the following article in the Microsoft Knowledge Base:

Q183366 : INFO: WNetAddConnection2 and Multiple User Credentials


For more information, please Search on the following topics in either the Win32 Programmer's Reference or The Microsoft Developer Network Library CD-ROM:

WNetAddConnection2
WNetCancelConnection2
NETRESOURCE
-- C Vigil =)
(Before becoming a member, I also signed on several posts as
"JustPassingThru" and "QuickieBoy" -- as in "Giving Quick Answers")
 
Has anyone tried to make a program behave like a mapped drive in the windows explorer shell?

I want to make a program that will look like a mapped drive, but the files and folders it will display are stored somewhere else. Full functionality (open save delete move...). Once I get it to look like a mapped drive I think I can do the rest. I am just having trouble finding how to do that.

Charles Killmer
chuck8773@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top