Trying to return a struct with the exact layout as in C++, the problem is when TCHAR rMsg[256]
is included in the C++ struct and [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public char rMsg;
is included in the C# struct (both will be shown below) I get the error 'Method's type is not PInvoke compatible'.
I should also point out the C++ isn't mine and I can't change it as other functions require the layout of the struct given below.
I know the issue is the TCHAR as when I comment out the relevant lines in C++ and C# I don't get the error.
I'm calling the .dll I created (which I will also include in the code below) just to return a struct so that I know I have the correct syntax.
C++:
extern "C" {
__declspec(dllexport) tVDACQ_CallBackRecVal ( _stdcall TestAcq2())
{
//Just adding random values so I know it works.
tVDACQ_CallBackRecVal AR;
AR.rFlags = 1;
AR.rFrameHeight = 10;
AR.rFrameWidth = 10;
return AR;
}
}
//Struct
typedef struct {
int rFlags,
rType,
rEvent,
rSocket;
TCHAR rMsg[256]; //Has to stay as TCHAR
int rFrameWidth,
rFrameHeight;
short* rFrameBuffer;
union {
int rCaptureRows;
int rCaptureFrames;
};
int rCapturePercent;
void* rUserCallBackProc,
* rUserParam;
int rAborted;
void* rPacketData;
int rFGControl;
} tVDACQ_CallBackRecVal;
C#:
//Simply calling the .dll
[DllImport("C:\\Users\\jch\\source\\repos\\FlatPanelSensor\\x64\\Debug\\VADAV_AcqS.dll", EntryPoint = "TestAcq2", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern tVDACQ_CallBackRec TestAcq2();
[StructLayout(LayoutKind.Sequential)]
public struct tVDACQ_CallBackRec
{
public int rFlags;
public int rType;
public int rEvent;
public int rSocket;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string rMsg; //I believe this is the issue
public int rFrameWidth;
public int rFrameHeight;
public IntPtr rFrameBuffer;
public int rCaptureRows;
public int rCaptureFrames;
public int rCapturePercent;
public IntPtr rUserCallBackProc;
public IntPtr rUserParam;
public int rAborted;
public IntPtr rPacketData;
public int rFGControl;
}
//Then I just call the function
tVDACQ_CallBackRec testStruct= TestAcq2();
I would expect a struct that has all the components as defined, but instead I get the error as stated in the title.
Again, if I were to not include the TCHAR (c++ struct) and string (c# struct) then I wouldn't get the error so I know that is the underlying issue and I don't know how to solve that part.
EDIT: For solution see first comment from @David Heffernan
The easiest way to solve this is to change the prototype so that the struct is passed as an argument to the function by ref:
void _stdcall TestAcq2(tVDACQ_CallBackRecVal *retval)