可视C++:错误 C2664:'GetModuleFileNameW':无法将参数 2 从 'char [260]' 转换为'LPWCH'

Visual C++:error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'

本文关键字:char LPWCH 转换 C2664 错误 C++ GetModuleFileNameW 可视 参数      更新时间:2023-10-16

当我试图编译我的项目时,我遇到了一些无法解决的错误。。无论如何,这是其中一个代码:

public:
void Init(HMODULE hModule, string Filename)
{
    char szLoc[ MAX_PATH ];
    GetModuleFileName(hModule, szLoc, sizeof( szLoc ) );
    char* dwLetterAddress = strrchr( szLoc, '' );
    *( dwLetterAddress + 1 ) = 0;
    strcat( szLoc, Filename.c_str() );
    __OutStream.open( szLoc, ios::app);
}

错误是:

error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style  cast or function-style cast

谢谢你的帮助。。问候,Messer

Windows API的许多"函数"实际上是ANSI(A)或Unicode(W表示宽)版本函数的宏。根据您的项目设置,当您想要调用DoSomeFunction时,这些宏将是DoSomeFunctionADoSomeFunctionW。可移植的方法是使用TCHAR,因为它被定义为ANSI的char和Unicode的wchar_t

如果不想使用Unicode进行编译,可以将项目设置更改为Project Properties -> Configuration Properties -> General -> Character Set -> Use Multibyte Character Set

如果您确实想使用Unicode进行编译,那么应该在必要的函数名后面附加一个A(例如:GetModuleFileNameA)。