簡體   English   中英

WCHAR 類型的參數與 const char* 類型的參數不兼容

[英]Argument of type WCHAR is incomaptible with parameter of type const char*

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;
}

我正在使用多字節字符集,此錯誤僅在調試模式下發生,在發布時不會發生。

當項目字符集設置為 Unicode(顯然是這種情況)時,此代碼將無法編譯,因為strcmp()接受char*輸入,而不是wchar_t*輸入。

由於代碼使用的是 Win32 API 的TCHAR版本,因此它應該使用_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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM