Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reusable code for marshaling structures?

Status
Not open for further replies.

ProtocolPirate

Programmer
Nov 21, 2007
104
0
0
US

I would like to create a base class which can generically handle marshaling different table structures to unmanaged Brieve calls. I'm trying to avoid repeating the whole implementation for each different table structure. So I need to be able to do things like:

'Initialize the marshaling
m_dataLength = Marshal.SizeOf(myStruct)
m_ptr = Marshal.AllocCoTaskMem(m_dataLength)
Marshal.StructureToPtr(myStruct, m_ptr, True)

and

'Return a structure from unmanaged memory to managed memory
Native.BtrCall(Operation.GetFirst, m_posblk, m_ptr, m_dataLength, ...)
myStruct = Marshal.PtrToStructure(m_ptr, GetType(myStruct)) ' return this to the derived class


Where myStruct could be a number of different types of table structures. (And m_ptr is an IntPtr). I need to be able get these different structures passed to the marshaling functions in a generic way, such as with the type Object. On the call to Marshal.PtrToStructure(m_ptr, GetType(myStruct)) I can assign the return value to an Object and it works great, but when I pass a table structure in to the top three lines as an Object parameter the program quickly hangs after just a few calls to the unmanaged code.

Is there a way to do this so that I can get reusable code?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top