-1

伙计们,我有这个错误,我该如何解决?!但在此之前我说我在网上搜索并找到了我的问题的解决方案,例如:属性,然后导航到配置属性>常规。将字符集切换为“使用多字节字符集”。但我也有同样的错误?!

错误是:

Error   7   error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'const wchar_t [58]' to 'LPCSTR'   

Error   8   error C2664: 'int std::basic_string<_Elem,_Traits,_Ax>::compare(const std::basic_string<_Elem,_Traits,_Ax> &) const' : cannot convert parameter 1 from 'CHAR [260]' to 'const std::basic_string<_Elem,_Traits,_Ax> &'   

我的代码是:

#include "StdInc.h"
#include<fstream>
#include<sstream>
#include<string>
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<Windows.h>
#include<TlHelp32.h>

using std::ifstream;
using std::string;
using std::getline;
using std::ios;
using std::cerr;
using std::cout;
using std::endl;
using std::fixed;
using std::left;
using std::right;
using std::showpoint;
using std::cin;

class check {

public :


void check_seta () {

    ifstream cfgm2("finfin.cfg",ios::in);

    string cfgLine;

    while (getline(cfgm2,cfgLine)) {

        if (string::npos != cfgLine.find("seta mamamia")){

             if (cfgLine.at(19) == '0'){

                 MessageBox(NULL , L"lol not do that",NULL,MB_ICONERROR); 

                 std::wstring Processname(L"mod.exe");

                 DWORD ProcessId = FindProcessId(Processname);

                 HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE ,ProcessId);

                 TerminateProcess(pHandle,0);

                 CloseHandle(pHandle);
             }
             break;
        }

    }
}

DWORD FindProcessId(const std::wstring& processName)
    {
    PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);

    HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if ( processesSnapshot == INVALID_HANDLE_VALUE )
    return 0;

    Process32First(processesSnapshot, &processInfo);
    if ( !processName.compare(processInfo.szExeFile) )
    {
        CloseHandle(processesSnapshot);
        return processInfo.th32ProcessID;
    }

    while ( Process32Next(processesSnapshot, &processInfo) )
    {
        if ( !processName.compare(processInfo.szExeFile) )
        {
            CloseHandle(processesSnapshot);
            return processInfo.th32ProcessID;
        }
    }

    CloseHandle(processesSnapshot);
    return 0;
}
};
4

3 回答 3

4

在项目设置中,您没有选择 Unicode。因此,您拥有了 WIN32 API 的窄字符版本。MessageBox 映射到 MessageBoxA,进程信息结构中包含 LPCSTR。如果您更改设置,它们将变为 MessageBoxW 和 LPCWSTR,并且代码将编译(或者您在其他地方遇到其他错误)。

如果您打算使用 A 版本,请使用 char 代替 wchar_t,使用 string 代替 wstring,使用“xxx”代替 L“xxxx”。

于 2013-05-31T17:54:00.057 回答
1

您使用的是宽字符串文字 ( L"..."),因此您必须在您提到的项目设置中实际使用Unicode字符集。这将定义_UNICODE并使所有 Windows 函数名宏扩展为宽字符变体(后缀W)。

或者,如果您知道要使用宽字符串,最好直接调用宽字符版本。也就是说,对于其他特定于字符宽度的函数,使用MessageBoxW而不是等。MessageBox

于 2013-05-31T17:47:23.387 回答
0

在 Visual Studio 的项目属性中,转到 Configuration Settings > C/C++ > Preprocessor > Preprocessor Definitions 并确保 UNICODE;被定义为。这为我解决了类似的错误。

于 2013-06-19T14:27:37.113 回答