如何将WCHAR [MAX_PATH]转换为const char *? [英] How convert WCHAR[MAX_PATH] to const char*?
问题描述
我需要将我的应用程序编译为使用Unicode字符设置,我有一个方法,用于通过您的名字查找进程的ID如果当前进程(循环中)是
我的目标,那么必须进行比较。
在我的方法中,参数procName必须是字符串,所以我尝试转换
WCHAR [MAX_PATH] (
PROCESSENTRY32的 szExeFile 类型 tlhelp32.h 上的结构 const char * (
_stricmp()需要) MultiByteToWideChar 但没有成功。
可以帮助我吗?
I need compile my application as Use Unicode Character Set and i have a method that is used to find id of a process by your name where is necessary make a comparation for know if current process (in loop) is
the my target.
On my method the parameter procName must be string, so i tried convert the
WCHAR[MAX_PATH] ( type of szExeFile of
PROCESSENTRY32 structure on tlhelp32.h) to const char* (required by
_stricmp()) using MultiByteToWideChar but without success.
Could help me please?
DWORD getPid(string procName){
HANDLE hsnap;
const char *proc = "";
PROCESSENTRY32 pt;
hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pt.dwSize = sizeof(PROCESSENTRY32);
do{
MultiByteToWideChar(CP_ACP, 0, proc, -1, pt.szExeFile, MAX_PATH);
if(_stricmp(proc, procName.c_str())==0){
cout << "found process!";
return pt.th32ProcessID;
}
} while(Process32Next(hsnap, &pt));
CloseHandle(hsnap);
return 0;
}
推荐答案
Unicode变体中的Process32Next已经返回Unicode(宽)进程名称,因此无需转换它。
Process32Next in Unicode variant already returns Unicode (wide) process name, so no need to convert it.
与Process32First相同(您的代码不会调用顺便说一下)
Same with Process32First (your code does not call it, by the way)
- pa
这篇关于如何将WCHAR [MAX_PATH]转换为const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!