Marshal callback in a struct from C++ to C# - c#

I need to return the necessary information about an object as a struct with callbacks and other data.
This is what it looks like on the C# side:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ChartAddDataCallback(IntPtr data, int size);
[StructLayout(LayoutKind.Sequential)]
public struct ChartAccessors
{
public IntPtr HWnd;
public ChartAddDataCallback addDataCallback;
}
[DllImport("CppPart.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "GetAccessors")]
public static extern ChartAccessors GetAccessors();
The C++ "mirrored" version looks like this:
typedef void(__cdecl *AddDataCallback) (int * data, int size);
struct ChartAccessors
{
HWND m_chartHWnd;
AddDataCallback m_addDataCallback;
};
extern "C" __declspec(dllexport) ChartAccessors GetAccessors();
Usage:
static void Main(string[] args)
{
ChartAccessors accessors = GetAccessors();
}
However, when i start up the program i get the exception "Method's type signature is not PInvoke compatible."
It works if i use any other return type (like int or float) instead of the struct.

Marshal.PtrToStructure was actually solving this issue, as Pavel pointed out.
void RegisterCallbacks(IntPtr callbackPtr)
{
ChartAccessors accessors = (ChartAccessors)Marshal.PtrToStructure(callbackPtr, typeof(ChartAccessors));
// do stuff with the struct
}

Related

Pass C# string to C++ pointer Char in typedef

I need pass a string of IP "192.168.1.1" from C# code to the typedef char pointer in DLL which written by c++. and I declare the char *pcAddrs like
char *pcAddrs; //c++
[MarshalAs(UnmanagedType.LPStr)] public string ip //C#
and declare open function
//c++
int Open( COMMIF_INFO *pInfo )
//c#
[DllImport("Open-IF.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Open(COMMIFINFO info);
Now i get an error of wrong parameter when press button1 to trigger the Open function. It is the string and char pointer wrong?
Thanks in advance.
C++ DLL info
typedef struct CommIfInfo
{
char *pcAddrs;
long lPortNo;
long lRetry;
long lSendTimeOut;
long lCommSide;
long lMode;
long lKind;
} COMMIF_INFO;
//Function need to call.
int Open( COMMIF_INFO *pInfo )
Code in C#
// DLL import
[DllImport("Open-IF.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Open(COMMIFINFO info);
// Structure
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct COMMIFINFO
{
[MarshalAs(UnmanagedType.LPStr)] public string ip;
public int PortNo;
public int Retry;
public int SendTimeOut;
public int CommSide;
public int Mode;
public int Kind;
}
private void button1_Click(object sender, EventArgs e)
{
string _ip = "192.168.1.1";
COMMIFINFO info = new COMMIFINF();
info.ip = _ip;
info.Kind = 1;
int ErrCode = Open(info);
}
Program will get wrong parameter error if passing the COMMIFINFO using method below
[DllImport("Open-IF.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Open(COMMIFINFO info);
Found is because the parameter pass to open function need by reference. So program working if use "in" to pass parameter with reference.
[DllImport("Open-IF.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Open(in COMMIFINFO info);

How to fix 'Cannot marshal 'return value': Invalid managed/unmanaged type combination (Int/UInt must be paired with SysInt or SysUInt).'

I'm trying to return a struct from a C++ callback from C# and I get the error as described in the title.
I appreciate there is a lot of information, but I wanted to include too much rather than not enough.
I've tried returning the function as a structure, but then read it may be easier to return the function as an IntPtr and use Marshal.PtrtoStructure.
The function that I want to call is from a .dll that comes from a different source and the input is:
C++:
rVDACQ_Connect(int, tVDACQ_CallBackProc, void*, short*, int)
the function in the sample (C++) code returns a struct (rACQ_CallBackRec) which is as follows:
rACQ_CallBackRec = theApp.m_Intf.rVDACQ_Connect(cVDACQ_FBright, CALLBACK_Acquisition, this, m_FrmBuffer, 0);
//rACQ_CallBackRec is the struct type tVDACQ_CallBackRec (as described below)
With CALLBACK_Aquisition being:
extern "C" {
__declspec(dllexport) void _stdcall CALLBACK_Acquisition(tVDACQ_CallBackRec* AR)
{
tVDACQ_CallBackProc test;
((CPreviewDlg*)AR->rUserParam)->My_ACQ_CallBack(AR, test);
}
}
The layout of the struct is as follows:
typedef struct {
int rFlags, // combination of cVDACQ_Fxxxx
rType, // cVDACQ_ETxxx
rEvent, // cVDACQ_Exxx
rSocket; // 0:no relation to a 'socket'; otherwise socket's ID>0 (event's source ID)
TCHAR rMsg[256]; // message (trace, wrn, err)
int rFrameWidth, // full frame width
rFrameHeight; // full frame height
short* rFrameBuffer; // user supplied frame buffer "AFrameBuffer"
union {
int rCaptureRows; // # of received rows (for single frame acquisition)
int rCaptureFrames; // # of received full frames (for framegrabber)
};
int rCapturePercent; // received data in percents
void* rUserCallBackProc, // user supplied "ACallBackProc"
* rUserParam; // user supplied "AUserParam"
int rAborted; // 1: VDACQ_Abort -1:internally
void* rPacketData; // pointer to received packet; usually it is nil
int rFGControl; // frame-grabber's control flags
} tVDACQ_CallBackRec;
typedef void(_stdcall* tVDACQ_CallBackProc)(tVDACQ_CallBackRec*); //The Callback
C#
I've created the callback procedure in C#, as well as the struct and PInvoke:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void tVDACQ_CallBackProc(tVDACQ_CallBackRec AR);
[DllImport("C:\\Users\\jch\\source\\repos\\FlatPanelSensor\\x64\\Debug\\VADAV_AcqS.dll", EntryPoint = "CALLBACK_Acquisition", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern void CALLBACK_Acquisition(tVDACQ_CallBackRec AR);
[DllImport("C:\\Users\\jch\\source\\repos\\FlatPanelSensor\\FlatPanelSensor\\bin\\Debug\\VADAV_FGM_64.dll", EntryPoint = "VDACQ_Connect", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I4)]
public unsafe static extern IntPtr VDACQ_Connect(int i, [MarshalAs(UnmanagedType.FunctionPtr)] tVDACQ_CallBackProc proc, dynamic n, IntPtr[] buffer, int j);
Struct:
[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;
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;
}
CallBack:
tVDACQ_CallBackProc callBack =
(AR) =>
{
CALLBACK_Acquisition(AR); //Don't know how necessary this
is
};
Function call:
IntPtr work = VDACQ_Connect(0, callBack, this, m_FrmBuffer, 0);
//I know 'this' isn't defined as a void* in my DLLImport,
//but making it an IntPtr didn't work either so
//the only type I could think of was dynamic.
If I could return this as a IntPtr or, even better, the struct I defined in my C# code that would be great.
If you need anymore information then please let me know, as I think I included everything.

How to assign value to a char array (C-String) within C++ DLL through C# code

I have a DLL developed in C++ that performs some computations. I am trying to link this DLL into my C# application.
I have a struct within a struct which contains a char array (C string). I have a pointer to the object created by the C++ DLL within my C# application.
namespace Test
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Child
{
public float number1;
public float number2;
[MarshalAs(UnmanagedType.LPArray)]
public char[] name;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct DataStructure
{
public Child child;
...
}
.
.
.
public unsafe partial class Form1:Form {
[DllImport("Calculation.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern IntPtr createInstance();
[DllImport("Calculation.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern DataStructure* processData(IntPtr source); // Setup of data
[DllImport("Calculation.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern int calculate(IntPtr calc); // Perform calculation
.
.
public Form2()
{
private void button1_Click_1(object sender, EventArgs e)
{
// test functions
unsafe
{
IntPtr ptr1 = createInstance(); // Returns pointer to the instance
DataStructure* data = processData(ptr1); // Returns pointer to DataStructure object created through DllImport
data->child.number1 = 1.234F; // Works
data->child.number1 = 9.876F; // Works
data->child.name = "......" // This doesn't work!!
int result = calculate(ptr1); // Returns value when name commented
}
}
}
}
If I don't comment name, I get the Error - Cannot take the address of managed object.
If I comment name, I am able to run the calculation without issue.
I wish to update the value of the char array name but can't seem to figure out the solution. Below is my usual C solution. Thanks!
#include "DataStructure.h"
#define DLLNAME "Calculation.dll"
int main(int argc, char** argv) {
HINSTANCE dllHandle = LoadLibraryA(fileNameDll);
const void* ptr1 = 0;
int result = 0;
DataStructure *data;
ptr1 = createInstance();
data = processData(ptr1);
data->child.number1 = 1.234;
data->child.number2 = 9.876;
sprintf(data->child.name, "3104-03");
result = calculate(ptr1);
}

How to marshal a const char* parameter in function pointer used to send data from C++ dll to C# to avoid Release build crash

I register a function pointer that takes a string as parameter in the C++ dll that is used to send text back to the C# application. I tried to marshal the string parameter as IntPtr and BStr in addition to others. With the debug build I have no crash. With the release build the data are returned correctly (tested with log to a txt file). However, the C# program crashes with AccessViolationException.
Here is the code:
A: using BStr
C++:
typedef void(*callbackFunction)(BSTR resultDataJson);
extern "C" DLLEXPORT int setReceiveFunction(callbackFunction funcPointer)
{
//function pointer is stored
return 0;
}
//send the data back with function pointer
void MyClass::sendCallback(const QByteArray& msg)
{
if (this->m_callbackFunctionForReceivedData != nullptr)
{
BSTR resultMsg = this->ANSItoBSTR(msg.data());
this->m_callbackFunctionForReceivedData(resultMsg);
}
}
C#:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void receiveCallbackData([MarshalAs(UnmanagedType.BStr)]string resultDataJson);
[DllImport("./myDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static public extern int setReceiveFunction([MarshalAs(UnmanagedType.FunctionPtr)] receiveCallbackData functionCallback);
//callback function where data are received
private void functionCallback(string receivedJsonData)
{
//The received string can be logged here and looks correct.
}
B. using IntPtr
typedef void (*callbackFunction)(const char* resultDataJson);
extern "C" DLLEXPORT int setReceiveFunction(callbackFunction funcPointer)
{
//function pointer is stored
return 0;
}
//send the data back with function pointer
void MyClass::sendCallback(const QByteArray& msg)
{
if (this->m_callbackFunctionForReceivedData != nullptr)
{
this->m_callbackFunctionForReceivedData(msg.data());
}
}
C#:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void receiveCallbackData(IntPtr resultDataJson);
[DllImport("./MyDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static public extern int setReceiveFunction([MarshalAs(UnmanagedType.FunctionPtr)] receiveCallbackData functionCallback);
private void functionCallback(IntPtr receivedJsonDataInt)
{
string receivedJsonData = Marshal.PtrToStringAnsi(receivedJsonDataInt);
//The received string can be logged here and looks correct.
}
Both work in Debug but crash in Release. Why? What is the solution to this problem? Thanks for your help!

Invalid Managed/Unmanaged Type Combination With Embedded, Dynamically-Allocated Array

I have a common construct in an unmanaged Win32 C++ DLL:
// FirstElemPtrContainer.h
#include "stdafx.h"
typedef unsigned char elem_type; // a byte
typedef struct FirstElemPtrContainer {
unsigned char num_elems;
void *allocd_ary;
} FirstElemPtrContainer;
The void* in the struct is meant to contain a pointer to the first element of an allocated byte array.
The DLL that uses this definition then exports functions to allocate, populate, and deallocate the struct:
// The exported allocator function.
extern "C" _declspec(dllexport)
FirstElemPtrContainer *BuildStruct(int elem_count)
{
FirstElemPtrContainer *fepc_ptr = new FirstElemPtrContainer;
fepc_ptr->num_elems = elem_count;
elem_type *ary = new elem_type[fepc_ptr->num_elems];
for (int i = 0; i < fepc_ptr->num_elems; i++)
{
ary[i] = ((i + 1) * 5); // multiples of 5
}
fepc_ptr->allocd_ary = ary;
return fepc_ptr;
}
// The exported deallocator function.
extern "C" _declspec(dllexport) void
DestroyStruct(FirstElemPtrContainer *fepc_ptr)
{
delete[] fepc_ptr->allocd_ary;
delete fepc_ptr;
}
These work just fine for a native caller.
In C#, I try to describe this same structure via PInvoke:
[StructLayout(LayoutKind.Sequential)]
public struct FirstElemPtrContainer
{
public byte num_elems;
[MarshalAs(UnmanagedType.LPArray,
ArraySubType = UnmanagedType.U1, SizeConst = 4)]
public IntPtr allocd_ary;
}
... and describe the call interface like so:
public static class Imports
{
[DllImport("MyLib", CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr BuildStruct(int elem_count);
[DllImport("MyLib", CallingConvention = CallingConvention.Winapi)]
public static extern void DestroyStruct(IntPtr fepc_ptr);
}
Now I attempt to call my interface:
class Program
{
const int NUM_ELEMS = 4;
static void Main(string[] args)
{
IntPtr fepc_ptr = Imports.BuildStruct(NUM_ELEMS);
if ( fepc_ptr == IntPtr.Zero )
{
Console.WriteLine("Error getting struct from PInvoke.");
return;
}
FirstElemPtrContainer fepc =
(FirstElemPtrContainer)Marshal.PtrToStructure(fepc_ptr,
typeof(FirstElemPtrContainer));
//...
}
}
The PtrToStructure() call gives the error "Cannot marshal field 'allocd_ary' of type 'MyLibInvoke.FirstElemPtrContainer': Invalid managed/unmanaged type combination (Int/UInt must be paired with SysInt or SysUInt)."
You can see that I've hard-coded a particular number of elements, which we'll assume the caller adheres to. I've also added an ArraySubType clause, though it seems not to make a difference. Why the type mismatch complaint?
Your struct should be declared like this:
[StructLayout(LayoutKind.Sequential)]
public struct FirstElemPtrContainer
{
public byte num_elems;
public IntPtr allocd_ary;
}
it has to be done this way because allocd_ary is a pointer to unmanaged memory and cannot be marshalled by the p/invoke marshaller.
In order to read the contents of allocd_ary you can use Marshal.Copy.
FirstElemPtrContainer fepc = (FirstElemPtrContainer)Marshal.
PtrToStructure(fepc_ptr, typeof(FirstElemPtrContainer));
byte[] ary = new byte[fepc.num_elems];
Marshal.Copy(fepc.allocd_ary, ary, 0, ary.Length);
I suspect that CallingConvention.Winapi is wrong and that you should be using CallingConvention.Cdecl.

Categories

Resources