473,801 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoke C# delegate asynchronously from unmanaged C++

Is it possible to invoke a C# delegate/event handler asynchronously,
from unmanaged C++ ?

I assume this requires a Managed C++ wrapper, which would call
BeginInvoke on the delegate ?

Is this correct, and if so, does anyone have an example ?

Thanks !!
Nov 15 '05 #1
11 2340
Yoni,

It is not possible from unmanaged C. You will either have to use the
#pragma managed and #pragma unmanaged directives to enter into managed code
and make the call, or you will have to marshal the delegate as a function
pointer, in which case you would have to call it asynchronously, creating
your own thread, etc, etc. Or, you could export the type as a COM object
and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
Is it possible to invoke a C# delegate/event handler asynchronously,
from unmanaged C++ ?

I assume this requires a Managed C++ wrapper, which would call
BeginInvoke on the delegate ?

Is this correct, and if so, does anyone have an example ?

Thanks !!

Nov 15 '05 #2
Nicholas, maybe i don't understand you,

but, I'm doing it!

However, my app crashes after about 15 minutes of doing it intesively.

I fear that since i am not locking down the manged code in any way,
it gets moved around.
The Unmanaged code not being aware of this change, invokes the Managed code.
However it is using the wrong address. And this leads to a crash -
"ExecutionEngin eException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote
in message news:eS******** *****@tk2msftng p13.phx.gbl...
Yoni,

It is not possible from unmanaged C. You will either have to use the
#pragma managed and #pragma unmanaged directives to enter into managed code and make the call, or you will have to marshal the delegate as a function
pointer, in which case you would have to call it asynchronously, creating
your own thread, etc, etc. Or, you could export the type as a COM object
and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
Is it possible to invoke a C# delegate/event handler asynchronously,
from unmanaged C++ ?

I assume this requires a Managed C++ wrapper, which would call
BeginInvoke on the delegate ?

Is this correct, and if so, does anyone have an example ?

Thanks !!


Nov 15 '05 #3
Assaf,

How are you doing it? Which method are you using?
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

<as*****@hotmai l.com> wrote in message
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
Nicholas, maybe i don't understand you,

but, I'm doing it!

However, my app crashes after about 15 minutes of doing it intesively.

I fear that since i am not locking down the manged code in any way,
it gets moved around.
The Unmanaged code not being aware of this change, invokes the Managed code. However it is using the wrong address. And this leads to a crash -
"ExecutionEngin eException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote in message news:eS******** *****@tk2msftng p13.phx.gbl...
Yoni,

It is not possible from unmanaged C. You will either have to use the #pragma managed and #pragma unmanaged directives to enter into managed

code
and make the call, or you will have to marshal the delegate as a function pointer, in which case you would have to call it asynchronously, creating your own thread, etc, etc. Or, you could export the type as a COM object and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
Is it possible to invoke a C# delegate/event handler asynchronously,
from unmanaged C++ ?

I assume this requires a Managed C++ wrapper, which would call
BeginInvoke on the delegate ?

Is this correct, and if so, does anyone have an example ?

Thanks !!



Nov 15 '05 #4
check this out.
it has some code.

in the visual studio .net:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033/cpguide/html/cpconcallbacksa m
ple.htm

assaf

"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote
in message news:uK******** ******@TK2MSFTN GP11.phx.gbl...
Assaf,

How are you doing it? Which method are you using?
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

<as*****@hotmai l.com> wrote in message
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
Nicholas, maybe i don't understand you,

but, I'm doing it!

However, my app crashes after about 15 minutes of doing it intesively.

I fear that since i am not locking down the manged code in any way,
it gets moved around.
The Unmanaged code not being aware of this change, invokes the Managed

code.
However it is using the wrong address. And this leads to a crash -
"ExecutionEngin eException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com>

wrote
in message news:eS******** *****@tk2msftng p13.phx.gbl...
Yoni,

It is not possible from unmanaged C. You will either have to use the #pragma managed and #pragma unmanaged directives to enter into managed

code
and make the call, or you will have to marshal the delegate as a function pointer, in which case you would have to call it asynchronously, creating your own thread, etc, etc. Or, you could export the type as a COM object and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
> Is it possible to invoke a C# delegate/event handler asynchronously,
> from unmanaged C++ ?
>
> I assume this requires a Managed C++ wrapper, which would call
> BeginInvoke on the delegate ?
>
> Is this correct, and if so, does anyone have an example ?
>
> Thanks !!



Nov 15 '05 #5
I do not know if I'm off topic. However, I had a crash problem (after a
about 1000 calls) when using a callback from C to C# (having delegate in
C#).
The problem was in the C declaration. Here is a correct sample (watch for
the 2nd WINAPI)

// in C header file:
DLLEXPORT void WINAPI PassExitFnPtr (void (WINAPI * pExit)(int exit_code));

José

<as*****@hotmai l.com> a écrit dans le message de
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
Nicholas, maybe i don't understand you,

but, I'm doing it!

However, my app crashes after about 15 minutes of doing it intesively.

I fear that since i am not locking down the manged code in any way,
it gets moved around.
The Unmanaged code not being aware of this change, invokes the Managed code. However it is using the wrong address. And this leads to a crash -
"ExecutionEngin eException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote in message news:eS******** *****@tk2msftng p13.phx.gbl...
Yoni,

It is not possible from unmanaged C. You will either have to use the #pragma managed and #pragma unmanaged directives to enter into managed

code
and make the call, or you will have to marshal the delegate as a function pointer, in which case you would have to call it asynchronously, creating your own thread, etc, etc. Or, you could export the type as a COM object and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
Is it possible to invoke a C# delegate/event handler asynchronously,
from unmanaged C++ ?

I assume this requires a Managed C++ wrapper, which would call
BeginInvoke on the delegate ?

Is this correct, and if so, does anyone have an example ?

Thanks !!



Nov 15 '05 #6
BTW, I had a second problem as well.
You have to make sure that you keep a live reference to your delegate in
your C# code (this has to be kept as long as the callback could be fired)

José

"José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> a écrit dans le
message de news:e5******** *****@TK2MSFTNG P09.phx.gbl...
I do not know if I'm off topic. However, I had a crash problem (after a
about 1000 calls) when using a callback from C to C# (having delegate in
C#).
The problem was in the C declaration. Here is a correct sample (watch for
the 2nd WINAPI)

// in C header file:
DLLEXPORT void WINAPI PassExitFnPtr (void (WINAPI * pExit)(int exit_code));
José

<as*****@hotmai l.com> a écrit dans le message de
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
Nicholas, maybe i don't understand you,

but, I'm doing it!

However, my app crashes after about 15 minutes of doing it intesively.

I fear that since i am not locking down the manged code in any way,
it gets moved around.
The Unmanaged code not being aware of this change, invokes the Managed

code.
However it is using the wrong address. And this leads to a crash -
"ExecutionEngin eException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com>

wrote
in message news:eS******** *****@tk2msftng p13.phx.gbl...
Yoni,

It is not possible from unmanaged C. You will either have to use the #pragma managed and #pragma unmanaged directives to enter into managed

code
and make the call, or you will have to marshal the delegate as a function pointer, in which case you would have to call it asynchronously, creating your own thread, etc, etc. Or, you could export the type as a COM object and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
> Is it possible to invoke a C# delegate/event handler asynchronously,
> from unmanaged C++ ?
>
> I assume this requires a Managed C++ wrapper, which would call
> BeginInvoke on the delegate ?
>
> Is this correct, and if so, does anyone have an example ?
>
> Thanks !!



Nov 15 '05 #7
actually your problem sounds very similar to mine.
i am actually running a test now.
with your modification!
if this works, it will end 3 months of chasing this bug.

tnx

assaf

"José Joye" <jose.joye@__No _SPam__bluewin_ _maPS_oN__.ch> wrote in message
news:e5******** *****@TK2MSFTNG P09.phx.gbl...
I do not know if I'm off topic. However, I had a crash problem (after a
about 1000 calls) when using a callback from C to C# (having delegate in
C#).
The problem was in the C declaration. Here is a correct sample (watch for
the 2nd WINAPI)

// in C header file:
DLLEXPORT void WINAPI PassExitFnPtr (void (WINAPI * pExit)(int exit_code));
José

<as*****@hotmai l.com> a écrit dans le message de
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
Nicholas, maybe i don't understand you,

but, I'm doing it!

However, my app crashes after about 15 minutes of doing it intesively.

I fear that since i am not locking down the manged code in any way,
it gets moved around.
The Unmanaged code not being aware of this change, invokes the Managed

code.
However it is using the wrong address. And this leads to a crash -
"ExecutionEngin eException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com>

wrote
in message news:eS******** *****@tk2msftng p13.phx.gbl...
Yoni,

It is not possible from unmanaged C. You will either have to use the #pragma managed and #pragma unmanaged directives to enter into managed

code
and make the call, or you will have to marshal the delegate as a function pointer, in which case you would have to call it asynchronously, creating your own thread, etc, etc. Or, you could export the type as a COM object and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
> Is it possible to invoke a C# delegate/event handler asynchronously,
> from unmanaged C++ ?
>
> I assume this requires a Managed C++ wrapper, which would call
> BeginInvoke on the delegate ?
>
> Is this correct, and if so, does anyone have an example ?
>
> Thanks !!



Nov 15 '05 #8
Hello,

Whenever doing p/invoke or interop under .NET 1.1+,
I suggest to use CLR SPY:
http://blogs.gotdotnet.com/anathan/P...b-8448824b8267

This uses CDP - Customer Debug Probes. More information about these probes
can be found at:
X:\...\SDK\v1.1 \Tool Developers Guide\Samples\c dp\Managed\Read Me.txt
X:\...\SDK\v1.1 \Tool Developers Guide\Samples\c dp\

These probes will help you find marshalling related bugs much faster. For
example,
let's say there is a calling convention mismatch. Here is what you can get
with CLR SPY
logging:
"...PInvoke Calling Convention Mismatch in ConsoleApplicat ion.exe...: Stack
imbalance
may be caused by incorrect calling convention for method
TestFunc(win32d ll.dll)"

I hope it helps

<as*****@hotmai l.com> wrote in message
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
Nicholas, maybe i don't understand you,

but, I'm doing it!

However, my app crashes after about 15 minutes of doing it intesively.

I fear that since i am not locking down the manged code in any way,
it gets moved around.
The Unmanaged code not being aware of this change, invokes the Managed code. However it is using the wrong address. And this leads to a crash -
"ExecutionEngin eException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote in message news:eS******** *****@tk2msftng p13.phx.gbl...
Yoni,

It is not possible from unmanaged C. You will either have to use the #pragma managed and #pragma unmanaged directives to enter into managed

code
and make the call, or you will have to marshal the delegate as a function pointer, in which case you would have to call it asynchronously, creating your own thread, etc, etc. Or, you could export the type as a COM object and use that, calling the BeginInvoke method yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Yoni Rabinovitch" <yo************ **@adc.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
Is it possible to invoke a C# delegate/event handler asynchronously,
from unmanaged C++ ?

I assume this requires a Managed C++ wrapper, which would call
BeginInvoke on the delegate ?

Is this correct, and if so, does anyone have an example ?

Thanks !!




Nov 15 '05 #9
Thanks, everyone, for your input.

I should have mentioned that a central requirement of mine was to be
able to do this without any DllImport stuff, just straight IJW.

In the end, I found an (albeit convoluted) way of doing this, at
http://www.codeproject.com/managedcpp/cbwijw.asp.

This shows how to synchronously invoke a C# delegate via a callback
from unmanaged C++, without any DllImports.

It is reasonably straightforward to extend this example to cover
asynchronous invocation (BeginInvoke... EndInvoke).
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
7437
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a method to an IntPtr? / Christian
2
3009
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method with arguments I need to do this as the method is being called Asynchronously from elsewhere using delegate's BeginInvoke method. The form is well created/initialized yet the messages states otherwise.
11
11205
by: Yoni Rabinovitch | last post by:
Is it possible to invoke a C# delegate/event handler asynchronously, from unmanaged C++ ? I assume this requires a Managed C++ wrapper, which would call BeginInvoke on the delegate ? Is this correct, and if so, does anyone have an example ? Thanks !!
11
9084
by: Florian A. | last post by:
Hi everyone! I'm trying to write a global hook in C# and C++/CLI. I'm almost done if there wasn't this little delegate problem. I get the function pointer to a delegate and pass it to my hook.dll. The delegate gets called for a few seconds but then the delegate seems to be relocated on the CLR heap. If tried using a GCHandle but as far as I can tell this only prevents the garbage collector from collecting my delegate and doesn't stop...
9
1972
by: Terry Olsen | last post by:
I'm running an asynchronous Socket. In the ReceiveCallback method, I need to append what is received to a textbox on the main form. I have this code: Private Sub ToChatWindow(ByVal msg As String) txtChat.AppendText(msg) End Sub I've never used a delegate or invoke before and I'm rather confused on the topic. Hope someone can help.
0
2019
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
0
955
by: rubino57 | last post by:
Hi folks, It turns out that in the snippet below, the MCPCallback sometimes gets called from the unmanaged DLL. (ie. the messageBox you see inside MCPCallback). Ths callback is called asynchronously by a different thread from the unmanaged DLL. I would greatly appreciate any pointers?
3
1734
by: CAIBird | last post by:
hi, i'v been coding with c++/cli and i hit a terrible problem. i created a class library project and add a ref class(abstract) with some static member functions like: ////////////// CrawlerWrapper.h public ref class CrawlerWrapper abstract sealed {
4
4234
by: igd | last post by:
I am forced to decorate my .NET Main()-method with , since an unmanaged function (which i am calling through P/Invoke) calls CoCreateInstance() in turn. The problem is, that another unmanaged function starts a new thread which invokes a callback function. This callback function is a delegate in my .NET app and since it is executed in a STA (Singe Threaded Appartment), the callback function/delegate can not called right. Is there a...
0
9698
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10300
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10279
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9106
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7595
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6834
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5492
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2963
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.