首先是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_HEADERSpNTHeader;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