Marshalling MyStruct[] from C# to C++ API
-
I have an API I'm wrapping in C#, and I'm having trouble tweaking the DllImport. Here's the struct definition in C#:
[
MarshalAs(
UnmanagedType.Struct,
SizeConst = 512,
CharSet = CharSet.Ansi
)
]
public struct MyStruct
{
[MarshalAs(UnmanagedType.I4)]public Int32 Size;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 508)]public String Name;
}In C:
public struct MyStruct
{
int size,
char[508] name
}And the C function declaration:
EXPORT void ListFiles(void* buffer, DWORD count)
{
//Populate the buffer with /count/ records
}The idea is to pass in a pointer to a buffer. The API function will fill that buffer with
count
records. Each record being byte data in the format ofMyStruct
. I can successfully retrieve the data by passing in a buffer of typebyte[512 * count]
. But then I have to manually parse the data with BitConverter calls in afor
loop. What I'm trying to do is declare an array ofMyStruct[count]
, and set up a DllImport that will properly marshal that array into the API, so that the API can fill the structures directly, with no need for me to parse data on return. I've had no success with this whatsoever usingMarshalAs(UnmanagedType.LPArray)
or trying to let the compiler do automatic marshalling. Everything I've tried results in an array of structures that all have size 0 and null names. Anyone have any pointers or suggestions to help me figure out how to marshal an array of structs from C# as a void* in C++?
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM users WHERE clue IS NOT NULL GO
(0 row(s) affected)
-
I have an API I'm wrapping in C#, and I'm having trouble tweaking the DllImport. Here's the struct definition in C#:
[
MarshalAs(
UnmanagedType.Struct,
SizeConst = 512,
CharSet = CharSet.Ansi
)
]
public struct MyStruct
{
[MarshalAs(UnmanagedType.I4)]public Int32 Size;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 508)]public String Name;
}In C:
public struct MyStruct
{
int size,
char[508] name
}And the C function declaration:
EXPORT void ListFiles(void* buffer, DWORD count)
{
//Populate the buffer with /count/ records
}The idea is to pass in a pointer to a buffer. The API function will fill that buffer with
count
records. Each record being byte data in the format ofMyStruct
. I can successfully retrieve the data by passing in a buffer of typebyte[512 * count]
. But then I have to manually parse the data with BitConverter calls in afor
loop. What I'm trying to do is declare an array ofMyStruct[count]
, and set up a DllImport that will properly marshal that array into the API, so that the API can fill the structures directly, with no need for me to parse data on return. I've had no success with this whatsoever usingMarshalAs(UnmanagedType.LPArray)
or trying to let the compiler do automatic marshalling. Everything I've tried results in an array of structures that all have size 0 and null names. Anyone have any pointers or suggestions to help me figure out how to marshal an array of structs from C# as a void* in C++?
Grim
(aka Toby)
MCDBA, MCSD, MCP+SB
SELECT * FROM users WHERE clue IS NOT NULL GO
(0 row(s) affected)
Although I don't know the answer, I may have a tip to you: I learned that, for some more complicated cases, a managed C++ wrapper is the way to go. Interop coding that takes hours or days of coding in C# often takes only a few minutes in managed C++. Unless you have a strong reason to stay with "pure C#", I'd consider using managed C++. I see dead pixels Yes, even I am blogging now!