简体   繁体   English

C2664错误,C ++对我来说是陌生的

[英]C2664 error, C++ which is foreign to me

The error I get is: 我得到的错误是:

"DWORD GetModuleFileNameW(HMODULE,LPWSTR,DWORD)' : cannot convert parameter 2 from 'char *' to 'LPWSTR"

On this line 在这条线上

GetModuleFileName(NULL, &strL[0], MAX_PATH);

This the code 这个代码

    BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
{
    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
        {
            std::string strL;
            strL.resize(MAX_PATH);
            GetModuleFileName(NULL, &strL[0], MAX_PATH);

            DisableThreadLibraryCalls(hModule);

            if(strL.find("notepad.exe") != std::string::npos)
            {
                gl_hThisInstance = hModule;

                LoadOriginalDll();
            }

            break;
        }
    case DLL_PROCESS_DETACH:
        {
            ExitInstance();

            break;
        }
    }
    return TRUE;
}

From MSDN , MSDN

typedef wchar_t* LPWSTR, *PWSTR;

So it is expecting a wchar_t * (wchar_t is 2 bytes or more), but &std::string[0] is a char* (char is a byte). 因此,它期望一个wchar_t * (wchar_t为2个字节或更多),但是&std::string[0]是一个char* (char是一个字节)。 You need to use std::wstring instead: 您需要使用std::wstring代替:

std::wstring strL;

If you want your code to compile without using wide strings, refer to here: 如果要在不使用宽​​字符串的情况下编译代码,请参见此处:

How do I turn off Unicode in a VC++ project? 如何在VC ++项目中关闭Unicode?

Chances are if Unicode is enabled in VC++, there are defines like this: 如果在VC ++中启用了Unicode,则有如下定义:

#ifdef UNICODE
#define CreateFile  CreateFileW
#else
#define CreateFile  CreateFileA
#endif // !UNICODE

Fix: 固定:

Have you tried: Project Properties - General - Project Defaults - Character Set? 您是否尝试过:项目属性-常规-项目默认值-字符集?

See answers in this question for the differences between "Use Multi-Byte Character Set" and "Not Set" options: About the "Character set" option in visual studio 2010 有关“使用多​​字节字符集”和“未设置”选项之间的区别,请参见此问题的答案:关于Visual Studio 2010中的“字符集”选项

And from the link inside the quote: 从引号里面的链接:

It is a compatibility setting, intended for legacy code that was written for old versions of Windows that were not Unicode enabled. 这是一个兼容性设置,适用于为未启用Unicode的Windows的旧版本编写的旧代码。 Versions in the Windows 9x family, Windows ME was the last one. 在Windows 9x家族中,Windows ME是最后一个版本。 With "Not Set" or "Use Multi-Byte Character Set" selected, all Windows API functions that take a string as an argument are redefined to a little compatibility helper function that translates char* strings to wchar_t* strings, the API's native string type. 选中“未设置”或“使用多字节字符集”后,将所有以字符串为参数的Windows API函数重新定义为一个小的兼容性帮助程序函数,该函数将char *字符串转换为wchar_t *字符串(API的本机字符串类型) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM