UNC Path for a local drive

Posted on 1998-08-11
Last Modified: 2008-03-03
How can I  get a UNC path name of the local drive on which
my application is running.

for e.g. My application is running on C:
I should be able to get the path name as \\UTRASH\c$\myapplication
Question by:umeshkhoje

Expert Comment

ID: 1468571
This is not quite what you asked but may help...

I have done something similar way back in VB3 world, the way I did it was to add a hidden drivelistbox, then when the user choose a file I looked up the drive letter and got the UNC from the drivelistbox.


Accepted Solution

tward earned 50 total points
ID: 1468572
I have not tried this but you can try this function:

Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_MORE_DATA = 234 '  dderror
Public Const ERROR_NOT_SUPPORTED = 50&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Public Const ERROR_NO_NETWORK = 1222&
Public Const ERROR_NOT_CONNECTED = 2250&

Public Const UNIVERSAL_NAME_INFO_LEVEL = &H00000001&
Public Const REMOTE_NAME_INFO_LEVEL = &H00000002&

Declare Function WNetGetUniversalName Lib "mpr" Alias "WNetGetUniversalNameA" (ByVal lpLocalPath As String, ByVal dwInfoLevel As Long, lpBuffer As Any, lpBufferSize As Long) As Long

Here is an explanation:

The WNetGetUniversalName function takes a drive-based path for a network resource and obtains a data structure that contains a more universal form of the name.
DWORD WNetGetUniversalName(
    LPCTSTR lpLocalPath,      // pointer to drive-based path for a network resource
    DWORD dwInfoLevel,      // specifies form of universal name to be obtained
    LPVOID lpBuffer,      // pointer to buffer that receives universal name data structure
    LPDWORD lpBufferSize       // pointer to variable that specifies size of buffer
Points to a null-terminated string that is a drive-based path for a network resource.
For example, if drive H has been mapped to a network drive share, and the network resource of interest is a file named SAMPLE.DOC in the directory \WIN32\EXAMPLES on that share, the drive-based path is H:\WIN32\EXAMPLES\SAMPLE.DOC.
Specifies the type of data structure that the function will store in the buffer pointed to by lpBuffer. This parameter can be one of the following values:
Value      Meaning
UNIVERSAL_NAME_INFO_LEVEL      The function will store a UNIVERSAL_NAME_INFO data structure in the buffer.
REMOTE_NAME_INFO_LEVEL_      The function will store a REMOTE_NAME_INFO data structure in the buffer.
The UNIVERSAL_NAME_INFO data structure points to a Universal Naming Convention (UNC) name string.
Points to a buffer that receives the type of data structure specified by the dwInfoLevel parameter.
Points to a variable that specifies the size in bytes of the buffer pointed to by lpBuffer.
If the function succeeds, it sets the variable pointed to by lpBufferSize to the size in bytes of the data structure stored in the buffer. If the function fails because the buffer is too small, indicated by the ERROR_MORE_DATA error code, it sets the variable pointed to by lpBufferSize to the required buffer size.
Return Values
If the function succeeds, the return value is NO_ERROR.
If the function fails, the return value is an error code. To get extended error information, call GetLastError. GetLastError may return one of the following error codes:
Value      Meaning
ERROR_BAD_DEVICE      The string pointed to by lpLocalPath is invalid.
ERROR_CONNECTION_UNAVAIL      There is no current connection to the remote device, but there is a remembered (persistent) connection to it.
ERROR_EXTENDED_ERROR      A network-specific error occurred. Use the WNetGetLastError function to obtain a description of the error.
ERROR_MORE_DATA      The buffer pointed to by lpBuffer is too small. The function sets the variable pointed to by lpBufferSize to the required buffer size.
ERROR_NOT_SUPPORTED      The dwInfoLevel parameter was set to UNIVERSAL_NAME_INFO_LEVEL, but the network provider does not support UNC  names. This function is not supported by any of the network providers.
ERROR_NO_NET_OR_BAD_PATH      None of the providers recognized this local name as having a connection. However, the network is not available for at least one provider to whom the connection may belong.
ERROR_NO_NETWORK      There is no network present.
ERROR_NOT_CONNECTED      The device specified by lpLocalPath is not redirected.
A universal form of a local drive-based path identifies a network resource in an unambiguous, computer-independent manner. The name can then be passed to processes on other computers, allowing those processes to obtain access to the resource.
The WNetGetUniversalName function currently supports one universal name form: universal naming convention (UNC) names, which look like the following:
Using the example from the preceding description of the lpLocalPath parameter, if the shared network drive is on a server named COOLSERVER, and the share name is HOTSHARE, the UNC name for the network resource whose drive-based name is H:\WIN32\EXAMPLES\SAMPLE.DOC would be
The UNIVERSAL_NAME_INFO data structure contains a pointer to a UNC name string. The REMOTE_NAME_INFO data structure contains a pointer to a UNC name string. It also includes pointers to two other useful strings. A process can pass the REMOTE_NAME_INFO structure's lpszConnectionInfo member into the WNetAddConnection2 function to connect a local device to the network resource, and then append the string pointed to by the lpszRemainingPath member to the local device string. The resulting string can be passed to Win32 functions that use a drive-based path.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 37
Copy a row 12 59
SQL VB connection works in one PC and doesn't in another 15 58
Recommendation vb6 to or others 14 112
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

867 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

12 Experts available now in Live!

Get 1:1 Help Now