Calling C++ function pointer from C#

S

sasha

I have a c++ code that callls csharp. Now I want to be able to pass
a function pointer from C++ to Csharp code and have c# callback to it.
Is it possible and how?

Here is what I have so far :
#include "windows.h"
#include <stdio.h>
#import "CSDll.tlb" named_guids

int main(int argc, char* argv[])
{
HRESULT hRes = S_OK;
CoInitialize(NULL);
CSDll::IMyManagedInterface *pManagedInterface = NULL;

hRes = CoCreateInstance(CSDll::CLSID_Class1, NULL,
CLSCTX_INPROC_SERVER,
CSDll::IID_IMyManagedInterface, reinterpret_cast<void**>
(&pManagedInterface));

if (S_OK == hRes)
{
hRes = pManagedInterface->raw_Run();
pManagedInterface->Release();
}

CoUninitialize();
return 0;
}
 
B

Ben Voigt [C++ MVP]

sasha said:
I have a c++ code that callls csharp. Now I want to be able to pass
a function pointer from C++ to Csharp code and have c# callback to it.
Is it possible and how?

Do you have a special reason for using COM interop instead of C++ interop?
With C++ interop, you can create a delegate in C++/CLI and pass it to the C#
code.
Here is what I have so far :
#include "windows.h"
#include <stdio.h>
#import "CSDll.tlb" named_guids

int main(int argc, char* argv[])
{
HRESULT hRes = S_OK;
CoInitialize(NULL);
CSDll::IMyManagedInterface *pManagedInterface = NULL;

hRes = CoCreateInstance(CSDll::CLSID_Class1, NULL,
CLSCTX_INPROC_SERVER,
CSDll::IID_IMyManagedInterface, reinterpret_cast<void**>
(&pManagedInterface));

if (S_OK == hRes)
{
hRes = pManagedInterface->raw_Run();
pManagedInterface->Release();
}

CoUninitialize();
return 0;
}
 
S

sasha

sasha said:
I have a c++ code that  callls csharp. Now I want to be able to pass
a function pointer from C++ to Csharp code and have c# callback to it.
Is it possible and how?

Do you have a special reason for using COM interop instead of C++ interop?
With C++ interop, you can create a delegate in C++/CLI and pass it to theC#
code.


Here is what I have so far :
#include "windows.h"
#include <stdio.h>
#import "CSDll.tlb" named_guids
int main(int argc, char* argv[])
{
   HRESULT hRes = S_OK;
   CoInitialize(NULL);
   CSDll::IMyManagedInterface *pManagedInterface = NULL;
   hRes = CoCreateInstance(CSDll::CLSID_Class1, NULL,
CLSCTX_INPROC_SERVER,
    CSDll::IID_IMyManagedInterface, reinterpret_cast<void**>
(&pManagedInterface));
   if (S_OK == hRes)
   {
       hRes = pManagedInterface->raw_Run();
       pManagedInterface->Release();
   }
    CoUninitialize();
return 0;
  }

I have no reason, frankly speaking. How would I do that. please
provide the reference to it. I have lots of legacy code, as a back-
end, that heavily relies on function pointers. And I want to call
those from my C# code. Any suggestion or examples of that?

Thanks
 
S

Shekhar

You have 3 options:

Using Explicit PInvoke in C++ (DllImport Attribute)

Using C++ Interop (Implicit PInvoke)

A Closer Look at Platform Invoke

Read more here: http://msdn.microsoft.com/en-us/library/ms235282(VS.80).aspx


sasha said:
I have a c++ code that callls csharp. Now I want to be able to pass
a function pointer from C++ to Csharp code and have c# callback to it.
Is it possible and how?

Do you have a special reason for using COM interop instead of C++ interop?
With C++ interop, you can create a delegate in C++/CLI and pass it to the
C#
code.


Here is what I have so far :
#include "windows.h"
#include <stdio.h>
#import "CSDll.tlb" named_guids
int main(int argc, char* argv[])
{
HRESULT hRes = S_OK;
CoInitialize(NULL);
CSDll::IMyManagedInterface *pManagedInterface = NULL;
hRes = CoCreateInstance(CSDll::CLSID_Class1, NULL,
CLSCTX_INPROC_SERVER,
CSDll::IID_IMyManagedInterface, reinterpret_cast<void**>
(&pManagedInterface));
if (S_OK == hRes)
{
hRes = pManagedInterface->raw_Run();
pManagedInterface->Release();
}
CoUninitialize();
return 0;
}

I have no reason, frankly speaking. How would I do that. please
provide the reference to it. I have lots of legacy code, as a back-
end, that heavily relies on function pointers. And I want to call
those from my C# code. Any suggestion or examples of that?

Thanks
 
B

Ben Voigt [C++ MVP]

sasha said:
Do you have a special reason for using COM interop instead of C++
interop? With C++ interop, you can create a delegate in C++/CLI and
pass it to the C# code.
[snip]
I have no reason, frankly speaking. How would I do that. please
provide the reference to it. I have lots of legacy code, as a back-
end, that heavily relies on function pointers. And I want to call
those from my C# code. Any suggestion or examples of that?

Thanks

Well that's actually a slightly different problem. There's
System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate
that helps with that in p/invoke. And it seems there's
GetDelegateForFunctionPointer for the case you originally mentioned.

For C++ interop, you don't have to do anything special at all. You just
compile your code with C++/CLI and call your legacy code normally. If you
want a C++/CLI function to be usable by C#, put it into a "ref class".

trivial example:

#include <cmath>
ref class Wrapper // ref class, C# can use it
{
public:
static double ComputeIt()
{
return System::Math::Sin(0.2) + std::sin(0.3); //
see, it can both call C# and legacy C++ code in the same function
}
};
 
S

sasha

sasha said:
sasha wrote:
I have a c++ code that callls csharp. Now I want to be able to pass
a function pointer from C++ to Csharp code and have c# callback to
it. Is it possible and how?
Do you have a special reason for using COM interop instead of C++
interop? With C++ interop, you can create a delegate in C++/CLI and
pass it to the C# code.
[snip]
I have no reason, frankly speaking. How would I do that. please
provide the reference to it. I have lots of legacy code, as a back-
end, that heavily relies on function pointers. And I want to call
those from my C# code. Any suggestion or examples of that?

Well that's actually a slightly different problem. There's
System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate
that helps with that in p/invoke. And it seems there's
GetDelegateForFunctionPointer for the case you originally mentioned.

For C++ interop, you don't have to do anything special at all. You just
compile your code with C++/CLI and call your legacy code normally. If you
want a C++/CLI function to be usable by C#, put it into a "ref class".

trivial example:

#include <cmath>
ref class Wrapper // ref class, C# can use it
{
public:
static double ComputeIt()
{
return System::Math::Sin(0.2) + std::sin(0.3); //
see, it can both call C# and legacy C++ code in the same function
}

};


Wait, so from legacy code, I call managed c++ DLL, from the manage
DLL I call Csharp DLL.

is there an example of that as a starting point? thanks
 
B

Ben Voigt [C++ MVP]

sasha said:
sasha said:
On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <[email protected]>
wrote:
sasha wrote:
I have a c++ code that callls csharp. Now I want to be able to
pass a function pointer from C++ to Csharp code and have c#
callback to it. Is it possible and how?
Do you have a special reason for using COM interop instead of C++
interop? With C++ interop, you can create a delegate in C++/CLI and
pass it to the C# code.
[snip]
I have no reason, frankly speaking. How would I do that. please
provide the reference to it. I have lots of legacy code, as a back-
end, that heavily relies on function pointers. And I want to call
those from my C# code. Any suggestion or examples of that?

Well that's actually a slightly different problem. There's
System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate
that helps with that in p/invoke. And it seems there's
GetDelegateForFunctionPointer for the case you originally mentioned.

For C++ interop, you don't have to do anything special at all. You
just compile your code with C++/CLI and call your legacy code
normally. If you want a C++/CLI function to be usable by C#, put it
into a "ref class".

trivial example:

#include <cmath>
ref class Wrapper // ref class, C# can use it
{
public:
static double ComputeIt()
{
return System::Math::Sin(0.2) + std::sin(0.3);
// see, it can both call C# and legacy C++ code in the same function
}

};


Wait, so from legacy code, I call managed c++ DLL, from the manage
DLL I call Csharp DLL.

is there an example of that as a starting point? thanks

I just gave you an example. You need to make sure any managed libraries you
need to call are added as references, native libraries you use header files
and static libraries or import libraries as usual. There's nothing
special... throw a call to qsort into that example and the C++/CLI compiler
will automatically make the callback function pointer compatible with native
qsort. C++ interop is also called "It Just Works" :)

Unless I'm misunderstanding your requirement. I think you want C# to call
C++/CLI, C++/CLI passes callback function pointers to legacy C++ libraries,
legacy C++ libraries calls through the function pointer back to C++/CLI
which can call other C++/CLI, legacy C++, C#, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top