Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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:
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 console application called AddConn2.
Link the library MPR.LIB to the compiler list of libraries.
Compile and run the program AddConn2.EXE.
#include <windows.h>
#include <stdio.h>
#include <winnetwk.h>
#pragma comment(lib, "mpr.lib")
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 = szLocalName;
nr.lpRemoteName = szRemoteName;
nr.lpProvider = NULL;
//
// Call the WNetAddConnection2 function to assign
// a drive letter to the share.
//
res = WNetAddConnection2(&nr, szPassword, 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;
}