(C++)wchar_t 转 string / TCHAR转为char/判断是否进程运行/获得目标进程的入口

匿名 (未验证) 提交于 2019-12-02 23:32:01
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Giser_D/article/details/89787116

首先是wchar_t转string

 void Wchar_tToString(string& szDst, wchar_t* wchar) { 	wchar_t* wText = wchar; 	DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE); 	char* psText; 	psText = new char[dwNum]; 	WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE); 	szDst = psText; 	delete[]psText; }

TCHARתchar

 //将TCHAR转为char   void TcharToChar(const TCHAR* tchar, char* _char) { 	int iLength; 	//获取字节长度    	iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL); 	//将tchar值赋给_char     	WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL); }

//判断是否进程运行

 //判断是否进程运行 bool isProcessRun(string process_name) { 	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 	if (INVALID_HANDLE_VALUE == hSnapshot) { 		return NULL; 	} 	PROCESSENTRY32 pe = {sizeof(pe) }; 	for (BOOL ret = Process32First(hSnapshot, &pe); ret; ret = Process32Next(hSnapshot, &pe)) { 		wchar_t* process_str = pe.szExeFile; 		string current_process_name; 		Wchar_tToString(current_process_name, process_str); 		if (current_process_name == process_name) { 			g_ProcessRun = true; 			break; 		} 	} 	CloseHandle(hSnapshot); 	return g_ProcessRun; } 

//获得目标进程的入口

 //获得目标进程的入口 LPBYTE GetExeEntryPoint(char* filename) { 	PIMAGE_NT_HEADERS      pNTHeader; 	DWORD pEntryPoint; 	PLOADED_IMAGE       pImage; 	pImage = ImageLoad(filename, NULL); 	if (pImage == NULL) 	return NULL; 	pNTHeader = pImage->FileHeader;//获得文件头 	pEntryPoint = pNTHeader->OptionalHeader.AddressOfEntryPoint + pNTHeader->OptionalHeader.ImageBase; //???看不懂 	ImageUnload(pImage); 	return (LPBYTE)pEntryPoint; }

上述方法引用的头文件:

 #include <iostream> #include <stdarg.h> #include <Windows.h> #include <Psapi.h> #include <stdint.h> #include <tlhelp32.h> #include <imagehlp.h> #pragma comment(lib,"Psapi.lib")  //编译这个lib文件 #pragma comment(lib,"imagehlp.lib")  //编译这个lib文件

直接调用即可,希望C++封装越做越好,争取接近python

文章来源: https://blog.csdn.net/Giser_D/article/details/89787116
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!