0
Answer

calling a DLL function using function pointers

sindhu maheswari

sindhu maheswari

14y
2.3k
1

hi all,
i am trying to export my C++ functions to C#. and i have succeeded in exporting them. while importing the functions. now to one of such functions, i want to pass two newly defined functions in C# as arguments, how should i achieve this. kindly help me.i am very badly in need of the solution to complete my project.
this is how i have implemented:
1. function exporting from C++:
<b>extern __declspec(dllexport) bool Callregistercallback(CRTSPWrapper* pObject,funccallback fptr_video,funccallback fptr_audio,void* ptr);</b>
where funccallback is a function pointer
2. function call in C++ test application
m_oSarixrtsp-&gt;registercallback(GetVideoPacket,GetAudioPacket,NULL);
where GetVideoPacket and GetAudioPackets are functions that are defined in the main of C++
3.function importing in C#
[DllImport("sampleclient_dll.dll", CharSet = CharSet.Ansi)]
static private extern bool Callregistercallback(IntPtr pTestClassObject,IntPtr fptr_video,IntPtr fptr_audio,IntPtr ptr);
4.function defintion in C#:
here i have defined two delegates to the functions namely Getvideopacket and getaudiopacket as follows:
public delegate void getadelegate(char[] pBuffer, int nSize);
public delegate void getvdelegate(char[] pBuffer, int nSize);
 public bool registercallback([MarshalAs(UnmanagedType.FunctionPtr)]IntPtr fptr_video, [MarshalAs(UnmanagedType.FunctionPtr)] IntPtr fptr_audio, IntPtr ptr)
        {
            getvdelegate objv = new getvdelegate(Getvideopacket);
            getadelegate obja = new getadelegate(Getaudiopacket);
            return Callregistercallback(this.m_pNativeObject, fptr_video, fptr_audio, ptr);
        }
5.function call from test application in C#:
testclass.registercallback(Getvideopacket,Getaudiopacket,null);

this is resulting in a compile time error stating: cannot convert a System method to System IntPtr.
Kinldy help me out please. thanks in advance.