managed wrapper for C++ dll

Hi,

I have a unmanaged C++ dll which i wants to wrap it so i can use it in my C# and vb.net apps.
But i am stucked at wrapping a function that accepts a C++ callback function

typedef int (*myCallbackFunc)(void*, const byteArray *);

how do i wrap this call in managed C++.

thanks
jaxrpcAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
>> but what i trying to achieve is to pass a delegate to a unmanged C++ function that accepts a pointer to function
I'm not sure you can do that (although I'm not 100% sure you can't either) since unmanaged code doesn't know about delegates. At some point you'll need to get from a delegate to a true function, but you can have your delegate do this, no? What I mean is, have the delegate call your unmanaged function and pass it a function pointer of an unmanaged function that does the work you need to do.
0
 
trinitrotolueneDirector - Software EngineeringCommented:
you can choose to expose unmanaged c++ functionality to your C# code....have you tried this?
0
 
trinitrotolueneDirector - Software EngineeringCommented:
for instance you can use the following pragmas to instruct the compiler

#pragma unmanaged
...
...
//unmanaged c++ code

#pragma managed
...
...
//managed code

ofcourse I presume you are compiling with the /clr option
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
trinitrotolueneDirector - Software EngineeringCommented:
0
 
jaxrpcAuthor Commented:
hi thanks for the links.

but is it possible to create a managed C++ function that takes in a delegate and pass it to C++ function that takes in a C style pointer to function.

I avoid using pinvoke when building the managed C++ bridge.

assuming that the unmanaged C++ function takes in a pointer to function

void setCallback(Callback *cb)

in my exposed function for managed code

void setCallback(Delegate_Callback cb)
{

}
0
 
evilrixSenior Software Engineer (Avast)Commented:
You know you can code managed and unmanaged together in one assembly using mixed mode assemblies and IJW (It Just works) right?

http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

"Mixed assemblies are capable of containing both unmanaged machine instructions and MSIL instructions. This allows them to call and be called by .NET components, while retaining compatibility with components that are entirely unmanaged. Using mixed assemblies, developers can author applications using a mixture of managed and unmanaged functionality. This makes mixed assemblies ideal for migrating existing Visual C++ applications to the .NET Platform."

Below is an example of IJW at work.This simple bit of code shows just how simple it is to use IJW to interface between managed and unmanaged code, no need to mess with GUIDs or other nasty COM things.
// C++ (all in one IJW assembly)
 
#include <iostream>
 
namespace UnmanagedCode
{
        void foo()
        {
                std::cout << "Hello, world" << std::endl;
        }
}
 
namespace ManagedCode
{
        public ref class fooWrapper
        {
        public:
                static void foo()
                {
                        UnmanagedCode::foo();
                }
        };
}
 
 
// C# code (seperate assembly)
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace scratchcs
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagedCode.fooWrapper.foo();
        }
    }
}

Open in new window

0
 
jaxrpcAuthor Commented:
hi,

thanks for info on IJW but what i trying to achieve is to pass a delegate to a unmanged C++ function that accepts a pointer to function. assuming that the unmanaged codes and managed codes are in the same file.
0
 
itsmeandnobodyelseCommented:
>>>>how do i wrap this call in managed C++.

IMO, you can't. Instead you should/could write the callback in unmanaged C++, let the callback function collect data to a global/shared variable/container, which then could be retrieved from managed C++, using a further wrapper function.
0
 
itsmeandnobodyelseCommented:
>>>> and pass it a function pointer of an unmanaged function that does the work you need to do.

Probably that suggestion of evilrix meant a similar approach than that I was proposing ;-)
0
 
jaxrpcAuthor Commented:
hi guys,
sorry for the late reply.

i heeded evilrix advice to pass an unmanaged function pointer to the unmanaged C++ library and in the unmanaged function i trigger a managed delegate. It works but i am not sure if this is a good practise.

I declared an unmanaged function with the same signature as the function pointer for callback

pass the unmanaged function to the unmanaged C++ library to registerCallback function.

in the unmanaged function i declared ... i invoked a .net delegate and passed whatever is passed to the unmanaged function to the .net delegate.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> It works but i am not sure if this is a good practise.
This is exactly the kind of reason Microsoft included IJW (It Just Works) into managed C++ :)

Without IJW you'd have to get invoked in a horrid mess of COM and other nasty things (shudder!)
0
All Courses

From novice to tech pro — start learning today.