Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error 1 error C2664: 'PostMessageW' : cannot convert parameter 4 from 'CHAR [32][32]' to 'LPARAM' d:\him\1dvrtestfront end\gdsapi.cpp 431
how to convert char[][] to LPARAM
Posted
Comments
Sandeep Mewara 5-Apr-11 6:28am    
Incomplete!

No idea on what were you doing, etc.
Albert Holguin 5-Apr-11 10:05am    
actually that tells you everything you need to know to help him

Just do a cast. But be careful about PostMessage function. Since it is asynchronous, you must make sure that the pointer you send will remain valid after you exit from the calling function.

C++
void function()
{
    char aFewStrings[32][32];
    ...
    //don't do that!!
    //because aFewStrings will get out of scope and will be destroyed
    //so your message handler will probably get a bad pointer...
    PostMessage(..., (LPARAM)(void*)aFewString);
}


Use SendMessage if you want a synchronous call. Or just make sure the pointer you send remains valid (allocate it with new, or keep it as a member variable of your class).
 
Share this answer
 
Comments
Albert Holguin 5-Apr-11 10:03am    
when you send a two dimensional array this way, make sure that you realize that you'll lose the dimensions of the array, so your application must know the dimensions of the array on the receiving side to recast to an array with the same exact dimensions. Also, if using PostMessage, allocate the memory on the heap and deallocate it on the receiving end to avoid memory leaks.
Olivier Levrey 5-Apr-11 10:09am    
I agree. I forgot to mention that about dimensions: good point for you Albert.
Albert Holguin 5-Apr-11 10:13am    
had to point it out because i can sense a follow on question... "how do i figure out the dimensions of my 2D array after PostMessage?"... lol
Olivier Levrey 5-Apr-11 10:18am    
:D
Albert Holguin 5-Apr-11 10:03am    
BTW, my 5 oliver!
Also, using Visual Studio, it auto-fills in that "W" at the end, resulting in "PostMessageW". You don't want this. Just get rid of the W and you are left with raw "PostMessage". That's what you want.

- Bob
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 12:00pm    
Good point. A compensated the vote of "3", which I cannot agree with. My 5.
--SA
If you use unicode functions PostMessageW you should use unicode characters (WCHAR, wchar_t, or unsigned short).
But 'Olivier Levrey' is right, you should NEVER post a message with pointers to the applications message loop. If you post pointers with new you possibly will get memory leaks. For pointers you should always use synchronous calls like SendMessage. Thats why the owner of the memory (stack or new or malloc etc.) is always the caller.
Regards.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 11:59am    
The answer is correct. Who voted 1, I wonder? I want to compensate with my 5.
--SA
mbue 5-Apr-11 13:29pm    
Dont worry ;-)
Bob Ciora 6-Apr-11 6:47am    
[code]PostMessage[/code]'ing a heap-allocated pointer is fine as long as the target knows it and cleans up the pointer in the handling routine. I do it all the time (in MFC) when, e.g. a Dialog class gets information outside the normal windows message loop. This asynchronous method wraps up the data in some heap-allocated structure, then Posts the data to itself so that it can be processed synchronously.

A problem with [code]SendMessage[/code]'ing something (which is asynchronous) is that if you're doing the [code]SendMessage[/code] from a thread that is *not* the main message loop, you run the risk of locking up that main loop. Of course, that depends more on the complexity of the app.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900