Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "fill" an IntPtr parameter with a float value?

I am using dllImport to use a C library in C# .NET. One of the methods in this library uses data type void* as parameter. I found out, that I can use the data type IntPtr in C# matching the void*.

Now I simply don't know how to set the value of this IntPtr parameter. In fact I want to put a float value into this parameter. How would I do this?

Thanks in advance for any idea. Simone


1 Answers

If you can use unsafe blocks, this one works:

static IntPtr IntPtrFromFloat( float f )
{
    unsafe
    {
        return (*(IntPtr*)&f);
    }
}

It creates an IntPtr containing an address equal to the binary representation of the float.

It should also be possible to just declare the parameter as float. It is 32bits anyway [32bit C-DLL assumed].

like image 62
Timbo Avatar answered Mar 01 '26 08:03

Timbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!