Passing A Float In WParam or LParam of Windows Message

D

dzar

I have an application that sends messages to other applications through
PostMessage(HWND_BROADCAST, MY_MESSAGE_ID, wparam_float, lparam_float);
in C (and this works... I can typecast and built byte arrays with
whatever I want in C and extract them at the other end just fine).

I am now needing to interface with C# and find I cannot do this in an
easy way (at least for me). When I try something like:

Single sValue= (Single) m.LParam;

in my OnNotifyMessage override, I get a float (Single) but of the large,
"integer" value it things LParam must be (but it's not, it's a 4-byte
floating point value).

Any thoughts on what to do to make C# "typecast" this to a Single, properly?

Thanks,
Dave
 
J

Jeffrey Tan[MSFT]

Hi Dave,

I am not sure I understand your problem completely. Can you help to explain
the following statement clearer?
"in my OnNotifyMessage override, I get a float (Single) but of the large,
"integer" value it things LParam must be (but it's not, it's a 4-byte
floating point value)."

Based on my experience, m.LParam is of type "IntPtr", however, it should be
no problem to convert it into Single if you are sure that this message's
lParam is actually float type.

I will wait for your further clarify, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dzar

Jeffrey said:
Hi Dave,

I am not sure I understand your problem completely. Can you help to explain
the following statement clearer?
"in my OnNotifyMessage override, I get a float (Single) but of the large,
"integer" value it things LParam must be (but it's not, it's a 4-byte
floating point value)."

Based on my experience, m.LParam is of type "IntPtr", however, it should be
no problem to convert it into Single if you are sure that this message's
lParam is actually float type.

What you say may be correct (I cannot verify, as I"M not in front of the C#
system right now). But, yes, I do wish to treat this IntPtr as a Single (float)
as that is what it is (really, it is!). I just have not found a way to do it (I
am a new C# programmer, does it show?)

Thanks!
Dave
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for your feedback.

System.IntPtr is also a value type which is used to represent a pointer or
a handle. On x86 system, the handle and pointer is of length 4 bytes, so
the System.IntPtr is also 4 bytes. Since System.Single type is also a value
type with 32bit(4 bytes) length, you can convert m.LParam to "Single"
directly without any problem.

I have copied your code below into the VS2005 C# IDE, it compiles without
any problem:
Single sValue= (Single) m.LParam;

Anyway, if you need any further help, please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dzar

Jeffrey said:
Hi Dave,

Thanks for your feedback.

System.IntPtr is also a value type which is used to represent a pointer or
a handle. On x86 system, the handle and pointer is of length 4 bytes, so
the System.IntPtr is also 4 bytes. Since System.Single type is also a value
type with 32bit(4 bytes) length, you can convert m.LParam to "Single"
directly without any problem.

I have copied your code below into the VS2005 C# IDE, it compiles without
any problem:
Single sValue= (Single) m.LParam;

OK, I'm obviously not being clear. What you are showing takes whatever integer
value is in m.LParam and represents that as a float. For example, if m.LParam is
0xc269b440 (-1033259968)then sValue = -1.033259968E+9. This is NOT correct.
The value in m.LParam is 5.6379 when read as an actual floating point value.
This is what m.LParam is. It's not an integer at all.

In C, for example, I do something like

float *mfp = (float *) &LParam;
float myfloat = *mfp;

Now the final result is in myfloat and is the floating point value that was
passed as opposed to the floating point representation of the integer in LParam.

This is what I cannot do in C#. Well, I guess I could do the same thing with
unmanaged code and pointers, but I was hoping for a better way. And I've
actually had no luck getting unmanaged code to work in this way, either, which
is why I assumed there must be a better way.

Any new thoughts?

Thanks,
Dave
 
P

Peter Duniho

[...]
This is what I cannot do in C#. Well, I guess I could do the same thing
with unmanaged code and pointers, but I was hoping for a better way.
And I've actually had no luck getting unmanaged code to work in this
way, either, which is why I assumed there must be a better way.

I'm not sure why you can't get it to work using unsafe code. I also am
not sure there's not a better way, but you should be able to do what you
want using the BitConverter class.

Pete
 
D

dzar

Peter said:
[...]
This is what I cannot do in C#. Well, I guess I could do the same
thing with unmanaged code and pointers, but I was hoping for a better
way. And I've actually had no luck getting unmanaged code to work in
this way, either, which is why I assumed there must be a better way.

I'm not sure why you can't get it to work using unsafe code. I also am
not sure there's not a better way, but you should be able to do what you
want using the BitConverter class.

Perfect! Thank you for the pointer to the BitConverter class. It does exactly
what I want. I'm new and finding my way though all the classes is quite a chore!
Glad this is as easy as it should be.

Regards,
Dave
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Sorry for misunderstanding your point.

Oh, I think I understand your meaning now.

In the message sending side, you have converted the float bytes layout and
re-interpret it as DWORD. So in the .Net managed code receiving side, you
need to get the pure bytes layout of the *int* and re-interpret it as float
type. So, the code snippet below demonstrated your logic:

//Message Sending side, bytes convetion
float f = (float)3.1415926;
Byte [] bytes=BitConverter.GetBytes(f);
IntPtr ip = (IntPtr)BitConverter.ToUInt32(bytes, 0);

//Message Receving side
Single new_f=BitConverter.ToSingle(BitConverter.GetBytes((uint)ip), 0);

I think this code snippet should meet your need. If not, please feel free
to let me know. Thanks.

Also, thank Peter for his sharing.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top