I'm new to Interop and need to call a managed C++ method from C# which returns an instance of the following struct:
typedef struct DataBlock_ {
unsigned char data[10240];
unsigned int numberOfBytes;
unsigned long int startAddr;
} DataBlock;
The C++ method which returns the instance is declared as follows:
__declspec(dllexport) DataBlock getDefaultPass( void )
{
DataBlock default_pass = {
{
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF,
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF,
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF,
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF,
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF,
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF,
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF,
(char)0xFF,(char)0xFF,(char)0xFF,(char)0xFF
},
32,
0xFFE0
};
return default_pass;
}
I've declared the struct and method in C# as follows:
public static partial class My
{
[StructLayout(LayoutKind.Sequential)]
public struct DataBlock
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10240)]
public byte[] data;
//public fixed byte data[10240]; <-- this requires 'unsafe' but still doesn't work
public UInt32 numberOfBytes;
public UInt32 startAddr;
}
[DllImport("my.dll")]
static public extern DataBlock getDefaultPass( );
[DllImport("my.dll")]
static public extern byte sendPassword(DataBlock data);
}
I call the method from C# as follows:
var defaultPassword = My.getDefaultPass();
var response = My.sendPassword(defaultPassword);
but the call to getDefaultPass() throws
An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in ConsoleApplication1.exe
Additional information: Method's type signature is not PInvoke compatible.
Based on this question, I tried changing the declaration of data to public fixed byte data[10240] and marked the struct as unsafe, but then the method returns an instance with numberOfBytes and startAddr set to 0, and the subsequent call to sendPassword() fails (note that in this answer the subsequent calls use a pointer to the struct as opposed to the instance itself, as is my case). How then should I be calling the method(s) from C#?
The project targets .NET 3.5 and x86.
Thanks in advance for any help.