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;
} 6 8532
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;
}
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
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
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
}
};
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rajesh |
last post by:
Dear All,
Please let me know the advantage of function pointer?
Is it fast calling function using function pointer?
Is it possible to use function pointer to optimise code?
Thanks and regards...
|
by: David Rose |
last post by:
I have a DLL (not .NET) that takes a function pointer argument and calls
that function with an integer argument. The DLL is being called from C#.
So far, it is partially working, but the...
|
by: bluejack |
last post by:
Ahoy... before I go off scouring particular platforms for specialized
answers, I thought I would see if there is a portable C answer to this
question:
I want a function pointer that, when...
|
by: Lloyd Dupont |
last post by:
I have some managed C++ interacting with native DLL,
for good integration I'm setting up some function pointer in the native DLL,
passing some function pointer from the managed world....
|
by: WittyGuy |
last post by:
How to typecast a "function pointer" to "const void*" type in C++ way?
int MyFunction (double money); // Function prototype
const void* arg = (const void*)MyFunction; // type casting...
| |
by: Felix Kater |
last post by:
Hi,
I can compile and run this code (see below) which twice calls the
function f, first with too less, second with too much arguments.
But is it legal and free of memory leaks and other...
|
by: Jonathan Green |
last post by:
I have some questions about the behavior of the code below.
#include <stdio.h>
void test(void *v);
int main(void)
{
int i=1;
int j=2;
|
by: aruna.mysore |
last post by:
Hi all,
I have a specific problem passing a function pointer array as a
parameter to a function. I am trying to use a function which takes a
function pointer array as an argument. I am too sure...
|
by: MikeC |
last post by:
Folks,
I've been playing with C programs for 25 years (not professionally -
self-taught), and although I've used function pointers before, I've never
got my head around them enough to be able to...
|
by: Immortal_Nephi |
last post by:
I had a lot of research to see how function pointer works. Sometimes,
programmers choose switch keyword and function in each case block can
be called. Sometimes, they choose ordinary function...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |