views:

456

answers:

3

Hi,

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  A: 

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
A: 

One thing you could try is

IntPtr ptr = new IntPtr(&fltVal);

You'll have to mark the method it is in as unsafe, and compile with the /unsafe flag

Patrick McDonald
A: 

Hi,

thanks to you all for your quick response. I tried the first suggestion and it seemed to work. "Seemed" in this case means I cannot say for sure yet. There are some other problems to be solved (not by me) before I can verify it really works. I'll be back to give feedback then.

Thanks again Simone