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

VulpesRank #55 · 29.2K reputationPosted Jun 6, 2012, 9:52 AM EDT
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:
Daniel Passwater7 reputationPosted Jun 6, 2012, 11:10 AM EDT
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?
VulpesRank #55 · 29.2K reputationPosted Jun 6, 2012, 1:01 PM EDT
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 Passwater7 reputationPosted Jun 7, 2012, 6:41 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jun 7, 2012, 6:48 AM EDT
Marshal.StructureToPtr should use structPtr as it did before because the argument is of type IntPtr not void*.
Daniel Passwater7 reputationPosted Jun 7, 2012, 10:49 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jun 7, 2012, 2:04 PM EDT
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