关于WCHAR* 和char* 的转换问题,希望各位帮个忙指点一下,谢谢
在编译的时候遇到了这个难题,下面是我的代码:望高人多多指教..........
#include "stdafx.h"
#include<stdio.h>
#include<windows.h>
#include <tlhelp32.h>
#include <stdlib.h>
#include "defines.h"
int __cdecl Kill_process_main(){
char * proname={"chrome.exe"};
char p[260];
int i;
HANDLE hSnapshot;
hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //进程快照
PROCESSENTRY32 pe;
pe.dwSize=sizeof(PROCESSENTRY32);
std::string szDst;
/* char *orig
size_t origsize = strlen(orig) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L\" (wchar_t *)\");
wcout << wcstring << endl; */
/*size_t origsize =strlen(p)+1;
const size_t newsize =300;
size_t convertedChars = 0;
pe.szExeFile[newsize];
mbstowcs_s(&convertedChars, pe.szExeFile, origsize, p, _TRUNCATE);
//mbstowcs(&convertedChars, pe.szExeFile, origsize, p, _TRUNCATE);*/
//Process32First(hSnapshot,&pe);
for(i=0;i<=2;i++)
{
strcpy(p,proname[0]);
Process32First(hSnapshot,&pe);
int nCompare;
nCompare =WideCharToMultiByte(CP_ACP, NULL, pe.szExeFile, wcslen(pe.szExeFile), p, 260, 0, 0);
//nCompare =MultiByteToWideChar(CP_ACP, NULL, p, strlen(p) + 1, pe.szExeFile, 260);*/
do
{
if(strcmp( pe.szExeFile, p )
) //比较 如果相等就是等于 0
{
HANDLE hProcess;
hProcess=OpenProcess(PROCESS_ALL_ACCESS|PROCESS_TERMINATE,FALSE,pe.th32ProcessID);
if(hProcess)
{
TerminateProcess(hProcess,0);//关闭进程
}
}
}
while(Process32Next(hSnapshot,&pe));
}
CloseHandle(hSnapshot);
return 0;
}
这段代码在if(strcmp( pe.szExeFile, p ))会报错,说是不能WCHAR* 转换成char* ,上面我也用了WideCharToMultiByte函数,但是没有效果啊,并且试着用了mbstowcs_s函数,也是不起作用,szExeFile是定义在结构体里,如下:
typedef struct tagPROCESSENTRY32W
{
DWORD dwSize;
DWORD cntUsage;
DWORD th32ProcessID; // this process
ULONG_PTR th32DefaultHeapID;
DWORD th32ModuleID; // associated exe
DWORD cntThreads;
DWORD th32ParentProcessID; // this process's parent process
LONG pcPriClassBase; // Base priority of process's threads
DWORD dwFlags;
WCHAR szExeFile[MAX_PATH]; // Path
} PROCESSENTRY32W;
当我写成strcmp((const char*) pe.szExeFile, p )时,就不会报错,但是不能接着执行
{
HANDLE hProcess;
hProcess=OpenProcess(PROCESS_ALL_ACCESS|PROCESS_TERMINATE,FALSE,pe.th32ProcessID);
if(hProcess)
{
TerminateProcess(hProcess,0);//关闭进程
很是郁闷,这个谁会?帮个忙,谢谢。我是新手,指点的话请详细点啊 ;谢谢各位大神!!!!就剩40分了,全部奉上......
[解决办法]
strcmp( pe.szExeFile, p )
一个是char一个是wchar肯定不能比较。
[解决办法]
The ANSI code pages can be different on different computers, or can be changed for a single computer, leading to data corruption. For the most consistent results, applications should use Unicode, such as UTF-8 (code page 65001) or UTF-16, instead of a specific code page, unless legacy standards or data formats prevent the use of Unicode. If use of Unicode is not possible, applications should tag the data stream with the appropriate encoding name when protocols allow it. HTML, XML, and HTTP files allow tagging, but text files do not.
int WideCharToMultiByte(
UINT CodePage,
DWORD dwFlags,
LPCWSTR lpWideCharStr,
int cchWideChar,
LPSTR lpMultiByteStr,
int cbMultiByte,
LPCSTR lpDefaultChar,
LPBOOL lpUsedDefaultChar
);
[解决办法]
nCompare =WideCharToMultiByte(CP_ACP, NULL, pe.szExeFile, wcslen(pe.szExeFile), p, 260, 0, 0);
在这条语句里,你将pe.szExeFile宽字节字符串的内容转换为p多字节字符串的内容,前者为UNICODE编码,后者为GB_2312编码
strcmp( pe.szExeFile, p )
在这条语句里,你调用strcmp比较这两个字符串
但你真的知道strcmp函数的功能是什么吗?它是将两个char*指针指向的内存的字节逐一比较,直到遇到0结束符位置,不是你叫做“字符串”的东西它就能比较!
pe.szExeFile里面是什么数据?是宽字节字符串。“我是谁abc”这个字符串按照Unicode编码,在里面占用了14字节内存空间。p里面是什么数据?是多字节字符串。“我是谁abc”这个字符串按照GB_2312编码,在里面占用10字节内存空间。哪怕它们保存的是概念上的同一个字符串,因为编码不同,内存中的数据完全不同!所以才需要WideCharToMultiByte这样的函数在它们之间转换。
如果strcmp能够直接比较这两个“字符串”,那你为什么还用WideCharToMultiByte,直接strcpy不就行了吗?按字节拷贝的东西自然按字节比较,你用WideCharToMultiByte函数转换之后再按字节比较,能相等才真是见了鬼。
[解决办法]
strcmp( pe.szExeFile, p )wchar_t*和char*是不能比较的,因为他们的编码方式不一样需要转换
[解决办法]
wchar_t char2wchar(LPCSTR mutibyte)
{
int size=MultiByteToWideChar(CP_ACP,0,mutibyte,-1,NULL,0);
wchar_t *_mutibyte=new wchar_t[size+1];
MultiByteToWideChar(CP_ACP,0,mutibyte,-1,_mutibyte,size);
return _mutibyte;
}
strcmp( pe.szExeFile, p );
delete []_mutibyte;
_mutibyte=0;