std::string WcharToChar(const wchar_t* wp, size_t m_encode = CP_ACP) { std::string str; int len = WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), NULL, 0, NULL, NULL); char *m_char = new char[len + 1]; WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), m_char, len, NULL, NULL); m_char[len] = '\0'; str = m_char; delete m_char; return str; } DWORD GetProcessIDFromName(char *strProcessName) { HANDLE snapshot; PROCESSENTRY32 processinfo; processinfo.dwSize = sizeof(processinfo); snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snapshot == NULL) { return 0; } BOOL status = Process32First(snapshot, &processinfo); while (status) { string str = WcharToChar(processinfo.szExeFile); if (_stricmp(strProcessName, str.c_str()) == 0) { return processinfo.th32ProcessID; } status = Process32Next(snapshot, &processinfo); } return 0; }
参考:https://www.cnblogs.com/pswzone/archive/2012/07/25/2609115.html