Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
communication between native c++ thread in dll and c#
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

cody
GOOD ANSWER
hi i have the following scenario here:

i have a gui written in c#. the gui has to load a certain dll (written in
native c++) and call one function in it which starts a thread. this thread
runs during the whole time in the program.

the problem now is the gui has to communicate with this thread. i need to
send commands to the thread and the thread has to send results to the gui
code.

i already know how to call native code but how can i achieve this
communication after the thread has been started?

cody

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
David Stucki [MS]
GOOD ANSWER
If you were doing this same thing all in C++ how would you do it? A
possibility would be one thread would signal a shared syncronization object
that the worker thread was waiting on (or checking periodically) to do
something. I would get this working in straight C++. Then the C++ code
that signals the worker thread could be wrapped in a simple exported
function call, which you can then use from the managed code.

David Stucki
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
cody
GOOD ANSWER
[Original message clipped]

oh my god this sounds very complicated. my experience in threading in c++ is
so not good.

ok, if i want to c# gui from my c++ thread i have to call a delegate. but
how can i call managed code from native c++? and what do you mean with
"shared syncronization object"?

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Thomas Scheidegger [MVP] NETMaster (VIP)
GOOD ANSWER
> how can i call managed code from native c++?

also with a delegate.

Note, the C++ callback signature must have the __stdcall
calling convention, C# only supports this one.

I have prepared a VS.NET solution that shows exactly this:
http://dnetmaster.net/source/CSharpGuiCppThread.zip
[17KB size] (USE AT YOUR OWN RISK)

It contains two projects:
'CSharpApp' a C# Windows Forms application
'CppDll' a VC++/Win32 DLL

flow:

C# GUI Button => DLL API / C++ starts worker thread

and:

C# delegate <= callback from C++ worker thread
||
form.Invoke
||
C# GUI ProgressBar

finally:

C# GUI Button => DLL API / C++ stops worker thread

Just watch for deadlocks & reentrant code!

--
Thomas Scheidegger - MVP .NET - 'NETMaster'
http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
cody
GOOD ANSWER
[Original message clipped]

thank you a lot, Thomas, you are great!

cody

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
dagos dag
GOOD ANSWER
hi,

thanks very much, your sample code is exelent.
I use the DLL thread with a VB application and no problems.

That's really usefull!

bye

--------------------------------
From: dagos dag
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Thomas Scheidegger [MVP] NETMaster (VIP)
GOOD ANSWER
Also check:
http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.asp

http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.asp

--
Thomas Scheidegger - MVP .NET - 'NETMaster'
http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Windows.Forms.ProgressBar




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734