I have some old C++ file which I know used to compile. I have created a new install of Visual C++ version 6.
I am getting lots of compile errors with CStrings about not being able to convert to const char *
Here's an example.
CString dogs = "test";
writeoutfile(dogs, 1);
void Crender::writeoutfile(CString data, long data_size) {}
I get this error:
error C2664: 'void __thiscall Crender::writeoutfile(const char *,long)' : cannot convert parameter 1 from 'class CString' to 'const char *'
Is there some way I can get round this?
You have to get the raw pointer to the char field. This can be done with
CString::GetBuffer()
so you could call
writeoutfile(dogs.GetBuffer(), 1);
CString should convert to const char*. Is it a Unicode build? That's the only explanation I can think of.
GetBuffer() is for getting a writeable pointer to the data contained inside CString. Don't do that!
Related
So im writing a .dll for an injection, i ran into this problem and i have no clue abt how to fix tiError C2440 'initializing': cannot convert from 'const IMAGE_NT_HEADERS64 *' to 'const IMAGE_NT_HEADERS *' mod C:\Users\user\source\repos\mod\mod\Pattern.cpp 21
the code im using here:
const IMAGE_NT_HEADERS* ntHeader = reinterpret_cast<const IMAGE_NT_HEADERS64*>(reinterpret_cast<const uint8_t*>(dosHeader) + dosHeader->e_lfanew);
How can i fix this?
You are trying to assign a const IMAGE_NT_HEADERS64* to a const IMAGE_NT_HEADERS*. They are not the same type.
Either cast the adjusted pointer to const IMAGE_NT_HEADERS*, or declare ntHeader as const IMAGE_NT_HEADERS64*.
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.
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().
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 am trying to compile some code that was given to me that I'm told compiles fine. Perhaps on a different compiler. I am using VS2010 and I have the following line:
char *dot = strrchr(filename, '.');
This causes the compiler error:
"error C2440: 'initializing': cannot convert from 'const char *' to
'char *'
How come? And how do I fix it?
The error message is pretty clear. strrchr returns a const char*. So you need:
const char *dot = strrchr(filename, '.');
If you really need a char*, you can use strcpy for conversion.
C++ has saner versions of strchr and strrchr than C thanks to overloading, so say:
const char * dot = strrchr(filename, '.');
In C, which has no overloading, you only have a single function char * strrchar(const char *, const char *), and it's up to you to decide whether the result is constant or mutable, depending on which type of pointer to feed into the function. C has many such type-unsafe functions.