3

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

mskfisher
  • 3,291
  • 4
  • 35
  • 48
  • Do you want a float value to pass by value, or by pointer? So 'float' or 'float *' instead of 'void *'? – Rutger Nijlunsing Jun 12 '09 at 09:18
  • There are three different ways your question could be taken. You need to clarify the question to get the correct answer. The three possibilities are: (1) How do I make an 32 bit intptr that has exactly the same bits as a 32 bit float? (2) how do I make an intptr that contains the address of some variable I have that contains a float? Or (3) how do I take an intptr that contains a given address of a variable, and replace the contents of that variable with the given float? – Eric Lippert Jun 12 '09 at 16:10

1 Answers1

3

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].

Timbo
  • 27,472
  • 11
  • 50
  • 75