【问题标题】:arg of type "const char *" is incompatible with parameter of type "LPSTR" c++“const char *”类型的 arg 与“LPSTR”c++ 类型的参数不兼容
【发布时间】:2018-01-22 20:20:51
【问题描述】:

大家好,很抱歉问这个问题,但我还没有在网上找到答案。所以我在编译我的一个项目时不断收到错误““const char *”类型的arg与“LPSTR”类型的参数不兼容”。当我将它发送给我的朋友相同的代码时,他可以编译它的所有内容都在使用 Visual Studio 2017 的 Windows 10 上。这是源代码

    void RunProcess()
{
    runSetDebugPrivs();
    while (!FindProcessName("csgo.exe", &__gameProcess)) Sleep(12);
    while (!(getThreadByProcess(__gameProcess.th32ProcessID))) Sleep(12);
    __HandleProcess = OpenProcess(PROCESS_ALL_ACCESS, false, __gameProcess.th32ProcessID);
    while (__dwordClient == 0x0) __dwordClient = GetModuleNamePointer("client.dll", __gameProcess.th32ProcessID);
    while (__dwordEngine == 0x0) __dwordEngine = GetModuleNamePointer("engine.dll", __gameProcess.th32ProcessID);
    while (__dwordVGui == 0x0) __dwordVGui = GetModuleNamePointer("vguimatsurface.dll", __gameProcess.th32ProcessID);
    __HWNDCss = FindWindow(NULL, "Counter-Strike: Global Offensive");
}

};

【问题讨论】:

  • 我打赌你使用的是 MSVC 15.5 或更高版本,而你的朋友没有。
  • 错误在线 while (__dwordClient == 0x0) __dwordClient = GetModuleNamePointer("client.dll", __gameProcess.th32ProcessID); while (__dwordEngine == 0x0) __dwordEngine = GetModuleNamePointer("engine.dll", __gameProcess.th32ProcessID); while (__dwordVGui == 0x0) __dwordVGui = GetModuleNamePointer("vguimatsurface.dll", __gameProcess.th32ProcessID);所以 7 8 和 9
  • 进入你的项目选项,找到C/C++\Language下的“Conformance Mode”选项,将其改为“No”。如果你认真学习 C++,你应该得到一个good book,并确保在你未来的项目中这个选项是“是”。
  • 非常感谢,我正在制作一些电子书

标签: c++ visual-studio compilation


【解决方案1】:

我认为问题在于 GetModuleNamePointer 函数的第一个参数需要非 const char* 字符串,但是当你调用函数时你把 const char* 放在了那里。文字常量始终为const char*。造成这种情况的原因可能是其他风格的 C 语言在这种情况下无法区分这些。一个可能的解决方法是将该函数的声明更改为在第一个参数中接受 LPCSTR(又名const char*)而不是LPSTR(又名char*)。

你没有显示GetModuleNamePointer 函数,所以我只能猜测。如果它是我在 hack 论坛上找到的,它是这样声明的:

DWORD GetModuleNamePointer(LPSTR LPSTRModuleName, DWORD __DwordProcessId);

但它可以安全地与 const 一起使用,因此您可以简单地将其更改为

DWORD GetModuleNamePointer(LPCSTR LPSTRModuleName, DWORD __DwordProcessId);

在声明更改后,它将与您的编译器兼容。

正如@StoryTeller 在 cmets 中已经指出的那样,您还可以在编译器中禁用标准一致性。以下描述了它的作用以及它对您有帮助的原因:https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance

简而言之:当您的 LPSTR 程序不工作时,正确的行为可能令人惊讶。它适用于较旧的编译器,而新的编译器更严格地遵循标准。 :-)

【讨论】:

    【解决方案2】:

    答案来自this forum entry。转到项目“Properties -> C\C++ -> Language”并确保“Conformance mode”设置为“NO”

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 1970-01-01
      • 2019-12-16
      • 2016-05-19
      • 2014-07-03
      • 1970-01-01
      • 2021-08-06
      • 2020-11-19
      • 1970-01-01
      相关资源
      最近更新 更多