共用方式為


判斷共用的位置

下列範例示範如何呼叫 WNetGetUniversalName 函式,以判斷重新導向磁片磁碟機上共用的位置。

首先,程式碼範例會呼叫 WNetGetUniversalName 函式,並指定 UNIVERSAL_NAME_INFO 資訊層級,以擷取資源的通用命名慣例指標 (UNC) 名稱字串。 然後範例會再次呼叫 WNetGetUniversalName ,並指定 REMOTE_NAME_INFO 資訊層級來擷取兩個額外的網路連線資訊字串。 如果呼叫成功,範例會列印共用的位置。

若要測試下列程式碼範例,請執行下列步驟:

  1. 將程式碼範例命名為 GetUni.cpp。

  2. 將範例新增至名為 GetUni 的主控台應用程式。

  3. 將程式庫 Shell32.lib、Mpr.lib 和 NetApi32.lib 連結至程式庫的編譯器清單。

  4. 從命令提示字元中,變更為 GetUni 目錄。

  5. 編譯 GetUni.cpp。

  6. 執行檔案GetUni.exe後面接著磁碟機號和冒號,如下所示:

    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;
}