c++ - WCHAR 类型的参数与 const char* 类型的参数不兼容

标签 c++

DWORD Snapshots::getWindow(const char* windowName)
{

    initVariables::hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
    PROCESSENTRY32 pEntry; /// new variable named pEntry, that works around tagPROCESSENTRY32
    pEntry.dwSize = sizeof(PROCESSENTRY32); /// compiler will deny any other value that doesn't fit in tagPROCESSEENTRY32  /// also using tagPROCESENTRY32
    do
    {
        if (!strcmp(pEntry.szExeFile, windowName))  /// we compare the found exe to the exe's name we need.
        {

        }
        return 0;
    } while(Process32Next(initVariables::hSnapshot, &pEntry));  /// 1. arg = our handle 2. arg  = us referencing pEntry as our lpme


    return 0;
}

我正在使用多字节字符集,这个错误只发生在 DEBUG 模式下,在发布时它不会发生。

最佳答案

当项目字符集设置为 Unicode(显然是这种情况)时,此代码将无法编译,因为 strcmp() 采用 char* 输入,而不是 wchar_t* 输入。

由于代码使用的是 TCHAR 版本的 Win32 API,所以应该使用 _tcscmp() 来匹配:

#include <tchar.h>

DWORD Snapshots::getWindow(const char* windowName)
{
    initVariables::hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (initVariables::hSnapshot == INVALID_HANDLE_VALUE)
        return 0;

    PROCESSENTRY32 pEntry; /// new variable named pEntry, that works around tagPROCESSENTRY32
    pEntry.dwSize = sizeof(PROCESSENTRY32); /// compiler will deny any other value that doesn't fit in tagPROCESSEENTRY32 /// also using tagPROCESENTRY32

    if (Process32First(initVariables::hSnapshot, &pEntry))
    {
        do
        {
            if (!_tcscmp(pEntry.szExeFile, windowName)) /// we compare the found exe to the exe's name we need.
            {

            }
        }
        while (Process32Next(initVariables::hSnapshot, &pEntry)); /// 1. arg = our handle 2. arg = us referencing pEntry as our lpme
    }

    CloseHandle(initVariables::hSnapshot);

    return 0;
}

但是,由于 windowNamechar* 而不是 TCHAR* 因此不受项目​​字符集的影响,请使用基于 ANSI 的 API直接:

#include <tchar.h>

DWORD Snapshots::getWindow(const char* windowName)
{
    initVariables::hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (initVariables::hSnapshot == INVALID_HANDLE_VALUE)
        return 0;

    PROCESSENTRY32 pEntry; /// new variable named pEntry, that works around tagPROCESSENTRY32
    pEntry.dwSize = sizeof(PROCESSENTRY32); /// compiler will deny any other value that doesn't fit in tagPROCESSEENTRY32 /// also using tagPROCESENTRY32

    if (Process32FirstA(initVariables::hSnapshot, &pEntry))
    {
        do
        {
            if (!strcmp(pEntry.szExeFile, windowName)) /// we compare the found exe to the exe's name we need.
            {

            }
        }
        while (Process32NextA(initVariables::hSnapshot, &pEntry)); /// 1. arg = our handle 2. arg = us referencing pEntry as our lpme
    }

    CloseHandle(initVariables::hSnapshot);

    return 0;
}

关于c++ - WCHAR 类型的参数与 const char* 类型的参数不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59002920/

相关文章:

c++ - 采用 std::initializer_list 的构造函数优于其他构造函数

c++ - std vector 迭代器不支持地址运算?

c++ - 如何在 C++ 中获取 JoyStick Z 旋转消息

c++ - 未对齐的内存访问 : is it defined behavior or not?

C++ 在一个类中实例化一个类。正确的方法?

c++ - 有没有更简洁的方法来做到这一点? C++ 中频

c++ - 挑战 - Arduino 返回多个数组

c++ - 按位读取缓冲区

c++ - 根据继承用法,以下哪项是不允许的?

c++ - 将 QDialog 窗口限制为一个实例 [Qt]?