To retrieve the name of the network resource associated with a local device, an application can call the WNetGetConnection function, as shown in the following example. The sample calls an application-defined error handler to process errors, and the TextOut function for printing. CHAR szDeviceName[80]; DWORD dwResult, cchBuff = sizeof(szDeviceName);
switch (dwResult) { // // Print the connection name or process errors. // case NO_ERROR: TextOut(hdc, 10, 10, (LPSTR) szDeviceName, lstrlen((LPSTR) szDeviceName)); break; // // The device is not a redirected device. // case ERROR_NOT_CONNECTED: TextOut(hdc, 10, 10, "Device z: not connected.", 24); // // The device is not currently connected, // but it is a persistent connection. // case ERROR_CONNECTION_UNAVAIL: TextOut(hdc, 10, 10, "Connection unavailable.", 23); // // Call an application-defined error handler. // default: NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetGetConnection"); return FALSE; }
DWORD dwResult, cchBuff = sizeof(szDeviceName);
// Call the WNetGetConnection function.
//
dwResult = WNetGetConnection("z:",
(LPSTR) szDeviceName,
&cchBuff);
switch (dwResult)
{
//
// Print the connection name or process errors.
//
case NO_ERROR:
TextOut(hdc, 10, 10, (LPSTR) szDeviceName,
lstrlen((LPSTR) szDeviceName));
break;
//
// The device is not a redirected device.
//
case ERROR_NOT_CONNECTED:
TextOut(hdc, 10, 10, "Device z: not connected.", 24);
//
// The device is not currently connected,
// but it is a persistent connection.
//
case ERROR_CONNECTION_UNAVAIL:
TextOut(hdc, 10, 10, "Connection unavailable.", 23);
//
// Call an application-defined error handler.
//
default:
NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetGetConnection");
return FALSE;
}