bool CWfpNET::IPC_Session_Connect(void) // Establish NULL IPC$ Sessions { NETRESOURCE nr; DWORD nStatus = 0; TCHAR RemoteResource[23]; // UNC Name length (17) + \\IPC$\0 (6) = 23 _snprintf_s(RemoteResource, _countof(RemoteResource), _TRUNCATE, _T("%s\\IPC$"),node.szComputerM); nr.dwType = RESOURCETYPE_ANY; nr.lpLocalName = NULL; nr.lpProvider = NULL; nr.lpRemoteName = RemoteResource; // First attempt: Use currently logged in user nStatus = WNetAddConnection3(NULL, &nr, NULL, // password NULL, // username 0); if(nStatus == NO_ERROR) return(true); else { nStatus = WNetAddConnection3(NULL, &nr, (LPTSTR) _T(""), (LPTSTR) _T(""), 0); if(nStatus != NO_ERROR) { ErrorHandler("WNetAddConnection3",nStatus); return (false); } } return (true); }
// @pymethod |win32wnet|WNetAddConnection3|Creates a connection to a network resource. // @comm Accepts keyword arguments. // @pyseeapi WNetAddConnection3 static PyObject *PyWNetAddConnection3 (PyObject *self, PyObject *args, PyObject *kwargs) { LPTSTR Username = NULL; LPTSTR Password = NULL; DWORD ErrorNo; // holds the returned error number, if any DWORD flags = 0; NETRESOURCE *pNetResource; PyObject *obPassword=Py_None, *obUsername=Py_None, *ret=NULL; PyObject *obhwnd, *obnr; static char *keywords[] = {"HwndOwner", "NetResource","Password","UserName","Flags", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|OOk", keywords, &obhwnd, // @pyparm int|hwnd||Handle to a parent window. &obnr, // @pyparm <o PyNETRESOURCE>|NetResource||Describes the network resource for the connection. &obPassword, // @pyparm str|Password|None|The password to use. Use None for default credentials. &obUsername, // @pyparm str|UserName|None|The user name to connect as. Use None for default credentials. &flags)) // @pyparm int|Flags|0|Combination win32netcon.CONNECT_* flags return NULL; HWND hwnd; if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&hwnd)) return NULL; if (!PyWinObject_AsNETRESOURCE(obnr, &pNetResource, FALSE)) return NULL; if (!PyWinObject_AsTCHAR(obPassword, &Password, TRUE) || !PyWinObject_AsTCHAR(obUsername, &Username, TRUE)) goto done; Py_BEGIN_ALLOW_THREADS ErrorNo = WNetAddConnection3(hwnd, pNetResource, Password, Username, flags); Py_END_ALLOW_THREADS if (ErrorNo != NO_ERROR) ReturnNetError("WNetAddConnection3", ErrorNo); else{ Py_INCREF(Py_None); ret = Py_None; } done: PyWinObject_FreeTCHAR(Password); PyWinObject_FreeTCHAR(Username); return ret; };
// Stonee, 2001/12/21 // NetWork上のリソースに接続するためのダイアログを出現させる // NO_ERROR:成功 ERROR_CANCELLED:キャンセル それ以外:失敗 // プロジェクトの設定でリンクモジュールにMpr.libを追加のこと DWORD NetConnect ( const TCHAR strNetWorkPass[] ) { //char sPassWord[] = "\0"; //パスワード //char sUser[] = "\0"; //ユーザー名 DWORD dwRet; //戻り値 エラーコードはWINERROR.Hを参照 TCHAR sTemp[256]; TCHAR sDrive[] = _T(""); int i; if (_tcslen(strNetWorkPass) < 3) return ERROR_BAD_NET_NAME; //UNCではない。 if (strNetWorkPass[0] != _T('\\') && strNetWorkPass[1] != _T('\\')) return ERROR_BAD_NET_NAME; //UNCではない。 //3文字目から数えて最初の\の直前までを切り出す sTemp[0] = _T('\\'); sTemp[1] = _T('\\'); for (i = 2; strNetWorkPass[i] != _T('\0'); i++) { if (strNetWorkPass[i] == _T('\\')) break; sTemp[i] = strNetWorkPass[i]; } sTemp[i] = _T('\0'); //終端 //NETRESOURCE作成 NETRESOURCE nr; ZeroMemory( &nr, sizeof( nr ) ); nr.dwScope = RESOURCE_GLOBALNET; nr.dwType = RESOURCETYPE_DISK; nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE; nr.dwUsage = RESOURCEUSAGE_CONNECTABLE; nr.lpLocalName = sDrive; nr.lpRemoteName = sTemp; //ユーザー認証ダイアログを表示 dwRet = WNetAddConnection3(0, &nr, NULL, NULL, CONNECT_UPDATE_PROFILE | CONNECT_INTERACTIVE); return dwRet; }
bool WinReviveNetworkResource(uint16 * _wfileName) { bool result = false; HWND windowHandle = null; NETRESOURCE nr = { 0 }; nr.dwType = RESOURCETYPE_DISK; nr.lpRemoteName = _wfileName; if(_wfileName[0] != '\\' || _wfileName[1] == '\\') { uint16 volumePathName[MAX_LOCATION]; if(GetVolumePathName(_wfileName, volumePathName, MAX_LOCATION)) { uint16 remoteName[MAX_LOCATION]; DWORD size = MAX_LOCATION; volumePathName[wcslen(volumePathName)-1] = 0; if(WNetGetConnection(volumePathName, remoteName, &size) == ERROR_CONNECTION_UNAVAIL) { nr.lpRemoteName = remoteName; nr.lpLocalName = volumePathName; } else return false; } else return false; } EnumThreadWindows(GetCurrentThreadId(), EnumThreadWindowsProc, (LPARAM)&windowHandle); if(!windowHandle) { EnumWindows(EnumThreadWindowsProc, (LPARAM)&windowHandle); } if(WNetAddConnection3(windowHandle, &nr, null, null, CONNECT_INTERACTIVE|CONNECT_PROMPT) == NO_ERROR) result = true; return result; }
// @pymethod |win32wnet|WNetAddConnection2|Creates a connection to a network resource. The function can redirect // a local device to the network resource. // @comm This function also accepts backwards-compatible, positional-only // arguments of (dwType, lpLocalName, lpRemoteName[, lpProviderName, Username, Password, flags]) // @comm Accepts keyword arguments. // @pyseeapi WNetAddConnection2 static PyObject *PyWNetAddConnection2 (PyObject *self, PyObject *args, PyObject *kwargs) { LPTSTR Username = NULL; LPTSTR Password = NULL; // values used for b/w compat args. DWORD Type; PyObject *obLocalName, *obRemoteName, *obProviderName = Py_None; PyObject *obnr, *obPassword=Py_None, *obUsername=Py_None, *ret=NULL; DWORD ErrorNo; // holds the returned error number, if any DWORD flags = 0; NETRESOURCE *pNetResource; NETRESOURCE tempNetResource; memset(&tempNetResource, 0, sizeof(tempNetResource)); if (PyArg_ParseTuple(args,"iOO|OOOi",&Type,&obLocalName,&obRemoteName,&obProviderName,&obUsername,&obPassword, &flags)) { // the b/w compat args have been used - build the NETRESOURCE structure memset((void *)&tempNetResource, '\0', sizeof(NETRESOURCE)); tempNetResource.dwType = Type; if (!PyWinObject_AsTCHAR(obLocalName, &tempNetResource.lpLocalName, TRUE) || !PyWinObject_AsTCHAR(obRemoteName, &tempNetResource.lpRemoteName, FALSE) || !PyWinObject_AsTCHAR(obProviderName, &tempNetResource.lpProvider, TRUE)) goto done; pNetResource = &tempNetResource; } else { PyErr_Clear(); static char *keywords[] = {"NetResource","Password","UserName","Flags", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOk", keywords, &obnr, // @pyparm <o PyNETRESOURCE>|NetResource||Describes the network resource for the connection. &obPassword, // @pyparm str|Password|None|The password to use. Use None for default credentials. &obUsername, // @pyparm str|UserName|None|The user name to connect as. Use None for default credentials. &flags)) // @pyparm int|Flags|0|Combination win32netcon.CONNECT_* flags return NULL; if (!PyWinObject_AsNETRESOURCE(obnr, &pNetResource, FALSE)) return NULL; } if (!PyWinObject_AsTCHAR(obPassword, &Password, TRUE) || !PyWinObject_AsTCHAR(obUsername, &Username, TRUE)) goto done; Py_BEGIN_ALLOW_THREADS #ifdef _WIN32_WCE_ // Windows CE only has the #3 version...use NULL for HWND to simulate #2 ErrorNo = WNetAddConnection3(NULL, pNetResource, Password, Username, flags); #else ErrorNo = WNetAddConnection2(pNetResource, Password, Username, flags); #endif Py_END_ALLOW_THREADS if (ErrorNo != NO_ERROR) ReturnNetError("WNetAddConnection2", ErrorNo); else{ Py_INCREF(Py_None); ret = Py_None; } done: PyWinObject_FreeTCHAR(Password); PyWinObject_FreeTCHAR(Username); PyWinObject_FreeTCHAR(tempNetResource.lpLocalName); PyWinObject_FreeTCHAR(tempNetResource.lpRemoteName); PyWinObject_FreeTCHAR(tempNetResource.lpProvider); return ret; };
int main(int argc, char ** argv) { NETRESOURCE nr = {0}; DWORD dwRetVal = 0; int c = 0; char host[32] = {0}; LOGINFO LogInfo = {0}; while ((c = getopt(argc, argv, "h:u:p:e:")) != -1) { switch (c) { // 目标ip case 'h': strncpy(host, optarg, 32); break; // 用户名 case 'u': strncpy(LogInfo.szUserName, optarg, NAME_LEN); break; // 密码 case 'p': strncpy(LogInfo.szPassword, optarg, PASSWD_LEN); break; // cmd case 'e': strncpy(LogInfo.szExcuteCmd, optarg, CMD_LEN); break; default: break; } } char szRemoteName[MAX_PATH] = {0}; if (host[0]) sprintf(szRemoteName, "\\\\%s\\%s", host, ADMIN); else usage(); if (!LogInfo.szUserName[0] || !LogInfo.szPassword[0]) usage(); nr.dwType = RESOURCETYPE_ANY; nr.lpLocalName = NULL; nr.lpRemoteName = szRemoteName; nr.lpProvider = NULL; dwRetVal = WNetAddConnection3(NULL, &nr, LogInfo.szPassword, LogInfo.szUserName, 0); if (dwRetVal != NO_ERROR) { int a = GetLastError(); return -1; } InstallRemoteService(host); Client(host, &LogInfo); WNetCancelConnection(nr.lpRemoteName, TRUE); return 0; }