Hi there,
I have a wiered scenario. I have two structures defined this way in
unmanaged dll.
struct A{
char * name;
int ID;
};
struct B{
struct A* record;
};
Then I have a function in the dll that consumes a pointer to struct B. like
this.
int InitRecord(struct B* data);
I declared the method as below in C#.
[StructLayout(LayoutKind.Sequential)]
public struct ManagedStructA{
public System.String name;
public System.Int16 ID;
};
[StructLayout(LayoutKind.Sequential)]
public struct ManagedStructB{
public System.IntPtr ManagedStructA;
};
[DllImport("MyCDll.Dll")]
public static extern System.Int16 InitRecord(ref ManagedStructB
managedData);
My question is how can i convert the ManagedStructA object to an IntPtr. Or
is there anyother way I can pass ManagedStructA to ManagedStructB so it will
be passed as a pointer internally. I tried using this declaration but it
throws exception saying "Marshaling types to LPStruct is not supported on
structure field"
[StructLayout(LayoutKind.Sequential)]
public struct ManagedStructB{
[MarshalAs(UnmanagedType.LPStruct)]
public System.IntPtr ManagedStructA;
};
Thanks for help.
David Stucki [MS] - 25 Aug 2003 23:07 GMT
Use Marshal.PtrToStructure and Marshal.StructureToPtr. If you're
allocating the memory that the IntPtr points to you'll also need to use
Marshal.Alloc* and the corresponding Marshal.Free*.
David Stucki
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.