problem while using delegates in c#
-
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++: extern __declspec(dllexport) bool Callregistercallback(CRTSPWrapper* pObject,funccallback fptr_video,funccallback fptr_audio,void* ptr); where funccallback is a function pointer 2. function call in C++ test application m_oSarixrtsp->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. regards sindhu
-
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++: extern __declspec(dllexport) bool Callregistercallback(CRTSPWrapper* pObject,funccallback fptr_video,funccallback fptr_audio,void* ptr); where funccallback is a function pointer 2. function call in C++ test application m_oSarixrtsp->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. regards sindhu
There is quite alot of code here interspersed in mixed languages with non code which makes it rather difficult to find what is what. Could you edit you post wrapping the code parts in <pre> </pre> tags (you can highlight the code and click the 'code block' link above to do this automatically)? It would be helpful if you could use different pre blocks for each language section.
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
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++: extern __declspec(dllexport) bool Callregistercallback(CRTSPWrapper* pObject,funccallback fptr_video,funccallback fptr_audio,void* ptr); where funccallback is a function pointer 2. function call in C++ test application m_oSarixrtsp->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. regards sindhu
that is unreadable, lacking PRE tags; as it involves P/Invoke, maybe this[^] helps. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
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++: extern __declspec(dllexport) bool Callregistercallback(CRTSPWrapper* pObject,funccallback fptr_video,funccallback fptr_audio,void* ptr); where funccallback is a function pointer 2. function call in C++ test application m_oSarixrtsp->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. regards sindhu
sindhumahe wrote:
and i have succeeded in exporting them
Are really certain that you have succeeded? It seems not. First you need to ensure that your function is exported property.If you use the definition:
extern __declspec(dllexport) bool Callregistercallback(CRTSPWrapper* pObject,funccallback fptr_video,funccallback fptr_audio,void* ptr);
without appropriate definition of the function inside the library module definition file,or without setting configuration the compiler to compile the lib as C code you will be unable to call it using:
[DllImport("sampleclient_dll.dll", CharSet = CharSet.Ansi)]
static private extern bool Callregistercallback(IntPtr pTestClassObject,IntPtr fptr_video,IntPtr fptr_audio,IntPtr ptr);That happens because by default every function is compiled as C++ code not as C code and C++ uses names decoration to avoid conflicts between functions with the same names but with different arguments. Refer to this article for more details how to properly export your functions.
Life is a stage and we are all actors!