[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1069
  • Last Modified:

How to pass a float as LPARAM in SendMessage()

I am building a Window App.  I want to send a message
from within a C function.  I want one of the parameters
in the message to be a float.  Here is the code:

float f_value;
f_value = 3.14;
SendMessage(Wnd1, MY_MESSAGE_TYPE,  1, f_value);

I get the following compiler warning:

warning C4244: 'function' : conversion from 'float ' to 'long ', possible loss of data

How can I pass the float value as a parameter of the message?
0
kaplan1
Asked:
kaplan1
1 Solution
 
inprasCommented:
Hi kaplan1
Use IEEE standard to do it which takes 32 bits to store a float value

the range for float is
bit 0    sign of mantissa
bit 1-23 mantissa
bit 24   sign bit for exponent
bit 25-32 exponent

now lets take example

value = 3.14
make it .314*(10^1) since 10 is default base we can forget it
all that we have to store is
314 in bits 1-23
bit 0 is 0 because sign bit is 1 if value is -ve
bit 24 is again 0 since sign of exponent is positive
bit 25-32 will contain the value 1

now U get a 32 bit value which is rewuired as LPARAM
Regards
0
 
tdubroffCommented:
If a float on your machine is only 32 bits that you can send it as a long (implicitly casting will generate a warning).  If your float is actually 64 bits, then a 32bit long won't cut it - you'd have to send 2 messages.

But lets say that a float and a long are actually the same bit size.  Then you can just write this:

float f_value;
f_value = 3.14;
SendMessage(Wnd1, MY_MESSAGE_TYPE,  1, (long)f_value);
// explicit cast will remove warning


Now, whoever receives the data must then cast the long value back to a float, like so:

float gotcha = (float) f_value_long;
0
 
ZoppoCommented:
Hi kaplan1,

If you want to use the message only within your own single process you can just pass the pointer to the float casted to long, i.e.:

float f_value;
f_value = 3.14;
SendMessage( Wnd1, MY_MESSAGE_TYPE, 1, (long)&f_value );

hope that helps,

ZOPPO
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
kaplan1Author Commented:
inpra,
 
I don't understand your answer.  What exact code should I put
in?

tdubroff,

Your comment is wrong since the cast to long actually
causes a conversion and I end up with 3.0.

Zoppo,

Your comment is wrong because it causes a long representation
of the pointer address to be sent.  It ends up with garbage:
6.94622e+006
0
 
ozoCommented:
float f_value;
long  l_value;
f_value = 3.14;
l_value = *(long *)&f_value;
f_value = *(float *)&l_value;
0
 
kaplan1Author Commented:
Thanks!
0

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now