Ramazan Kula
Ramazan Kula

Reputation: 1

VS2010 Multiproject Solution - Console and Form Application

I have a VS2005 solution project which consist of two dependent project. This project is a C console application which communicates with a device and gets some outputs from it. This project is an old project and it does not written by me. I am an Electrical-Electronics Engineer and mostly I use C language for projects. I do not know so much about C++ and C#. For some time I am dealing with VS2010 and c++ form applications. I get the basics but I have some problems. I want to add gui for this console project. I have designed a form applicatiom for this purpose. I have buttons to start the process in console application and RichTextbox for outputs.

I must call a function inside console project from winform application by clicking a button. I want to call that function in a loop for continuous readings.

I have tried to call "console.exe" file and run it inside my winform project and redirect the outputs to richtextbox but it was too slow for my projects. I have to do continuous speedy reading from my device.

What is the best way to do this? I want to convert my C console application to windows form application.

Thank you.

Upvotes: 0

Views: 560

Answers (3)

Michael Edenfield
Michael Edenfield

Reputation: 28338

You cannot call a function inside of an EXE directly from another EXE.

You have several options here, depending on how much work you're willing to do and what the long-term goals of your old project is.

  1. Simply copy the relevant code from your console project to your forms project; this works best if your console project is being obsoleted and you no longer plan to support it.

  2. Move the relevant code from your console project into a library (DLL) project, and reference it from both your console and forms application; this works if the code in your console project is relatively isolated (e.g. you don't have a lot of global variables etc.)

  3. Add some kind of IPC mechanism to your console project (listen on a TCP socket or named pipe, etc.) that you can connect to from your forms project and get data directly; this is as close as you can get to your original goal of "call a function in one EXE from another EXE" but it's significantly more work.

Upvotes: 1

Ioan Bucur
Ioan Bucur

Reputation: 567

Typically you would move or convert your existing logic from the console app to a class library project. Then you would include a reference to this class library into your forms project. Next in your forms project you call your library methods (when you press a button for example) from another thread in order not to block your UI thread. ATTN! When updating your RichTextBox with the results you'll have to call the Invoke method to do the cross-thread operation (see here for a related topic).

Upvotes: 0

Marshall Conover
Marshall Conover

Reputation: 845

As C is a subset of C++, you should be able to compile your C source code from the CLR program into your C++ project, using it as a library that you can call directly from your gui. Check out this question for how to get started on doing this.

You should be able to use the C code almost exactly as it was used before, though it will most likely require some tweaking. Here's another related stack overflow question.

Upvotes: 0

Related Questions