I'm working with legacy code base that has some C++ that's been converted to managed. I'm inheriting my C# class from the C++. I'm overriding a function that makes a call to a function that's signature contains a void* as a parameter. I can't seem to find anything in C# that matches it. (I've tried several things, including IntPtr and ref.)
C++
WriteStructure( String path, void* structure, unsigned int size )
Does anyone have a suggestion?
Loading
VulpesPosted Jun 7, 2012, 6:04 PM
If it's a compiler error you're getting, then all you need to do is to ensure that the argument you're passing is of the correct type from C#'s viewpoint.
If the argument type is IntPtr then you can pass structPtr or if it's void* (in unsafe code) you need to pass structPtr.ToPointer() which returns a value of that type:
http://msdn.microsoft.com/en-us/library/system.intptr.topointer.aspx
Daniel PasswaterPosted Jun 7, 2012, 2:49 PM
VulpesPosted Jun 7, 2012, 10:48 AM
Marshal.StructureToPtr should use structPtr as it did before because the argument is of type IntPtr not void*.
Daniel PasswaterPosted Jun 7, 2012, 10:41 AM
VulpesPosted Jun 6, 2012, 5:01 PM
WriteStructure(path, structPtr.ToPointer(), (uint) size);
If you're not using unsafe code, then you need to specify the parameter type to be IntPtr rather than void*.
You should be able to paste text into the reply box directly from the clipboard.
Daniel PasswaterPosted Jun 6, 2012, 3:10 PM
IntPtr structPtr = Marshal.AllocHGlobal(sizeOfStruct);
try
{
Marshal.StructureToPtr(myStruct, structPtr, false);
MyStructType myStructTemp = (MyStructType)Marshal.PtrToStructure(structPtr, typeof(MyStructType));
WriteStructure(path, (IntPtr)structPtr, (uint) size );
}
...but it didn't work: "cannot convert from 'System.IntPtr' to 'void*'
BTW: Is there a way to paste text into the reply window?
VulpesPosted Jun 6, 2012, 1:52 PM
However, you'll need to use a method such as Marshal.StructureToPtr to marshal whatever structure you're passing to an IntPtr in the first place: