⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remoteshell.cpp

📁 MPICH是MPI的重要研究,提供了一系列的接口函数,为并行计算的实现提供了编程环境.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	}	return S_OK;}// Function name	: CRemoteShell::SendBreak// Description	    : // Return type		: STDMETHODIMP // Argument         : long *nError// Argument         : BSTR *bErrorMsgSTDMETHODIMP CRemoteShell::SendBreak(long *nError, BSTR *bErrorMsg){	if (m_hProcess != NULL)	{		if (!GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, m_dwProcessId))		//if (!GenerateConsoleCtrlEvent(CTRL_C_EVENT, m_dwProcessId))		{			*nError = GetLastError();			WCHAR error_msg[256];			Translate_Error(*nError, error_msg, L"SendBreak:GenerateConsoleCtrlEvent failed ");			SysReAllocString(bErrorMsg, error_msg);			LogWMsg(L"processID: %d, error: %d message: %s", m_dwProcessId, *nError, error_msg);		}	}	return S_OK;}// Function name	: CRemoteShell::CreateTempFile// Description	    : // Return type		: STDMETHODIMP // Argument         : BSTR *bFileName// Argument         : long *nError// Argument         : BSTR *bErrorMsgSTDMETHODIMP CRemoteShell::CreateTempFile(BSTR *bFileName, long *nError, BSTR *bErrorMsg){	WCHAR wTemp[MAX_PATH];	HRESULT hr;	HANDLE hImpersonatedToken, hUser;	WCHAR error_msg[256];	HKEY hKey;	hr = CoImpersonateClient();	if (FAILED(hr))		LogMsg(TEXT("CreateTempFile:CoImpersonateClient failed - creating temp file with process token"));	//if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &hImpersonatedToken))	if (!OpenThreadToken(GetCurrentThread(), MAXIMUM_ALLOWED, TRUE, &hImpersonatedToken))	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateTempFile:OpenThreadToken failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateTempFile:OpenThreadToken failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	CoRevertToSelf();	//if (!DuplicateTokenEx(hImpersonatedToken, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, &hUser))	if (!DuplicateTokenEx(hImpersonatedToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hUser))	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateTempFile:DuplicateTokenEx failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateTempFile:DuplicateTokenEx failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	if (RegOpenKeyEx(			HKEY_LOCAL_MACHINE, 			MPICHKEY,			0, KEY_READ, &hKey) != ERROR_SUCCESS)	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateTempFile:RegOpenKeyEx failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateTempFile:RegOpenKeyEx failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	// Read the temp directory	DWORD type, num_bytes = MAX_PATH*sizeof(WCHAR);	WCHAR wDir[MAX_PATH];	if (RegQueryValueExW(hKey, L"Temp", 0, &type, (BYTE *)wDir, &num_bytes) != ERROR_SUCCESS)	{		RegCloseKey(hKey);		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateTempFile:RegQueryValueExW failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateTempFile:RegQueryValueExW failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	RegCloseKey(hKey);	if (ImpersonateLoggedOnUser(hUser))	{		if (GetTempFileNameW(wDir, L"mpi", 0, wTemp) == 0)		{			*nError = GetLastError();			Translate_Error(*nError, wTemp, L"CreateTempFile:GetTempFileName failed ");			LogWMsg(wTemp);			SysReAllocString(bErrorMsg, wTemp);			return S_OK;		}				WCHAR wFullTemp[MAX_PATH], *namepart;		GetFullPathNameW(wTemp, MAX_PATH, wFullTemp, &namepart);			RevertToSelf();		SysReAllocString(bFileName, wFullTemp);	}	else	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateTempFile:ImpersonateLoggedOnUser failed ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateTempFile: ImpersonateLoggedOnUser failed: %d, %s\n", *nError, error_msg);	}	CloseHandle(hUser);	return S_OK;}// Function name	: CRemoteShell::GetPortFromFile// Description	    : // Return type		: STDMETHODIMP // Argument         : BSTR bFileName// Argument         : long *nPort// Argument         : long *nError// Argument         : BSTR *bErrorMsgSTDMETHODIMP CRemoteShell::GetPortFromFile(BSTR bFileName, long *nPort, long *nError, BSTR *bErrorMsg){	WCHAR error_msg[256];	HRESULT hr;	HANDLE hImpersonatedToken, hUser;	hr = CoImpersonateClient();	if (FAILED(hr))		LogMsg(TEXT("GetPortFromFile:CoImpersonateClient failed - reading temp file with process token"));	//if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &hImpersonatedToken))	if (!OpenThreadToken(GetCurrentThread(), MAXIMUM_ALLOWED, TRUE, &hImpersonatedToken))	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"GetPortFromFile:OpenThreadToken failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"GetPortFromFile:OpenThreadToken failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	CoRevertToSelf();	//if (!DuplicateTokenEx(hImpersonatedToken, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, &hUser))	if (!DuplicateTokenEx(hImpersonatedToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hUser))	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"GetPortFromFile:DuplicateTokenEx failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"GetPortFromFile:DuplicateTokenEx failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	if (ImpersonateLoggedOnUser(hUser))	{		HANDLE hFile = CreateFileW(bFileName, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);		if (hFile == INVALID_HANDLE_VALUE)		{			*nError = GetLastError();			Translate_Error(*nError, error_msg, L"GetPortFromFile:CreateFile failed ");			LogWMsg(error_msg);			LogWMsg(bFileName);			SysReAllocString(bErrorMsg, error_msg);			return S_OK;		}				DWORD num_read = 0;		TCHAR pBuffer[100] = _T("");		LPTSTR pChar = pBuffer;		clock_t cStart = clock();		while (true)		{			num_read = 0;			if (!ReadFile(hFile, pChar, 100, &num_read, NULL))			{				*nError = GetLastError();				Translate_Error(*nError, error_msg, L"GetPortFromFile:ReadFile failed ");				LogWMsg(error_msg);				SysReAllocString(bErrorMsg, error_msg);				CloseHandle(hFile);				DeleteFileW(bFileName);				return S_OK;			}			if (num_read == 0)			{				if (clock() - cStart > 10 * CLOCKS_PER_SEC)				{					DWORD dwExitCode;					if (GetExitCodeProcess(m_hProcess, &dwExitCode))					{						if (dwExitCode != STILL_ACTIVE)						{							LogMsg(TEXT("GetPortFromFile:Process has exited without writing the port number to a file. Exit code: %d"), dwExitCode);							swprintf(error_msg, L"GetPortFromFile:Process has exited, no port number in the file.\nProcess exit code: %d", dwExitCode);							*nError = dwExitCode;							SysReAllocString(bErrorMsg, error_msg);							CloseHandle(hFile);							DeleteFileW(bFileName);							return S_OK;						}					}					LogWMsg(L"GetPortFromFile:Wait for process 0 to write port to temporary file timed out: '%s'\n", bFileName);					swprintf(error_msg, L"GetPortFromFile:Wait for process 0 to write port to temporary file timed out: '%s'", bFileName);					*nError = dwExitCode;					SysReAllocString(bErrorMsg, error_msg);					CloseHandle(hFile);					DeleteFileW(bFileName);					return S_OK;				}				Sleep(100);			}			else			{				for (unsigned int i=0; i<num_read; i++)				{					if (*pChar == _T('\n'))						break;					pChar ++;				}				if (*pChar == _T('\n'))					break;			}		}		CloseHandle(hFile);		DeleteFileW(bFileName);				*nPort = _ttoi(pBuffer);				RevertToSelf();	}	else	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"GetPortFromFile:ImpersonateLoggedOnUser failed ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"GetPortFromFile: ImpersonateLoggedOnUser failed: %d, %s\n", *nError, error_msg);	}	CloseHandle(hUser);	return S_OK;}// Function name	: CRemoteShell::GrantAccessToDesktop// Description	    : // Return type		: STDMETHODIMP // Argument         : BSTR bAccount// Argument         : BSTR bPassword// Argument         : long *nError// Argument         : BSTR *bErrorMsgSTDMETHODIMP CRemoteShell::GrantAccessToDesktop(BSTR bAccount, BSTR bPassword, long *nError, BSTR *bErrorMsg){	WCHAR error_msg[255];	HANDLE hUser = NULL;	HANDLE hImpersonatedToken = NULL;	HRESULT hr = S_OK;	try{	if (wcslen(bAccount))	{		TCHAR tAccount[MAX_PATH], tPassword[MAX_PATH], tDomain[MAX_PATH], *psztDomain;		ParseAccountDomain(bAccount, tAccount, tDomain);		if (_tcslen(tDomain) < 1)			psztDomain = NULL;		else			psztDomain = tDomain;#ifdef UNICODE		wcscpy(tPassword, bPassword);#else		wcstombs(tPassword, bPassword, wcslen(bPassword)+1);#endif		if (!LogonUser(			tAccount,			psztDomain, 			tPassword,			LOGON32_LOGON_INTERACTIVE, 			LOGON32_PROVIDER_DEFAULT, 			&hUser))		{			*nError = GetLastError();			Translate_Error(*nError, error_msg, L"GrantAccessToDesktop:LogonUser failed: ");			SysReAllocString(bErrorMsg, error_msg);			LogWMsg(L"GrantAccessToDesktop:LogonUser failed: %d, %s\n", *nError, error_msg);			return S_OK;		}	}	else	{		// Impersonate the client and get a user token		hr = CoImpersonateClient();		if (FAILED(hr))			LogMsg(TEXT("GrantAccessToDesktop:CoImpersonateClient failed"));		if (!OpenThreadToken(GetCurrentThread(), MAXIMUM_ALLOWED, TRUE, &hImpersonatedToken))		{			*nError = GetLastError();			Translate_Error(*nError, error_msg, L"GrantAccessToDesktop:OpenThreadToken failed: ");			SysReAllocString(bErrorMsg, error_msg);			LogWMsg(L"GrantAccessToDesktop:OpenThreadToken failed: %d, %s\n", *nError, error_msg);			return S_OK;		}		CoRevertToSelf();		if (!DuplicateTokenEx(hImpersonatedToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hUser))		{			*nError = GetLastError();			Translate_Error(*nError, error_msg, L"GrantAccessToDesktop:DuplicateTokenEx failed: ");			SysReAllocString(bErrorMsg, error_msg);			LogWMsg(L"GrantAccessToDesktop:DuplicateTokenEx failed: %d, %s\n", *nError, error_msg);			return S_OK;		}		CloseHandle(hImpersonatedToken);		hImpersonatedToken = NULL;	}		m_bLaunchOnDesktop = MyGrantAccessToDesktop(hUser);	CloseHandle(hUser);	hUser = NULL;	}catch(...)	{		if (hUser != NULL)			CloseHandle(hUser);		if (hImpersonatedToken != NULL)			CloseHandle(hImpersonatedToken);		LogMsg(TEXT("Exception thrown in GrantAccessToDesktop"));	}	return S_OK;}// Function name	: CRemoteShell::CreateFileMapping// Description	    : // Return type		: STDMETHODIMP // Argument         : BSTR bName// Argument         : long *nError// Argument         : BSTR *bErrorMsgSTDMETHODIMP CRemoteShell::CreateFileMapping(BSTR bName, long *nError, BSTR *bErrorMsg){	WCHAR error_msg[255];	if (m_pMapping && m_hMapping)		UnmapViewOfFile(m_pMapping);	if (m_hMapping)		CloseHandle(m_hMapping);		SECURITY_ATTRIBUTES saAttr;	PSECURITY_DESCRIPTOR pSD;	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);	saAttr.bInheritHandle = FALSE;		// Initialize a security descriptor.	pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);	if (pSD == NULL) 	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateFileMapping:LocalAlloc failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateFileMapping:LocalAlloc failed: %d, %s\n", *nError, error_msg);		return S_OK;    }		if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) 	{		*nError = GetLastError();		LocalFree ((HLOCAL) pSD);		Translate_Error(*nError, error_msg, L"CreateFileMapping:InitializeSecurityDescriptor failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateFileMapping:InitializeSecurityDescriptor failed: %d, %s\n", *nError, error_msg);		return S_OK;	}		// Add a NULL disc. ACL to the security descriptor thus allowing everyone access.	if (!SetSecurityDescriptorDacl(pSD,        TRUE,			// specifying a disc. ACL        (PACL) NULL,        FALSE))			// not a default disc. ACL    {		*nError = GetLastError();		LocalFree ((HLOCAL) pSD);		Translate_Error(*nError, error_msg, L"CreateFileMapping:SetSecurityDescriptorDacl failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateFileMapping:SetSecurityDescriptorDacl failed: %d, %s\n", *nError, error_msg);		return S_OK;	}		saAttr.lpSecurityDescriptor = pSD;	// Create a mapping from the page file	m_hMapping = CreateFileMappingW(		INVALID_HANDLE_VALUE,		&saAttr,		PAGE_READWRITE,		0, sizeof(LONG),		bName);		// Free the memory for the security descriptor    LocalFree((HLOCAL) pSD);	if (m_hMapping == NULL)	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateFileMapping:CreateFileMappingW failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateFileMapping:CreateFileMappingW failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	if (GetLastError() == ERROR_ALREADY_EXISTS)	{		*nError = 1;		SysReAllocString(bErrorMsg, L"CreateFileMapping: failure, the file already exists");		return S_OK;	}			// Map the file and save the pointer to the base of the mapped file	m_pMapping = (LONG*)MapViewOfFile(		m_hMapping,		FILE_MAP_WRITE,		0,0,		sizeof(LONG));		if (m_pMapping == NULL)	{		*nError = GetLastError();		Translate_Error(*nError, error_msg, L"CreateFileMapping:MapViewOfFile failed: ");		SysReAllocString(bErrorMsg, error_msg);		LogWMsg(L"CreateFileMapping:MapViewOfFile failed: %d, %s\n", *nError, error_msg);		return S_OK;	}	// Initialize the data to zero	*m_pMapping = 0;	return S_OK;}// Function name	: CRemoteShell::GetPortFromMapping// Description	    : This function reads the port from the memory mapped file.//                    It can only be called once.// Return type		: STDMETHODIMP // Argument         : long *nPort// Argument         : long *nError// Argument         : BSTR *bErrorMsgSTDMETHODIMP CRemoteShell::GetPortFromMapping(long *nPort, long *nError, BSTR *bErrorMsg){	if (m_pMapping == NULL || m_hMapping == NULL)	{		*nError = 1;		SysReAllocString(bErrorMsg, L"GetPortFromMapping failed because the mapping hasn't been created yet.");		LogWMsg(L"GetPortFromMapping failed because the mapping hasn't been created yet.");		return S_OK;	}	// Wait for the launched process to write the port number	while (*m_pMapping == 0)		Sleep(200);	// Save the number	*nPort = *m_pMapping;	// Reset the memory region to zero, indicating that the data has been read.	*m_pMapping = 0;		UnmapViewOfFile(m_pMapping);	CloseHandle(m_hMapping);	m_pMapping = NULL;	m_hMapping = NULL;	return S_OK;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -