convert LPVOID IN CHAR ERROR C2440: = - c++

How do I convert a LPVOID to char ?
char * pCacheMap;
pCacheMap = MapViewOfFile(hCacheMapping,FILE_MAP_READ|FILE_MAP_WRITE,0,0,0);
Error:
3 IntelliSense: a value of type "LPVOID" cannot be assigned to an entity of type "char *"
Error 1 error C2440: '=' : cannot convert from 'LPVOID' to 'char *'

Use a cast. Since LPVOID is a synonym for void * it is legal to cast it to any other pointer type so:
pCacheMap = static_cast<char *>(MapViewOfFile(...));
will work.

Related

I need to read the memory by the string, but when I try it gives the following error

argument of type "char *" is incompatible with parameter of type "LPCWSTR"
'int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)': unable to convert an argument 2 of 'char [255]' to 'LPCWSTR'
type conversion': conversion from 'unsigned int' to larger size 'LPVOID'
The code I'm using is this
char buffer[255] = { 0 };
unsigned addr = 0xb50798;
if (ReadProcessMemory(hproc, (LPVOID)addr, &buffer, sizeof(buffer), NULL))
{
MessageBoxW(hwnd, buffer, NULL, NULL);

extern "C" 'initializing': cannot convert from 'const char [1]' to 'char *'

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.

'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'

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().

C++03: _ui64toa_s: error C2446: ':' : no conversion from 'errno_t' to 'const char *'

I have the following line of code:
(total_time== 0xAAAABBBB) ? ("") : _ui64toa_s(total_time, myArray, 1024, 10);
As the title suggests, I receive this error:
error C2446: ':' : no conversion from 'errno_t' to 'const char *'
I am unsure how to convert errno_t to const char *.
I tried to solve it by writing
const char *n = _ui64toa_s(total_time, myArray, 1024, 10);
only to see this:
error C2440: 'initializing' : cannot convert from 'errno_t' to 'const char *'
_ui64toa_s does not return a string like _ui64toa does.
Instead it returns an errno_t which is defined as int.
errno_t e = _ui64toa_s(total_time, myArray, 1024, 10);
But since you're using C++ I recommend you use std::to_string instead.
std::to_string(total_time).c_str();
You might also want to take a look at this question.

Concatenate CString and Long in VC++?

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"