Platform SDK: Windows Networking |
Windows NT/Windows 2000
The following example demonstrates how to connect a drive letter to a remote server share with a call to the WNetAddConnection2 function. The sample informs the user whether or not the call was successful.
To test the following code sample, perform the following steps:
szUserName[32] = "myUserName", szPassword[32] = "myPassword", szLocalName[32] = "Q:", szRemoteName[MAX_PATH] = "\\\\products2\\relsys";
#include <windows.h> #include <stdio.h> #include <winnetwk.h> void main() { NETRESOURCE nr; DWORD res; TCHAR szUserName[32] = "MyUserName", szPassword[32] = "MyPassword", szLocalName[32] = "Q:", szRemoteName[MAX_PATH] = "\\\\products2\\relsys"; // // Assign values to the NETRESOURCE structure. // nr.dwType = RESOURCETYPE_ANY; nr.lpLocalName = (LPTSTR) &szLocalName; nr.lpRemoteName = (LPTSTR) &szRemoteName; nr.lpProvider = NULL; // // Call the WNetAddConnection2 function to assign // a drive letter to the share. // res = WNetAddConnection2(&nr, (LPTSTR) &szPassword, (LPTSTR) &szUserName, FALSE); // // If the call succeeds, inform the user; otherwise, // print the error. // if(res == NO_ERROR) printf("Connection added \n", szRemoteName); else printf("Error: %ld\n", res); return; }