I'm new to MFC and I don't know what to do with this error.
ERROR
error C2664: 'void ATL::CStringT::Format(const
wchar_t *,...)' : cannot convert parameter 1 from 'const char [6]' to
'const wchar_t *'
heres the line:
m_Echo1.Format("%d %",state.dwMemoryLoad);
By default a Windows app is set to use 16-bit characters, not 8-bit characters. Change your quoted string to L"%d %" to specify a string of 16-bit characters.
There are 2 distinct errors with the line of code you posted:
The format string contains an illegal format specifier (trailing %). If you want a format string to contain a literal percent-sign it has to be escaped using %%.
You are using a string literal that does not match the required encoding, i.e. a mismatch between ANSI and UNICODE character encoding. If m_Echo1 is of type CString the parameter has to be wrapped inside a _T or TEXT macro: _T( "%d %%" ). If m_Echo1 is of type CStringW the parameter must be passed as a UNICODE string literal by prepending it with L: L"%d %%".
Note: The error message you posted does not match the line of code. The error message refers to const char [6] while the string literal in your code is of type const char [5]. Make sure that error messages and code match up.
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.
error C2664: 'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)' : cannot convert parameter 4 from 'CHAR [260]' to 'const wchar_t *' 1>
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
What does this error mean?
As my function is:
BOOL DependentDLLDisplay()
{
char arr[200];
if(!Module32First(hProcessSnap,&me32))
{
cout<<" ERROR : Failed to Get DLL Information"<<endl;
CloseHandle(hProcessSnap);
return FALSE;
}
cout<<endl<<"DEPENDENT DLL OF THIS PROCESS :"<<endl;
do
{
wcstombs_s(NULL,arr,200,me32.szModule,200);
cout<<arr<<endl;
}while(Module32Next(hProcessSnap,&me32));
CloseHandle(hProcessSnap);
return TRUE;
}
Your object me32 is of the type MODULEENTRY32 as defined here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225.aspx
The szModule field that you pass as the 4th parameter to 'wcstombs_s` is defined as:
TCHAR szModule[MAX_MODULE_NAME32 + 1];
In the Windows API, TCHAR is defined as char in MBCS encoding, and wchar in UNICODE encoding.
The error you are seeing is indicating that you are including the MBCS version of the Windows library, thus MODULEENTRY32 is actually MODULEENTRY32A and me32.szModule is a char[], but are then trying to treat me32.szModule as if it were a wide wchar_t[] string when it is in fact an Ansi char[] string.
You can either switch to the UNICODE libraries by changing your project settings, or using a normal char string copy to obtain the value of that field.
Or, as Remy stated:
Or, you can explicitly use Module32FirstW()/Module32NextW(),
MODULEENTRY32W, std::wcout, etc, or explicitly use
Module32FirstA()/Module32NextA(), MODULEENTRY32A, etc. Either way, you
do not have to change the project settings. Don't use TCHAR-based APIs
anymore. In this case, since the code wants to end up with a char[]
string, it makes sense to use Module32FirstA()/Module32NextA() and
just remove wcstombs_s() altogether.
One last note: You should probably expand your local variable to be the same size of szModule rather than potentially truncate the contents.
When I use this code
if (GetKeyNameText(Key << 16, NameBuffer, 127))
{
KeyName = NameBuffer;
GoodKeyName = true;
}
I get the following error
C2664 'int GetKeyNameTextW(LONG,LPWSTR,int)': cannot convert argument
2 from 'char [128]' to 'LPWSTR'
The NameBuffer says this:
Error: argument of type "char*" is incompatible with parameter of type
"LPWSTR"
Any tips?
You have UNICODE defined, which means all your functions and TCHAR and LPTSTR are defaulting to wide characters (wchar_t).
That means you can't use a narrow-character string (using char) without special care.
There is an easy solution, and that's to explicitly call the narrow-character version of the function: GetKeyNameTextA.
Another solution is to stop using char and change to TCHAR and related types, and use the T macro for string literals.
You might want to read more about UNICODE in the Windows API.
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.
I am trying to compile the following code in my test application on windows in visual studio for C++:
const wchar_t* chinese = "好久不见";
But I get the following error:
error C2440: 'initializing' : cannot convert from 'const char [5]' to 'const wchar_t *
I am compiling with unicode, so I am confused about this. The error goes away if I cast the literal like this:
const wchar_t* chinese = (wchar_t*)"好久不见";
I am not sure that is safe nor do I really want to do that so how can I fix this.
Thank you!
You want a wide string literal, so prefix the string literal with L:
const wchar_t* chinese = L"好久不见";
Like GMan said, prefix the string with an 'L'. The L identifies the string as a wide char type (wchar_t).
const wchar_t* chinese = L"好久不见";