By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Calling C++ function pointer from C#

P: n/a
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;
}
Nov 19 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
>
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;
}

Nov 19 '08 #2

P: n/a
On Nov 19, 9:54*am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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 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
Nov 19 '08 #3

P: n/a
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/libr...82(VS.80).aspx
"sasha" <ab*********@gmail.comwrote in message
news:ce**********************************@v16g2000 prc.googlegroups.com...
On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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.
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

Nov 19 '08 #4

P: n/a
sasha wrote:
On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>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?

Thanks
Well that's actually a slightly different problem. There's
System::Runtime::InteropServices::Marshal::GetFunc tionPointerForDelegate
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
}
};
Nov 19 '08 #5

P: n/a
On Nov 19, 4:55 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
sasha wrote:
On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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?
Thanks

Well that's actually a slightly different problem. There's
System::Runtime::InteropServices::Marshal::GetFunc tionPointerForDelegate
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
Nov 20 '08 #6

P: n/a
sasha wrote:
On Nov 19, 4:55 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>sasha wrote:
>>On Nov 19, 9:54 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam>
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?
>>Thanks

Well that's actually a slightly different problem. There's
System::Runtime::InteropServices::Marshal::GetFun ctionPointerForDelegate
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.
Nov 20 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.