Link to home
Start Free TrialLog in
Avatar of jaxrpc
jaxrpc

asked on

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
Avatar of trinitrotoluene
trinitrotoluene
Flag of Australia image

you can choose to expose unmanaged c++ functionality to your C# code....have you tried this?
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
Avatar of jaxrpc
jaxrpc

ASKER

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)
{

}
Avatar of evilrix
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

Avatar of jaxrpc

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>>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.
>>>> 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 ;-)
Avatar of jaxrpc

ASKER

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.
>> 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!)