Determining the Location of a Share

Windows NT Only

The following example demonstrates how to use the WNetGetUniversalName function to determine the location of a share file on a redirected drive.

Name the code sample GetUni.cpp and add it to a Windows Consol App project called GetUni and link the libraries SHELL32.LIB, MPR.LIB, and NETAPI32.LIB to the compiler list of libraries.

Then from the command prompt, change to the GetUni directory and after compiling GetUni.cpp, run the file GetConn.EXE followed by a drive letter and colon like this:

GetUni H:\

/*
GetUni.cpp  code sample
*/
#define  STRICT
#include <windows.h>
#include <stdio.h>

#define BUFFSIZE = 1000

void main( int argc, char *argv[] )
{
DWORD cbBuff = 1000  //Size of Buffer
  TCHAR szBuff[1000];    //Buffer to receive information
  REMOTE_NAME_INFO  * prni = (REMOTE_NAME_INFO *)   &szBuff;
  //Pointers to head of buffer
  UNIVERSAL_NAME_INFO * puni = (UNIVERSAL_NAME_INFO *) &szBuff;
  DWORD res;

  if((argc < 2) | (lstrcmp(argv[1], "/?") == 0))
  {
    printf("Syntax:  GetUni DrivePathAndFilename\n"
         "Example: GetUni U:\\WINNT\\SYSTEM32\\WSOCK32.DLL\n");
    return;
  }
  
  //Call WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL option
  printf("Call WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL.\n");
  if((res = WNetGetUniversalName((LPTSTR)argv[1],
   UNIVERSAL_NAME_INFO_LEVEL,
   (LPVOID) &szBuff, //Structure is written to this block
   &cbBuff)) != NO_ERROR)     //of memory

    printf("Error: %ld\n\n", res);     //If it fails print the error
else
printf("Universal Name: \t%s\n\n",  //Otherwise print the results
puni->lpUniversalName);  //using the pointer to the REMOTE_NAME_INFO_LEVEL structure

 //Now call WNetGetUniversalName using the REMOTE_NAME_INFO_LEVEL option
  printf("Call WNetGetUniversalName using REMOTE_NAME_INFO_LEVEL.\n");
  if((res = WNetGetUniversalName((LPTSTR)argv[1], 
     REMOTE_NAME_INFO_LEVEL,     
        (LPVOID) &szBuff,    //Again the structure is written to 
       &cbBuff)) != NO_ERROR)     //this block of memory

    printf("Error: %ld\n", res);    //If it fails print the error
  else
    printf("Universal Name: \t%s\n"    //Otherwise print the results
         "Connection Name:\t%s\n"    //using the pointer to the REMOTE_NAME_INFO_LEVEL structure
       "Remaining Path: \t%s\n",
          prni->lpUniversalName, 
          prni->lpConnectionName, 
          prni->lpRemainingPath);
  return;
}