c++ネットワーク共有パスを開く

1540 ワード

#include "stdafx.h"


#include 
#include 
#include 
#include 
#pragma comment(lib, "Mpr.lib")

int _tmain(int argc, TCHAR **argv)
{

	TCHAR szPasswd[] = TEXT("90523");          //           
	TCHAR szUserName[] = TEXT("Administrator");        //         

	NETRESOURCE net_Resource;
	net_Resource.dwDisplayType = RESOURCEDISPLAYTYPE_DIRECTORY;
	net_Resource.dwScope = RESOURCE_CONNECTED;
	net_Resource.dwType = RESOURCETYPE_ANY;
	net_Resource.dwUsage = 0;
	net_Resource.lpComment = TEXT("");
	net_Resource.lpLocalName = TEXT("z:");  //        z:
	net_Resource.lpProvider = NULL;
	net_Resource.lpRemoteName = TEXT("\\\\172.16.80.62\\g"); // \\servername\     
	DWORD dwFlags = CONNECT_UPDATE_PROFILE;
	WNetCancelConnection2(net_Resource.lpLocalName, CONNECT_UPDATE_PROFILE, TRUE);
	return 1;
	DWORD dw = WNetAddConnection2(&net_Resource, szPasswd, szUserName, dwFlags);
	switch (dw)
	{
	case ERROR_SUCCESS:
		ShellExecute(NULL, TEXT("open"), TEXT("z:"), NULL, NULL, SW_SHOWNORMAL);
		break;
	case ERROR_ACCESS_DENIED:
		wprintf(TEXT("     !"));
		break;
	case ERROR_ALREADY_ASSIGNED:
		ShellExecute(NULL, TEXT("open"), TEXT("z:"), NULL, NULL, SW_SHOWNORMAL);
		break;
	case ERROR_INVALID_ADDRESS:
		wprintf(TEXT("IP    "));
		break;
	case ERROR_NO_NETWORK:
		wprintf(TEXT("     !"));
		break;
	}
	return 0;
}