Network Connect Get Disconnect v2
The WNetAddConnection2 variant of the network-drive example, using a NETRESOURCE structure to map a share with full error reporting.
The modernized companion to the plain WNetAddConnection example. Here the connection is described by a NETRESOURCE structure - scope, resource type (disk), display type, usage, local drive letter, remote UNC path - and passed to WNetAddConnection2, which also accepts username/password parameters and flags like CONNECT_UPDATE_PROFILE. The query (WNetGetConnection) and teardown (WNetCancelConnection) steps are the same as before, again with the full table of failure codes printed for each. Use this shape when you need to control more than the three bare string parameters - different resource types, persistence flags, or explicit credentials.
#include <Windows.H>
#include <WindowsX.H>
#include <Stdio.H>
#include <Stdlib.H>
#include <string.h>
#include <ShellAPI.H>
//////////////////////////////////////////////////////////////////////
int main()
{
char RBuffer[1024];
int SizeOfRBuffer = sizeof(RBuffer);
int Result = 0;
struct _NETRESOURCE NetResource;
NetResource.dwScope = RESOURCE_GLOBALNET;
NetResource.dwType = RESOURCETYPE_DISK;
NetResource.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
NetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE;
NetResource.lpLocalName = "S:";
NetResource.lpRemoteName = "\\SECUREPROVIDER\ShareBase";
NetResource.lpComment = "Comment";
NetResource.lpProvider = NULL;
// Result = WNetAddConnection2(&NetResource,"UserName","Password",FLAGS);
Result = WNetAddConnection2(&NetResource,0,0,0);
if(Result == ERROR_SUCCESS)
printf("Successfully Connected.\n");
if(Result == ERROR_ACCESS_DENIED)
printf("Access is denied.\n");
if(Result == ERROR_ALREADY_ASSIGNED)
printf("The device specified in the lpLocalName parameter is already connected.\n");
if(Result == ERROR_BAD_DEV_TYPE)
printf("The device type and the resource type do not match.\n");
if(Result == ERROR_BAD_DEVICE)
printf("The value specified in lpLocalName is invalid.\n");
if(Result == ERROR_BAD_NET_NAME)
printf("The value specified in the lpRemoteName parameter is not valid or cannot be located.\n");
if(Result == ERROR_BAD_PROFILE)
printf("The user profile is in an incorrect format.\n");
if(Result == ERROR_CANNOT_OPEN_PROFILE)
printf("The system is unable to open the user profile to process persistent connections.\n");
if(Result == ERROR_DEVICE_ALREADY_REMEMBERED)
printf("An entry for the device specified in lpLocalName is already in the user profile.\n");
if(Result == ERROR_EXTENDED_ERROR)
printf("A network-specific error occurred. To get a description of the error, use the WNetGetLastError function.\n");
if(Result == ERROR_INVALID_PASSWORD)
printf("The specified password is invalid.\n");
if(Result == ERROR_NO_NET_OR_BAD_PATH)
printf("The operation cannot be performed because either a network component is not started or the specified name cannot be used.\n");
if(Result == ERROR_NO_NETWORK)
printf("The network is not present.\n");
Result = 0;
//WNetGetConnection("Local Resource","Receive Buffer",&SizeOfRBuffer); // Get Connection
Result = WNetGetConnection("S:",RBuffer,&SizeOfRBuffer); // Get Connection
if(Result == ERROR_BAD_DEVICE)
printf("The string pointed to by the lpLocalName parameter is invalid.\n");
if(Result == ERROR_NOT_CONNECTED)
printf("The device specified by lpLocalName is not redirected.\n");
if(Result == ERROR_MORE_DATA)
printf("The buffer is too small. The lpnLength parameter points to a variable that contains the required buffer size. More entries are available with subsequent calls.\n");
if(Result == ERROR_CONNECTION_UNAVAIL)
printf("The device is not currently connected, but it is a persistent connection.\n");
if(Result == ERROR_NO_NETWORK)
printf("No network is present.\n");
if(Result == ERROR_EXTENDED_ERROR)
printf("A network-specific error occurred. To get a description of the error, use the WNetGetLastError function.\n");
if(Result == ERROR_NO_NET_OR_BAD_PATH)
printf("None of the providers recognized this local name as having a connection. However, the network is not available for at least one provider to whom the connection may belong.\n");
printf("%s\n",RBuffer);
Result = 0;
//WNetCancelConnection("Local Resource",TRUE/FALSE Force Dissconect); //Disconnect
Result = WNetCancelConnection("S:\\",TRUE); //Disconnect
if(Result == ERROR_SUCCESS)
printf("Successfully Disconnected.\n");
if( Result == ERROR_BAD_PROFILE)
printf("The user profile is in an incorrect format.\n");
if( Result == ERROR_CANNOT_OPEN_PROFILE)
printf("The system is unable to open the user profile to process persistent connections.\n");
if( Result == ERROR_DEVICE_IN_USE)
printf("The device is in use by an active process and cannot be disconnected.\n");
if( Result == ERROR_EXTENDED_ERROR)
printf("A network-specific error occurred. To get a description of the error, use the WNetGetLastError function.\n");
if( Result == ERROR_NOT_CONNECTED)
printf("The name specified by the lpName parameter is not a redirected device, or the system is not currently connected to the device specified by the parameter.\n");
if( Result == ERROR_OPEN_FILES)
printf("There are open files, and the fForce\n");
return 0;
}
The commented-out call shows where username, password, and flags would go; passing NULL/0 uses the current user's default credentials. Note the struct fields are set once and reused - zero-initialize the whole _NETRESOURCE first in real code so unused members stay clean.