Sign in to follow this  

WM_USER

This topic is 4377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Im trying to send over the value of a float variable from one application to another. The Win32 application who send the message:
float* g_pfTest = new float; <- global pointer

*g_pfTest = 22.4; <- just a test value
SendMessage(g_hwndOtherProgram, WM_USER+1, (WPARAM) 0, (LPARAM) g_pfTest ); 



In the Win32 applications Winproc that gets the message:
switch (message)
{
case WM_USER+1:
  float t = *(float*) lParam;
  char text[40];
  sprintf(text, "%0.3f", (float) t);
  MessageBox(hwnd, text, 0, 0);
  break;
}




The messagebox print "0.0", that isn't the correct value sent. Anyone know why?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need to pass that by value.


SendMessage(g_hwndOtherProgram, WM_USER+1, (WPARAM) 0, (LPARAM) *g_pfTest );

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Or use the WM_COPYDATA message. Here is an example of sending a string to another window:

COPYDATASTRUCT cds;
cds.dwData = MyDataId; //Some unique number
cds.cbData = s_cast(strlen(str)+1);
cds.lpData = str;
SendMessage(window, WM_COPYDATA, 0, (LPARAM)&cds);

Share this post


Link to post
Share on other sites
You can still send the float directly without needing WM_COPYDATA. You just need to use a little care to prevent the compiler from converting it an integer and back again. Something like:

SendMessage(wnd, WM_USER+1, 0, * (LPARAM *) g_pfTest);
...
case WM_USER+1:
float t = * (float *) &lParam;

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
You can still send the float directly without needing WM_COPYDATA. You just need to use a little care to prevent the compiler from converting it an integer and back again. Something like:

SendMessage(wnd, WM_USER+1, 0, * (LPARAM *) g_pfTest);
...
case WM_USER+1:
float t = * (float *) &lParam;

This is the solution I would use. It allows you to still use WM_USER. :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And as soon as you change your implementation to use a double, then what?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by supercoder74
couldnt you use the volatile keyword in this occasion?


No. There's nothing about this that would be helped by 'volatile'.

Share this post


Link to post
Share on other sites
Quote:
Original post by Flimflam
Quote:
Original post by Anon Mike
You can still send the float directly without needing WM_COPYDATA. You just need to use a little care to prevent the compiler from converting it an integer and back again. Something like:

SendMessage(wnd, WM_USER+1, 0, * (LPARAM *) g_pfTest);
...
case WM_USER+1:
float t = * (float *) &lParam;

This is the solution I would use. It allows you to still use WM_USER. :)


You can pass the WM_USER message code through the COPYDATASTRUCT.dwData if you want.

Also, since it has been said in this thread and I've heard it numerous times: GlobalAlloc does not allocate shared memory accessible to multiple processes. The memory is still in the private address space of the process which called GlobalAlloc, and allocated from the heap(and in Win32, there is no difference between the local and the global heap). If you want to use shared memory, the easiest way would be to use File Mapping.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by supercoder74
couldnt you use the volatile keyword in this occasion?


No. There's nothing about this that would be helped by 'volatile'.


Ah, ok. I am confused about volatile. :P

Share this post


Link to post
Share on other sites
From MSDN:
Quote:

Message numbers in the second range (WM_USER through 0x7FFF) can be defined and used by an application to send messages within a private window class. These values cannot be used to define messages that are meaningful throughout an application, because some predefined window classes already define values in this range. For example, predefined control classes such as BUTTON, EDIT, LISTBOX, and COMBOBOX may use these values. Messages in this range should not be sent to other applications unless the applications have been designed to exchange messages and to attach the same meaning to the message numbers.


In other words, use WM_APP instead of WM_USER.

Share this post


Link to post
Share on other sites

This topic is 4377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this