Windows NT Only
The following example demonstrates how to use the WNetAddConnection2 function to connect a drive letter to a remote share on a server.
To test the code sample, change the following lines to valid strings:
szUserName[32] = "myUserName",
szPassword[32] = "myPassword",
szLocalName[32] = "Q:",
szRemoteName[MAX_PATH] = "\\\\products2\\relsys";
Add the file to a Windows Consol App project called AddConn2 and link the library MPR.LIB to the compiler list of libraries. Then, run the file AddConn2.EXE after you compile the following program.
#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";
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = (LPTSTR) &szLocalName;
nr.lpRemoteName = (LPTSTR) &szRemoteName;
nr.lpProvider = NULL;
res = WNetAddConnection2(&nr, (LPTSTR) &szPassword, (LPTSTR) &szUserName, FALSE);
if(res == NO_ERROR)
printf("Connection added \n", szRemoteName);
else
printf("Error: %ld\n", res);
return;
}