473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 16 '05 #1
11 11173
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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
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 16 '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 -
"ExecutionEngineException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:eS*************@tk2msftngp13.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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
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 16 '05 #3
Assaf,

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

<as*****@hotmail.com> wrote in message
news:ub**************@TK2MSFTNGP11.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 -
"ExecutionEngineException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:eS*************@tk2msftngp13.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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
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 16 '05 #4
check this out.
it has some code.

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

assaf

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

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

<as*****@hotmail.com> wrote in message
news:ub**************@TK2MSFTNGP11.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 -
"ExecutionEngineException".

assaf


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

wrote
in message news:eS*************@tk2msftngp13.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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
> 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 16 '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*****@hotmail.com> a écrit dans le message de
news:ub**************@TK2MSFTNGP11.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 -
"ExecutionEngineException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:eS*************@tk2msftngp13.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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
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 16 '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*************@TK2MSFTNGP09.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*****@hotmail.com> a écrit dans le message de
news:ub**************@TK2MSFTNGP11.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 -
"ExecutionEngineException".

assaf


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

wrote
in message news:eS*************@tk2msftngp13.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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
> 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 16 '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*************@TK2MSFTNGP09.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*****@hotmail.com> a écrit dans le message de
news:ub**************@TK2MSFTNGP11.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 -
"ExecutionEngineException".

assaf


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

wrote
in message news:eS*************@tk2msftngp13.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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
> 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 16 '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\cdp\Managed\ReadMe.txt
X:\...\SDK\v1.1\Tool Developers Guide\Samples\cdp\

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 ConsoleApplication.exe...: Stack
imbalance
may be caused by incorrect calling convention for method
TestFunc(win32dll.dll)"

I hope it helps

<as*****@hotmail.com> wrote in message
news:ub**************@TK2MSFTNGP11.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 -
"ExecutionEngineException".

assaf


"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:eS*************@tk2msftngp13.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**************@exisconsulting.com

"Yoni Rabinovitch" <yo**************@adc.com> wrote in message
news:d9**************************@posting.google.c om...
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 16 '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 16 '05 #10
I'm not too clear with what you said. To me, IJW is not available to C#.
Only to managed C++.
Am I wrong?

José
"Yoni Rabinovitch" <yo**************@adc.com> a écrit dans le message de
news:d9**************************@posting.google.c om...
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 16 '05 #11
"José Joye" <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> wrote in message news:<eY**************@TK2MSFTNGP12.phx.gbl>...
I'm not too clear with what you said. To me, IJW is not available to C#.
Only to managed C++.
Am I wrong?


No, you're right. However, what I meant is that I have unmanaged C++,
which I wrap with managed C++. The C# is only "aware" of the managed
C++.

Now, essentially what I'm doing is to get the unmanaged C++ to
callback the C# delegate asynchronously, without using any DllImports
in the plumbing for all this.
Nov 16 '05 #12

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

Similar topics

11
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...
13
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...
2
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...
11
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...
9
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...
0
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...
0
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...
3
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: //////////////...
4
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.