Compartir a través de


Determinar la ubicación de un recurso compartido

En el ejemplo siguiente se muestra cómo llamar a la función WNetGetUniversalName para determinar la ubicación de un recurso compartido en una unidad redirigida.

En primer lugar, el ejemplo de código llama a la función WNetGetUniversalName , especificando el nivel de información de UNIVERSAL_NAME_INFO para recuperar un puntero a una cadena de nombre de convención de nomenclatura universal (UNC) para el recurso. A continuación, el ejemplo llama a WNetGetUniversalName una segunda vez, especificando el nivel de información de REMOTE_NAME_INFO para recuperar dos cadenas de información de conexión de red adicionales. Si las llamadas se realizan correctamente, el ejemplo imprime la ubicación del recurso compartido.

Para probar el ejemplo de código siguiente, realice los pasos siguientes:

  1. Asigne al ejemplo de código el nombre GetUni.cpp.

  2. Agregue el ejemplo a una aplicación de consola denominada GetUni.

  3. Vincule las bibliotecas Shell32.lib, Mpr.lib y NetApi32.lib a la lista del compilador de bibliotecas.

  4. En el símbolo del sistema, cambie al directorio GetUni.

  5. Compile GetUni.cpp.

  6. Ejecute el archivo GetUni.exe seguido de una letra de unidad y dos puntos, de la siguiente manera:

    GetUni H:\

#define  STRICT
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#pragma comment(lib, "mpr.lib")

#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;
  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 with the UNIVERSAL_NAME_INFO_LEVEL option
  //
  printf("Call WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL.\n");
  if((res = WNetGetUniversalName((LPTSTR)argv[1],
         UNIVERSAL_NAME_INFO_LEVEL, // The structure is written to this block of memory. 
         (LPVOID) &szBuff, 
         &cbBuff)) != NO_ERROR) 
    //
    // If the call fails, print the error; otherwise, print the location of the share, 
    //  using the pointer to UNIVERSAL_NAME_INFO_LEVEL.
    //
    printf("Error: %ld\n\n", res); 
   
  else
    _tprintf(TEXT("Universal Name: \t%s\n\n"), puni->lpUniversalName); 
    
  //
  // Call WNetGetUniversalName with 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,    //Structure is written to this block of memory
         &cbBuff)) != NO_ERROR) 
    //
    // If the call fails, print the error; otherwise, print
    //  the location of the share, using 
    //  the pointer to REMOTE_NAME_INFO_LEVEL.
    //
    printf("Error: %ld\n", res); 
  else
    _tprintf(TEXT("Universal Name: \t%s\nConnection Name:\t%s\nRemaining Path: \t%s\n"),
          prni->lpUniversalName, 
          prni->lpConnectionName, 
          prni->lpRemainingPath);
  return;
}