Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to run an unmanaged C++ application, this application must be connected to a library in C# (dll which when loading must show a form with a button) and wait for form close event to terminate.

While the form is loaded, at the moment of pressing the button the click event should be executed, which should execute a function that can send a number of return to the console application in C ++ and this application will show the resulting number in the output of the console.

What I need is to solve point 5. shown below, I read a lot of documents but almost all refer to the interoperation between a C++ dll and a C# application and not the reverse (Return an integer value from C# dll to a C++ console application) is what is needed here.

The execution must be dynamic, that is, while the form is displayed, a communication should be established from a function of the C# library and a function of the parent C++ console application.

All the examples that I have been able to find do almost the same, load the libraries, establish the communication and, from the C++ console application they call a function of the C# library and show the resulting return value and the application ends, but no example shows how to load the C# library, show the form, press a button, and send a integer number as a result to the C++ parent console application and show the result in the output of the console.

If you have any idea how to implement the Callback, I would be very grateful.

What I have tried:

What has been achieved is:

1. Load C++ console application (unmanaged), achieved
2. Load a C# library from the C++ console application, achieved
3. Import and communicate with some functions exported from the C# library, achieved
4. When loading the C# library, from this library a form with an button is shown, achieved
5. While the form is displayed, press the button and execute a function in C# that returns an integer to the C++ console application and print the result in the output of the console from the C++ console application, pending
Posted
Updated 14-Nov-18 7:51am
v2
Comments
Richard MacCutchan 14-Nov-18 9:46am    
This sounds like a lot of work to do something which could most likely be achieved more easily. Why not a C# console app that can communicate more easily with the library? Or a Windows Form app that starts a console app with the relevant number?
fisadmaster 14-Nov-18 9:55am    
@Richard MacCutchan, the requirement is established in this way because it needs to perform different operations between an unmanaged C++ application and a library in C# and it is necessary to test the interoperability between languages for future implementations
Richard MacCutchan 14-Nov-18 10:00am    
OK, then you need to set an event handler in the library that can call out to the console app. Which most likely will need some C++/CLI code in between.

You can find more information about this subject at Native and .NET Interoperability[^].
 
Share this answer
 
Comments
fisadmaster 14-Nov-18 10:11am    
You are right with C++/CLI code, But, I have already done a lot of examples but they are all of the static type, a call to a function in the C# library and a return value, but no example of how to expect a dynamic response, that is, more once for each execution, like a listener from unmanaged C++
Richard MacCutchan 14-Nov-18 10:19am    
I don't know of any other way to do what you want. It may be better to use a message passing service, or look at the possibility of using pipes.
Read my artcle about interoperability to understand the mechanismus.

You may add some additional layer like an addtional thread or some wait mechanismus like semaphores in the C++ app, which waites for the interaction and than runs further.

It sounds a bit weired, I think you really should reconsider the design - if it is possible. ;-)
 
Share this answer
 
Comments
fisadmaster 14-Nov-18 14:00pm    
Thanks for the comment but, I need the opposite of what is described in your article, I need to load a C # dll that shows a form and when clicking the button located in this form, an integer must be sent back to the C ++ application that has loaded the C # library and esdta should show the result in the console (I think it is callback from C ++ to C #).

You import the functions of C ++ to a C # application, I need to react from C # to unmanaged C ++
KarstenK 15-Nov-18 2:39am    
try: https://www.codeproject.com/articles/6916/how-to-load-a-dynamic-link-library-dll-into-a-micr

A problem may be that dll arent allowed to show UI.
fisadmaster 15-Nov-18 2:44am    
Thanks for the data, but the article is for C++ to C++ not C++ to C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900