Get an array of structures from native dll to c# application
Posted
by PaulH
on Stack Overflow
See other posts from Stack Overflow
or by PaulH
Published on 2010-03-08T16:50:14Z
Indexed on
2010/03/08
17:06 UTC
Read the original article
Hit count: 275
I have a C# .NET 2.0 CF project where I need to invoke a method in a native C++ DLL. This native method returns an array of type TableEntry
. At the time the native method is called, I do not know how large the array will be.
How can I get the table from the native DLL to the C# project? Below is effectively what I have now.
// in C# .NET 2.0 CF project
[StructLayout(LayoutKind.Sequential)]
public struct TableEntry
{
[MarshalAs(UnmanagedType.LPWStr)] public string description;
public int item;
public int another_item;
public IntPtr some_data;
}
[DllImport("MyDll.dll",
CallingConvention = CallingConvention.Winapi,
CharSet = CharSet.Auto)]
public static extern bool GetTable(ref TableEntry[] table);
SomeFunction()
{
TableEntry[] table = null;
bool success = GetTable( ref table );
// at this point, the table is empty
}
// In Native C++ DLL
std::vector< TABLE_ENTRY > global_dll_table;
extern "C" __declspec(dllexport) bool GetTable( TABLE_ENTRY* table )
{
table = &global_dll_table.front();
return true;
}
Thanks, PaulH
© Stack Overflow or respective owner