I want to call the following C function from C#, but it doesn't work. The C code is part of the .so file. In my case the code should run in an Linux environment!
The C# Code is part of a console app which uses the .so file
/**
* \brief Register a tag callback functions.
* \param callback: tag callback functions.
* \return None
*/
extern void nfcManager_registerTagCallback(nfcTagCallback_t *callback);
Here is the definition of the required parameter: (C Code)
/**
* \brief NFC Tag callback function structure definition.
*/
typedef struct {
/**
* \brief NFC Tag callback function when tag is detected.
* param pTagInfo tag infomation
*/
void (*onTagArrival) (nfc_tag_info_t *pTagInfo);
/**
* \brief NFC Tag callback function when tag is removed.
*/
void (*onTagDeparture) (void);
}nfcTagCallback_t;
And this is the defintion of nfc_tag_info_t: (C Code)
/**
* \brief NFC tag information structure definition.
*/
typedef struct
{
/**
* \brief indicates the technology of tag
*/
unsigned int technology;
/**
* \brief the handle of tag
*/
unsigned int handle;
/**
* \brief the uid of tag
*/
char uid[32];
/**
* \brief the uid length
*/
unsigned int uid_length;
/**
* \brief activated protocol
*/
tNFC_PROTOCOL protocol;
}nfc_tag_info_t;
I tried the following: (C# Code)
[DllImport("libnfc_nci_linux.so", CharSet = CharSet.Unicode, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
private static extern void nfcManager_registerTagCallback(IntPtr callback);
[StructLayout(LayoutKind.Sequential)]
public struct nfcTagCallback_t
{
[MarshalAs(UnmanagedType.FunctionPtr)]
public IntPtr onTagArrival;
[MarshalAs(UnmanagedType.FunctionPtr)]
public IntPtr onTagDeparture;
}
public static void onArrival(IntPtr intPtr)
{
Console.WriteLine("Arrival");
}
public static void onDeparture()
{
Console.WriteLine("Departure");
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void CallbackOnArrival(IntPtr intPtr);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void CallbackOnDeparture();
[StructLayout(LayoutKind.Sequential)]
private struct nfc_tag_info_t
{
/**
* \brief indicates the technology of tag
*/
public uint technology;
/**
* \brief the handle of tag
*/
public uint handle;
/**
* \brief the uid of tag
*/
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public char[] uid;
/**
* \brief the uid length
*/
public uint uid_length;
/**
* \brief activated protocol
*/
public byte protocol;
}
I call the method like this:
var nfcTagCallback = new nfcTagCallback_t();
nfcTagCallback.onTagDeparture = Marshal.GetFunctionPointerForDelegate(new CallbackOnDeparture(onDeparture));
nfcTagCallback.onTagArrival = Marshal.GetFunctionPointerForDelegate(new CallbackOnArrival(onArrival));
IntPtr testPointer = Marshal.AllocHGlobal(Marshal.SizeOf(nfcTagCallback));
Marshal.StructureToPtr(nfcTagCallback, testPointer, true);
nfcManager_registerTagCallback(testPointer); // <-- Exception (see below)
If I run this code, the following exception is thrown:
Unhandled exception. System.ArgumentException: Type 'PN7150_NFC.Program nfcTagCallback_t' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
at System.Runtime.InteropServices.Marshal.SizeOfHelper(Type t, Boolean throwIfNotMarshalable)
at System.Runtime.InteropServices.Marshal.SizeOf[T](T structure)
at PN7150_NFC.Program.Main(String[] args) in /home/pi/Desktop/PN7150-NFC/PN7150-NFC/Program.cs:line 95
Can somebody help me?
CodePudding user response:
You are trying to marshal IntPtr
as FunctionPtr
, which should be used on delegates. You can either:
- Define
onTagArrival
asCallbackOnArrival
andonTagDeparture
asCallbackOnDeparture
. - remove
[MarshalAs(UnmanagedType.FunctionPtr)]
.
Finally, as @jdweng pointed out in the comments, be careful with the calling convensions.