【问题标题】:strcmp error 'WCHAR [260]' to 'const char *' [duplicate]strcmp 错误“WCHAR [260]”到“const char *”[重复]
【发布时间】:2019-09-01 14:32:24
【问题描述】:

我在发布模式下编译此代码时遇到了问题(它在调试模式下完美运行)

#include "MemoryManagement.h"
#include "Windows.h"
#include "Colors.h"
#include <iostream>
#include <TlHelp32.h>
using namespace std;

MemoryManagement::MemoryManagement()
{
    handle = NULL;
}

MemoryManagement::~MemoryManagement()
{
    CloseHandle(handle);
}

DWORD MemoryManagement::getProcess(const char* proc)
{
    HANDLE hProcessId = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    DWORD process = 0;
    PROCESSENTRY32 pEntry;
    pEntry.dwSize = sizeof(pEntry);

    SetConsoleColor(colors::blue_bright);
    cout << "Searching game..." << endl;

    while (!process)
    {
        do {
            if (!strcmp(pEntry.szExeFile, proc))
            {
                process = pEntry.th32ProcessID;
                CloseHandle(hProcessId);
                handle = OpenProcess(PROCESS_ALL_ACCESS, false, process);
            }
        } while (Process32Next(hProcessId, &pEntry));
    }

    SetConsoleColor(colors::green_bright);
    cout << "Game found!" << endl << endl;

    return process;
}

uintptr_t MemoryManagement::getModule(DWORD procId, const char* modName)
{
    HANDLE hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
    MODULEENTRY32 mEntry;
    mEntry.dwSize = sizeof(mEntry);

    do
    {
        if (!strcmp(mEntry.szModule, modName))
        {
            CloseHandle(hModule);
            return (DWORD)mEntry.hModule;
        }
    } while (Module32Next(hModule, &mEntry));
    return 0;
}

DWORD MemoryManagement::getAddress(DWORD addr, std::vector<DWORD> vect)
{
    for (unsigned int i = 0; i < vect.size(); i++)
    {
        ReadProcessMemory(handle, (BYTE*)addr, &addr, sizeof(addr), 0);
        addr += vect[i];
    }
    return addr;
}

错误输出是这个:

1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(31): error C2664: 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'WCHAR [260]' to 'const char *'
1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(31): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(54): error C2664: 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'WCHAR [256]' to 'const char *'
1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(54): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Done building project "CS.vcxproj" -- FAILED.

我发现问题出在 strcmp 上,我给出了错误的数据类型...

我四处寻找,没有找到符合我情况的修复方法...

为什么它在 Debug 模式下工作,但在 Release 模式下却不行?

每次打开程序时我都必须比较两个可以更改的字符串,所以我不知道如何将参数指定为 const

【问题讨论】:

  • 关于“无法将参数 1 从 'WCHAR [260]' 转换为 'const char *'”有什么不清楚的地方?
  • 不清楚我该如何处理它,如果我知道我没有问这个问题,以及为什么它在调试模式而不是在发布模式下工作
  • 您通过修复代码来处理它,以便第一个参数是const char *,或者可以隐式转换为它的东西。如何修复代码完全取决于您,但这就是编译错误的原因。
  • 是的,我可以读取错误输出,我已经说过错误与数据类型有关,但这并没有回答我的问题
  • @SamVarshavchik 不,那将是错误的修复。正确的方法是使用正确的函数来完成这项工作,而不是破坏您的数据输入来破解类型不匹配的问题。请停止将答案放在 cmets 部分,错误时不能拒绝。

标签: c++


【解决方案1】:

这在调试模式下工作没有意义;这一定是一个错误的观察。

WCHARwchar_t(或unsigned short,如果需要)(ref) 的 Microsoft 别名。

你有这些东西的数组。

所述数组永远不会与const char* 兼容,因为wchar_tchar 是两个不同的东西。因此,您不能将数组传递给需要const char* 的函数,例如strcmp

C(以及扩展的 C++)还提供了strcmp 的宽字符版本,您可以使用:wcscmp

【讨论】:

  • 非常感谢,这就是我要找的东西
  • wcscmp 是标准 C.
  • @PeteBecker 原来如此!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 2020-09-08
  • 2014-07-13
相关资源
最近更新 更多