Compare WCHAR String with Const Char in C?

2019-08-09 18:18发布

I have written a code in C which will use the Process32First() API to get information about the process. All the information is stored in the PROCESSENTRY32 structure defined here:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms684839%28v=vs.85%29.aspx

pe32 is the name of the PROCESSENTRY32 structure. process name will be: pe32.szExeFile

I can print it this way:

_tprintf(TEXT("Process name: %s\n"),pe32.szExeFile);

now, I want to compare the process name with a specific process like explorer.exe

this is what I am doing:

if(!wcscmp(pe32.szExeFile, _T("explorer.exe"))
{
 perform some action here;
}

It does not work.

In MS Visual Studio 2008, the data type for szExeFile member of the PROCESSENTRY32 structure is: WCHAR tagPROCESSENTRY32::szExeFile[260]

So, I think it is a Wide Character String?

and explorer.exe is a normal character string (const char *), pointer to an array of characters.

how can I compare szExeFile with a normal string?

I find these data types quite confusing and I hope to understand them better with this example.

Thanks.

标签: c wchar
1条回答
放我归山
2楼-- · 2019-08-09 18:31

Change your if to use wide string for "explorer.exe" as

if(!wcscmp(pe32.szExeFile, L"explorer.exe")

ie use L"explorer.exe" to compare with wide string.

You can also settings in your VC project to use UNICODE character set, through Project->Settings->Configuration Properties->General.

查看更多
登录 后发表回答