• C

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?
kaplan1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kaplan1Author Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.