Set C Function Pointer to C # Function

I need to set the callback function in the C library to be equal to the C # function, and cannot find a way to do this without using dlopen or kernel32, which apparently applies to / unix windows. Does anyone know a way to do this?

Problem: Pointers to functions are set in the C shared library whose values ​​must be specified by overwriting them. For example.

//C Code
extern void (*ptr_R_ShowMessage) (const char *)

The current C # code creates a delegate to the function that matches this signature, uses the marshal class to get a pointer to that delegate, and then overwrites the C pointer with this value.

//call to libdl or kernel32.dll 
IntPtr showPointer = GetFunctionAddress(hndl,"ptr_R_ShowMessage");
IntPtr newShowPointer = Marshal.GetFunctionPointerForDelegate(matchingDelegate);
Marshal.WriteIntPtr(showPointer, newShowPointer);

The requirement for libdl and kernel32.dll causes all kinds of problems ... and ideally could be avoided.

- , C- #, C GetFunctionAddress? , , , .

+4
3

, # , libary. , , , .

-2

100%, , .

C Sdk, . C C, #, . ( , , # .)

# a Callback, C, SDK.

, C , :

void DecCallBack(int nPort, void* pBuf, int nSize, FRAME_INFO *pInfo, int nReserved1, int reserved 2);

#. , "" UnmanagedFunctionPointer:

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void DecCallBack(int nPort, IntPtr pBuf, int nSize, ref FRAME_INFO frameInfo, int nReserved1, int nReserved2);

DecCallBack, .

static DecCallBack _decodeCallBack = null;

, . HandleDecData()

private static void HandleDecData(int nPort, IntPtr pBuf, int nSize, ref FRAME_INFO frameInfo, int nReserved1, int nReserved2) {

   // Here we handle the callback,  this code is being called from an external C library

}       

, - #, ( HandleDecData()

_decodeCallBack += new DecCallBack(HandleDecData);

, , C. SDK C PlayM4_SetDecCallBack(), - - .

 if( !PlayM4_SetDecCallBack(channel, _decodeCallBack) ) 
  throw new InvalidOperationException("Error setting DecCallBack");

, .

+2

C ​​ , extern , C

, - # C, C ,

, , "", :

  • C- ,
  • C-, ptr_R_ShowMessage , p1
  • # P1

, , # : http://tutorials.csharp-online.net/CSharp_Delegates_and_Events%E2%80%94Win32_callbacks

+1

All Articles