1. 程式人生 > >c++ 開啟網路共享路徑

c++ 開啟網路共享路徑

#include "stdafx.h"


#include <Windows.h>
#include <tchar.h>
#include <WinNetWk.h>
#include <stdio.h>
#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;
}