[英]'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCSTR'
in the vc++ I have a solution with two projects.在 vc++ 中,我有一个包含两个项目的解决方案。 project A has a dllLoader.h
and dllLoader.cpp
which loads a dll
with LoadLibrary
and I need to call its functions in Project B. So I did Copy and Paste the header and cpp file to Project B.项目 A 有一个dllLoader.h
和dllLoader.cpp
,它们使用LoadLibrary
加载一个dll
,我需要在项目 B 中调用它的函数。所以我将头文件和 cpp 文件复制并粘贴到项目 B。
Project A Main.cpp
------------------
#include "../Plugin/DllLoader.h"
#include "../Plugin/Types.h"
int main(){
std::string str("plugin.dll");
bool scuccessfulLoad = LoadDll(str);}
and here is the dllLoader in Project A (the mirror/copy in Project B get changed with changes here)这是项目 A 中的 dllLoader(项目 B 中的镜像/副本在此处随更改而更改)
bool LoadDll(std::string FileName)
{
std::wstring wFileName = std::wstring(FileName.begin(), FileName.end());
HMODULE dllHandle1 = LoadLibrary(wFileName.c_str());
if (dllHandle1 != NULL)
{ ****
return TRUE;
}
Building the project itself does not show any error and get successfully done, but when I build the Solution (which contains other projects) I get the error构建项目本身没有显示任何错误并成功完成,但是当我构建解决方案(包含其他项目)时,我收到错误
C2664 'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCSTR' C2664“HMODULE LoadLibraryA(LPCSTR)”:无法将参数 1 从“const _Elem *”转换为“LPCSTR”
Your LoadDll()
function takes a std::string
as input, converts it (the wrong way 1 ) to std::wstring
, and then passes that to LoadLibrary()
.您的LoadDll()
函数将std::string
作为输入,将其(错误的方式1 )转换为std::wstring
,然后将其传递给LoadLibrary()
。 However, LoadLibrary()
is not a real function, it is a preprocessor macro that expands to either LoadLibraryA()
or LoadLibraryW()
depending on whether your project is configured to map TCHAR
to char
for ANSI or wchar_t
for UNICODE:但是, LoadLibrary()
不是一个真正的函数,它是一个预处理器宏,它扩展为LoadLibraryA()
或LoadLibraryW()
具体取决于您的项目是否配置为将TCHAR
映射到 ANSI 的char
或 UNICODE 的wchar_t
:
WINBASEAPI
__out_opt
HMODULE
WINAPI
LoadLibraryA(
__in LPCSTR lpLibFileName
);
WINBASEAPI
__out_opt
HMODULE
WINAPI
LoadLibraryW(
__in LPCWSTR lpLibFileName
);
#ifdef UNICODE
#define LoadLibrary LoadLibraryW
#else
#define LoadLibrary LoadLibraryA
#endif // !UNICODE
In your situation, the project that is failing to compile is configured for ANSI, thus the compiler error because you are passing a const wchar_t*
to LoadLibraryA()
where a const char*
is expected instead.在您的情况下,无法编译的项目是为 ANSI 配置的,因此编译器错误,因为您将const wchar_t*
传递给LoadLibraryA()
,其中需要const char*
。
The simplest solution is to just get rid of the conversion altogether and call LoadLibraryA()
directly:最简单的解决方案是完全摆脱转换并直接调用LoadLibraryA()
:
bool LoadDll(std::string FileName)
{
HMODULE dllHandle1 = LoadLibraryA(FileName.c_str());
...
}
If you still want to convert the std::string
to std::wstring
1 , then you should call LoadLibraryW()
directly instead:如果您仍想将std::string
转换为std::wstring
1 ,则应直接调用LoadLibraryW()
:
bool LoadDll(std::string FileName)
{
std::wstring wFileName = ...;
HMODULE dllHandle1 = LoadLibraryW(wFileName.c_str());
...
}
This way, your code always matches your data and is not dependent on any particular project configuration.这样,您的代码始终与您的数据匹配,并且不依赖于任何特定的项目配置。
1: the correct way to convert a std::string
to a std::wstring
is to use a proper data conversion method, such as the Win32 MultiByteToWideChar()
function, C++11's std::wstring_convert
class, a 3rd party Unicode library, etc. Passing std::string
iterators to std::wstring
's constructor DOES NOT perform any conversions, it simply expands the char
values as-is to wchar_t
, thus any non-ASCII char
values > 0x7F will NOT be converted to Unicode correctly (UTF-16 is Windows's native encoding for wchar_t
strings). 1:一种转换的正确方法std::string
到std::wstring
是使用适当的数据转换方法,例如在Win32 MultiByteToWideChar()
函数,C ++ 11的std::wstring_convert
类,第三方的Unicode库等。将std::string
迭代器传递给std::wstring
的构造函数不执行任何转换,它只是将char
值按原样扩展为wchar_t
,因此任何非 ASCII char
值 > 0x7F 都不会被转换为Unicode 正确(UTF-16 是 Windows 对wchar_t
字符串的本机编码)。 Only the 7-bit ASCII characters (0x00 - 0x7F) are the same values in ASCII, ANSI codepages, Unicode UTF encodings, etc. Higher-valued characters require conversion.只有 7 位 ASCII 字符 (0x00 - 0x7F) 在 ASCII、ANSI 代码页、Unicode UTF 编码等中是相同的值。更高值的字符需要转换。
You pass a wide string to the function.您将一个宽字符串传递给该函数。 So the code is clearly intended to be compiled targeting UNICODE, so that the LoadLibrary
macro expands to LoadLibraryW
.因此,代码显然旨在针对 UNICODE 进行编译,以便LoadLibrary
宏扩展为LoadLibraryW
。 But the project in which the code fails does not target UNICODE.但是代码失败的项目不是针对UNICODE的。 Hence the macro here expands to LoadLibraryA
.因此这里的宏扩展为LoadLibraryA
。 And hence the compiler error because you are passing a wide string.因此编译器错误,因为您正在传递一个宽字符串。
The problem therefore is that you have inconsistent compiler settings across different projects.因此,问题是您在不同项目中的编译器设置不一致。 Review the project configuration for the failing project to make sure that consistent conditionals are defined.检查失败项目的项目配置以确保定义一致的条件。 That is, make sure that the required conditionals (presumably to enable UNICODE) are defined in all of the projects that contain this code.也就是说,确保在包含此代码的所有项目中都定义了所需的条件(大概是为了启用 UNICODE)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.