C# IntPtr with moved memory location, how to apply 'fixed'?

  • Thread starter Thread starter Marcel Ruff
  • Start date Start date
M

Marcel Ruff

Hi,

i have an unmanaged C dll and i call a function from C#
which returns a pointer on a struct inside the C dll:

// C# code:
//member variable
IntPtr intPtr;

someMethod() {
intPtr = getStruct(); // calls C dll
}

when i later use the intPtr (passing it to C dll) it seems to point to another
memory location.

I have tried something like

fixed (void *p = intPtr.ToPointer()) {
...
}

but this does not compile:
"You cannot use the fixed statement to take the address of an already fixed expression"

How can i fix my memory location?

Thanks
Marcel
 
If it needs to be pinned use a GCHandle or use fixed to pinn the location
before the P/Invoke and change your declaration to accept a pointer instead
of an IntPtr. The other alternative is to P/Invoke LocalAlloc to get the
IntPtr, in which case the GC can't move it.
 
My mistake, it is resolved now.

I have called
invokeC(ref IntPtr intPtr)
which obviously made the address of the pointer
available in C (double pointer **).

Removing the 'ref' fixed it.

Thanks
Marcel
 
Back
Top