#include <windows.h> #include <WinNetWk.h> #include <iostream> #pragma comment(lib, "Mpr.lib") using namespace std; int wmain(int argc,wchar_t * argv[]) { /* DWORD WNetAddConnection2W( LPNETRESOURCEW lpNetResource, LPCWSTR lpPassword, LPCWSTR lpUserName, DWORD dwFlags ); */ LPNETRESOURCEW nr; // 定义一个LPNETRESOURCEW结构体 memset(&nr, 0, sizeof(nr)); //结构体初始化 //DWORD dwFlags; DWORD res; if (argc != 4) { cout << "Usage: ipc.exe <remotename> <username> <password>" << endl; return -1; } //LPNETRESOURCEW结构体的四个必须填写 nr->dwType = RESOURCETYPE_ANY; //任意资源类型 nr->lpLocalName = NULL; // 建立与网络资源的连接,而不会重定向本地设备 nr->lpRemoteName = argv[1]; // 连接的网络资源 nr->lpProvider = NULL; // 操作系统尝试通过解析lpRemoteName成员指向的字符串来确定正确的提供程序 if ((res = WNetAddConnection2(nr, argv[3], argv[2], CONNECT_TEMPORARY) == NO_ERROR)) { cout << "ipc管道连接成功" << endl; } else { //if (res == ERROR_ACCESS_DENIED) { // cout << "ipc管道连接失败,原因:无权限" << endl; //} //else if (res == ERROR_BAD_USERNAME) { // cout << "ipc管道连接失败,原因:用户名无效" << endl; //} //else if (res == ERROR_INVALID_PASSWORD) { // cout << "ipc管道连接失败,原因:密码无效" << endl; //} wchar_t * pMsgBuf; FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS , NULL, res, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&pMsgBuf, 0, NULL); cout << pMsgBuf << endl; LocalFree(pMsgBuf); } }
来源:https://www.cnblogs.com/zpchcbd/p/12190083.html