I'm still rather new to C++ but I've ran into a problem I can't solve, this is my error message:
'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'
And this the line that is throwing the error:
ModuleHandle = (DWORD)GetModuleHandle(moduleName.c_str());
You are passing a char * to something that needs a wchar_t *. You'll have to either convert your stringtype to wchar_t *, for example using the MultiByteToWideChar function (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072%28v=vs.85%29.aspx), or you can use the non-wide version of GetModuleHandle by calling GetModuleHandleA() instead of GetModuleHandleW().
Related
This question already has answers here:
Cannot convert parameter from 'const char[20]' to 'LPCWSTR'
(3 answers)
'CreateDirectoryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' in OpenCV 2.4.5 and VS 2010
(3 answers)
cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'
(4 answers)
'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCSTR'
(2 answers)
Cannot convert const char[16] to LPCWSTR [closed]
(1 answer)
Closed 1 year ago.
uintptr_t gameModule = (uintptr_t)GetModuleHandle("client.dll");
Severity Code Description Project File Line Suppression State
Error C2664 'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char [11]' to 'LPCWSTR'
uintptr_t gameModule = (uintptr_t)GetModuleHandle("client.dll");
HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from
'const char [11]' to 'LPCWSTR'
"client.dll" is a char string (const char [11]).
According to the Windows API TCHAR model, GetModuleHandle is a preprocessor macro which is expanded to GetModuleHandleW in Unicode builds (the default build mode for Visual Studio C++ projects since VS 2005).
GetModuleHandleW requires a LPCWSTR string parameter, i.e. a const wchar_t*, which is a wchar-t string.
So, you have a mismatch in your GetModuleHandle call, as you passed a char string, but GetModuleHandle (which is expanded to GetModuleHandleW) requires a wchar_t string (LPCWSTR).
You can fix this error passing L"client.dll" instead of "client.dll"; in fact, L"client.dll" (note the L prefix) is a wchar_t string:
// Pass L"client.dll" instead of "client.dll"
uintptr_t gameModule = (uintptr_t)GetModuleHandle(L"client.dll");
Another option would be explicitly invoking the "ANSI" function GetModuleHandleA:
// Explicitly call GetModuleHandleA
uintptr_t gameModule = (uintptr_t)GetModuleHandleA("client.dll");
but I would stick with Unicode APIs.
You could even totally embrace the TCHAR model, and decorate your string literal with _T() or TEXT(), e.g.:
uintptr_t gameModule = (uintptr_t)GetModuleHandle(_T("client.dll"));
That would work in both ANSI and UNICODE builds.
I have strange error when compiling open source lib
I using visual studio 2019
and I created a solution to the lib cmake -G "Visual Studio 16 2019" ../"
but I keep getting erros when compiling :
starting with this line :
CFUNC char *optarg = "";
C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(16,24): error C2440: 'initializing': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(16,20): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(30,27): error C2440: 'initializing': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(30,21): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(38,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(38,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(45,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(45,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(51,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(51,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(85,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(85,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(94,15): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(94,11): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
"" is a constant string. You can't just assign that to a non-constant char *, because then it could be mutated through the pointer.
According to the source code of libunistd, the implementation was taken from NetBSD, but a quick check¹ shows that NetBSD doesn't initialize optarg at all:
char *optarg; /* argument associated with option */
You could follow the same approach.
You could also turn off this error by passing /Zc:strictStrings- (note the hyphen) to your compiler. As long as the constant string isn't written to, there should be little harm in that.
You might want to report this as a bug to the library author.
¹ Not sure I'm looking at the latest version. It's from 2014.
It's not strange. The type of "" (or any string literal) is const char[] and the type of optarg is char*. So if the assignment was allowed you would be able to modify a string literal via the pointer optarg. Clearly that's not desireable.
In an older version of C++, this assignment was allowed as a special case, simply because there was too much old C code that did this. But this is no longer the case.
I need to pass a QChar to a function that expects a wchar_t:
void myFunc(wchar_t wch);
Trying to just pass the QChar fails with an error:
error: C2664: 'myFunc' : cannot convert parameter 1 from 'QChar' to 'wchar_t'
Found the answer even while I was asking the question, I needed to use QChar::unicode().
wchar_t wch = qch.unicode();
I have to concatenate two CString variables and two long variables in one
CString. I found one Format function that I have used like this:
CString str = "Some Data";
str.Format("%s%d", str, 123);
But it is giving errors. Here is the error log:
\AudWinSockXCtrl.cpp(410) : error C2440: 'initializing' : cannot convert from 'const char [10]' to 'ATL::CStringT'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
Constructor for class 'ATL::CStringT' is declared 'explicit'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
.\AudWinSockXCtrl.cpp(411) : error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' :
cannot convert parameter 1 from 'const char [5]' to 'const wchar_t *'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or
function-style cast
.\AudWinSockXCtrl.cpp(414) : error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' :
cannot convert parameter 1 from 'const char [4]' to 'const wchar_t *'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or
function-style cast
Is there any function like toString() like we use in Java?
CString str = _T("Some Data");
str.Format(_T("%s%d"), str, 123);
Read up on _T and <tchar.h> here: Generic-Text Mappings in Tchar.h .
sprintf ( OutputBuffer, "%s%d", str, 123 ) ;
Use CStringA for ANSI Version.
Use CStringW for Unicode Version.
Use CString for TCHAR Version.
To force CString to be evaluated as CStringA::
Goto, Project->Properties->Configuration Properties->General.
On right hand side, you will get "Character Set" Row, change that to "Not set"
SimpleIni Documentation says wchar_t is supported but I don't understand how to use it. This is what I tried:
CSimpleIniCaseW ini;
ini.LoadFile("myapp.ini");
std::wstring test(ini.GetValue("testsection", "testkey", ""));
error C2664:
'CSimpleIniTempl::GetValue'
: cannot convert parameter 1 from
'const char [12]' to 'const wchar_t *'
You need to specify your strings as L"testsection" << Note the L.