PSS ID Number: 131416
Article Last Modified on 10/30/2003
#include <windows.h>
#include <stdio.h>
// Function Name: GetUniversalName
//
// Parameters: szUniv - Contains the UNC equivalent of szDrive
// upon completion.
//
// szDrive - Contains a drive-based path.
//
// Return value: TRUE if successful; otherwise, FALSE.
//
// Comments: This function assumes that szDrive contains a
// valid drive-based path.
//
// For simplicity, this code assumes szUniv points
// to a buffer large enough to accommodate the UNC
// equivalent of szDrive.
BOOL GetUniversalName( char szUniv[], char szDrive[] )
{
// Get the local drive letter.
char chLocal = toupper( szDrive[0] );
// Cursory validation.
if ( chLocal < 'A' || chLocal > 'Z' )
return FALSE;
if ( szDrive[1] != ':' || szDrive[2] != '\\' )
return FALSE;
HANDLE hEnum;
DWORD dwResult = WNetOpenEnum( RESOURCE_CONNECTED, RESOURCETYPE_DISK,
0, NULL, &hEnum );
if ( dwResult != NO_ERROR )
return FALSE;
// Request all available entries.
const int c_cEntries = 0xFFFFFFFF;
// Start with a reasonable buffer size.
DWORD cbBuffer = 50 * sizeof( NETRESOURCE );
NETRESOURCE *pNetResource = (NETRESOURCE*) malloc( cbBuffer );
BOOL fResult = FALSE;
while ( TRUE )
{
DWORD dwSize = cbBuffer,
cEntries = c_cEntries;
dwResult = WNetEnumResource( hEnum, &cEntries, pNetResource,
&dwSize );
if ( dwResult == ERROR_MORE_DATA )
{
// The buffer was too small, enlarge.
cbBuffer = dwSize;
pNetResource = (NETRESOURCE*) realloc(pNetResource, cbBuffer);
continue;
}
if ( dwResult != NO_ERROR )
goto done;
// Search for the specified drive letter.
for ( int i = 0; i < (int) cEntries; i++ )
if ( pNetResource[i].lpLocalName &&
chLocal == toupper(pNetResource[i].lpLocalName[0]) )
{
// Match.
fResult = TRUE;
// Build a UNC name.
strcpy( szUniv, pNetResource[i].lpRemoteName );
strcat( szUniv, szDrive + 2 );
_strupr( szUniv );
goto done;
}
}
done:
// Clean up.
WNetCloseEnum( hEnum );
free( pNetResource );
return fResult;
}
An alternative workaround to using WNetOpenEnum and WNetEnumResource is to
use WnetGetConnection, which, when provided the drive letter of a shared
drive, returns the UNC name that is mapped to that drive.
Keywords: kbAPI kbbug kbcode kbnetwork kbWNet KB131416
Technology: kbAudDeveloper kbWin32API kbWin32sSearch kbWin95search kbZNotKeyword3