And to complete the thought that David started. . . .
IF your double variable is a local stack variable and won't live long enough for PostMessage-type
behavior, you can rectify the problem by making tho double variable a member of your class, so it
won't go out of scope at the end of the function. Then all you need to (maybe) worry about is
thread synchronization.
<pbruyant@yahoo.com> wrote in message
news:560f7377-a959-4d7b-b388-d821d79dcb87@z19g2000vbz.googlegroups.com...
I would like to pass as lParam the address to a double variable, ie:
double dbl;
parentWnd->SendNotifyMessage(SOME_MSG, wParam, &dbl);
As expected, the compiler complains that it cannot convert parameter 3
from 'double *' to 'long'.
I can re-write this line as:
parentWnd->SendNotifyMessage(SOME_MSG, wParam, (long)&dbl);
If we're being pedantic (and that often helps in C++ <g>), the last parameter realy should be
(LPARAM)&dbl.
I wonder if
1) wParam and lParam are intended to be used this way,
In general, yes absolutely but, BE VERY CAREFUL - see warning below.
2) the type cast is safe or if it can be hazardous.
The LPARAM is designed for this. But the handler for SOME_MSG *must* know what to expect, and
cast it back in a way which matches exactly. In this case
double *pDbl = (double *)lParam;
NOW THE WARNING:
The other problem is that "dbl" in your case, must exist when the message is processed, otherwise
pDbl will point to junk.
Using SendMessage() is ok, as the processing happens immediately, and the statement after
SendMessage() is only executed when the message has been processed and SendMessage() has returned.
Using PostMessage() is definitely not ok (in this case) as the message processing only happens
later, by which time dbl has gone out of scope and no longer exists.
Using SendNotifyMessage() is also very dangerous, as sometimes it behaves like SendMessage() and
sometimes like PostMessage() :
"If the window was created by the calling thread, SendNotifyMessage calls the window procedure for
the window and does not return until the window procedure has processed the message. If the window
was created by a different thread, SendNotifyMessage passes the message to the window procedure
and returns immediately; it does not wait for the window procedure to finish processing the
message."
If there is a possibility that the second case happens, then you can't use SendNotifyMessage()
like this. If there isn't, then you may as well use SendMessage() anyway, which is safe.
Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm