error C2440: 'initializing' : cannot convert from 'LPVOID' to 'UINT - c++

Im getting the following error while trying to convert code from C to C++:
error C2440: 'initializing' : cannot convert from 'LPVOID' to 'UINT (__cdecl *)(LPVOID,UINT,LPWSTR,UINT)'
Here's the piece of code causing problems:
UINT (*GetString)( LPVOID rsrc, UINT res, LPWSTR buf, UINT len )
= (LPVOID)0x4347e0;
How do I fix it?

You're trying to convince the compiler to treat 0x4347e0 (which is of type 'int') to be a pointer to a function taking 4 parameters. Casting the int to LPVOID isn't going to satisfy the compiler - you need to cast it to the right thing:
typedef UINT (*GetStringFnPtr)(LPVOID rsrc, UINT res, LPWSTR buf, UINT len );
GetStringFnPtr GetString = (GetStringFnPtr)0x4347e0;

Related

convert LPVOID IN CHAR ERROR C2440: =

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.

What is the correct function pointer for an unsigned WINAPI function?

I've a function declared in this way:
unsigned WINAPI searchSTR(void *j);
And I need a pointer to this function. My idea was:
unsigned (*pointerF) (void*);
pointerF = &searchSTR;
But there is an error:
"1 error C2440: '=' : cannot convert from 'unsigned int (__stdcall *)(void *)'
to 'unsigned int (__cdecl *)(void *)' ".
I tried other sintax, but nothing seems correct, he doesn't like the word WINAPI.
Can you suggest me the correct syntax? Maybe it is easy but I am blocked ! Thanks to all
The WINAPI macro expands to __stdcall, which is a different calling convention from the default __cdecl. You need to mark your function pointer with the calling convention to use:
unsigned (WINAPI *pointerF) (void*)
auto pointerF = &searchSTR;
Stop writing 1990's code. The compiler already knows the right type.

Error 3 error C2440: 'initializing' : cannot convert from 'void *' to 'Socket *'

I have problem when I implamante default constructor but I have error
Error 3 error C2440: 'initializing' : cannot convert from 'void *' to
'Socket *' Webserver.h 164 1 Project2_SocketLib
Socket.h
//SOCKET Accept(sockaddr* clientInfo,int* clientInfoSize)
SOCKET Accept()
{
static int size = sizeof(sockaddr);
return accept(this->hSocket, 0,0);
}
Webserver.h
Webserver(short port_to_listen,request_func rf,HWND Hwnd, WPARAM wParam, LPARAM lParam)
{
Socket in(port_to_listen,"INADDR_ANY", true, Hwnd, true);
//request_func = rf;
while (1) {
Socket* ptr_s =(void*) in.Accept();
unsigned ret;
_beginthreadex(0,0,Request, ptr_s,0,&ret);
}
}
Why you are explicitly type casting to void *
Socket* ptr_s =(void*) in.Accept();
Should be,
Socket sock = in.Accept();
Accept returns SOCKET. No need to convert it to void * or Socket *
I hope this will work
Socket* ptr_s =static_cast<Socket*>( in.Accept());
In C++, unlike in C, a void* can't be implicitly converted to a pointer to object. Instead, you should cast directly to the target pointer type:
Socket* ptr_s = reinterpret_cast<Socket*>(in.Accept());
Note: Prefer C++-style casts over C-style casts. They are more explicit in their intent and they are easier to search for.

cannot convert from 'FARPROC' to 'BOOL (__cdecl *)(LPMEMORYSTATUSEX)'

BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
kernel32, "GlobalMemoryStatusEx");
This is in a .cpp file. While compiling the above code I am getting the below error.
error C2440: 'initializing' : cannot convert from 'FARPROC' to 'BOOL (__cdecl *)(LPMEMORYSTATUSEX)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
I can't seem to figure out what should I cast the GetProcAddress function to.
Can someone please point me in the right direction?
Thanks
You need to cast it to the function pointer type. To simplfy, use a typedef for the function pointer type:
typedef BOOL (WINAPI *gmse_t)(LPMEMORYSTATUSEX);
gmse_t gmse = (gmse_t)GetProcAddress(kernel32, "GlobalMemoryStatusEx");
The GetProcAddress() reference page on MSDN provides example code.
You need to cast the general pointer that you get from GetProcAddress.
So, instead of the current
BOOL (WINAPI *gmse)(LPMEMORYSTATUSEX) = GetProcAddress(
kernel32, "GlobalMemoryStatusEx");
do
auto const gmse = reinterpret_cast<BOOL (WINAPI*)(LPMEMORYSTATUSEX)>(
GetProcAddress( kernel32, "GlobalMemoryStatusEx" )
);
In addition to adding that const I would use a more self-documenting name for that function pointer, like, well what about calling it GLobalMemoryStatusEx?
Another, yet an elegant solution(which is just a polished version of Cheers' and hmjd's answers combined) can be this:
typedef BOOL (WINAPI *gmse_t)(LPMEMORYSTATUSEX);
gmse_t gmse;
gmse = reinterpret_cast<gmse_t>(
GetProcAddress( kernel32, "GlobalMemoryStatusEx" )
);

error C2664: 'VariantCopy' : cannot convert parameter 2 from 'const VARIANT *' to 'VARIANTARG *'

While using VariantCopy method, I come across the following compile error:
error C2664: 'VariantCopy' : cannot convert parameter 2 from 'const VARIANT *' to 'VARIANTARG *'
Is there any way to solve this error? Do I need to include any preprocessor directive or library?
The problem was in different Windows SDKs the methods were different, that's why I had the problem.
The signature of VariantCopy used to be
HRESULT VariantCopy(
VARIANTARG FAR* pvargDest,
VARIANTARG FAR* pvargSrc
);
It is strange, but the input parameter is not const, you should be aware of that in your code.
MSDN documentation explains why. The function might modify pvargSrc
If pvargSrc is a VT_DISPATCH or VT_UNKNOWN, AddRef is called to
increment the object's reference count.
Update
In the most recent SDK, the 2nd parameter became const. However, I found this in MFC sources:
static HRESULT copy(_Out_ VARIANT* p1, _In_ const VARIANT* p2)
{
p1->vt = VT_EMPTY;
return VariantCopy(p1, const_cast<VARIANT*>(p2));
}