Author: Lou Adler
For file-based applications, how is the Universal Naming Convention obtained for
path names?
Answer:
To get the UNC name of a drive-based path, use the Windows API call
WNetGetUniversalName. This function takes a drive-based path (i.e., fully qualified
file name) and returns its UNC equivalent.
Be forewarned, though: This call is not supported by Windows 95. Here's something
that should work if you're in NT:
1 function GetUNCName(PathStr: string): string;
2 var3 bufSize: DWord;
4 buf: TUniversalNameInfo;
5 msg: string;
6 begin7 bufSize := SizeOf(TUniversalNameInfo);
8 if (WNetGetUniversalName(PChar(PathStr), UNIVERSAL_NAME_INFO_LEVEL,
9 buf, bufSize) > 0) then10 case GetLastError of11 ERROR_BAD_DEVICE: msg := 'ERROR_BAD_DEVICE';
12 ERROR_CONNECTION_UNAVAIL: msg := 'ERROR_CONNECTION_UNAVAIL';
13 ERROR_EXTENDED_ERROR: msg := 'ERROR_EXTENDED_ERROR';
14 ERROR_MORE_DATA: msg := 'ERROR_MORE_DATA';
15 ERROR_NOT_SUPPORTED: msg := 'ERROR_NOT_SUPPORTED';
16 ERROR_NO_NET_OR_BAD_PATH: msg := 'ERROR_NO_NET_OR_BAD_PATH';
17 ERROR_NO_NETWORK: msg := 'ERROR_NO_NETWORK';
18 ERROR_NOT_CONNECTED: msg := 'ERROR_NOT_CONNECTED';
19 end20 else21 msg := buf.lpUniversalName;
22 23 Result := msg;
24 end;