Network Connect Get Disconnect
A C program that connects to a network share with WNetAddConnection, queries it with WNetGetConnection, and disconnects with WNetCancelConnection — each with full error reporting.
The three classic WNet* connection calls in one program, each followed by an exhaustive if-chain that prints a human-readable explanation for every failure code the API can return. It maps a UNC share to a drive letter, asks Windows what remote path that letter currently points at, then tears the mapping down again (with force). Use it as a reference when you are building your own network-drive management UI or diagnosing why a mapped drive will not connect - the error-message table is the part worth stealing.
#include <Stdio.H>
#include <Stdlib.H>
#include <Windows.H>
//////////////////////////////////////////////////////////////////////
int main()
{
char RBuffer[1024];
int SizeOfRBuffer = sizeof(RBuffer);
int Result = 0;
//WNetAddConnection("Remote Resource","Password","Local Resource");
Result = WNetAddConnection("\\SECUREPROVIDER\Drv-C",0,"G:"); //Connect
if(Result == ERROR_SUCCESS)
printf("Success Connecting.\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("G:",RBuffer,&SizeOfRBuffer); // Get Connection
if(Result == ERROR_SUCCESS)
printf("Success Getting Connection.\n");
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");
if(Result == ERROR_SUCCESS)
printf("%s\n",RBuffer);
Result = 0;
//WNetCancelConnection("Local Resource",TRUE/FALSE Force Dissconect); //Disconnect
Result = WNetCancelConnection("G:\\",TRUE); //Disconnect
if(Result == ERROR_SUCCESS)
printf("Success Disconnecting.\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 first line of each section shows the generic signature; the live call substitutes real values (a server share, no stored credentials, drive letter G:). For new code prefer WNetAddConnection2/WNetCancelConnection2, which let you pass a NETRESOURCE structure and flags like CONNECT_UPDATE_PROFILE.