Replacing std::cout output with windows form - c++

I made a program in c++ (visual studio 2010) that looks for serial com ports and compares their friendly names with defined text. When there is a match that port is opened/connected and serial communication starts.
The program notifies the user when a com port is found, whether the connection was successful or not and if the data send was successful or not and other useful information. The program uses cout to notify the user.
I want to replace console output window with windows form but cant find much resources online on how to do this. To illustrate, I want this:
To become this:
I included form1.h and other files and tried replacing cout with below line but code is not compiling:
Form1::textBox1->Text = L" Text I want to display";
Can anyone explain how to use textBox1, or a tutorial for this?

"I included form1.h" - You can't just grab random files and hope it works. That is not how C++ works, or computers in general.
How then do you do something like this? The Standard Library provides std::cout and Visual Studio by default includes the Standard Library, so using it is fairly easy. But for graphics, you will need another library. I recommend Qt, if only because there are good tutorials for beginners.

So I finally did achieve the functionality I described above in my question and thought I should post my findings here.
To convert my code from console output to Windows Form I basically had to migrate from c++ to C++/cli.
holowczak.com has a great tutorial on how to get started with windows form (c++/cli) in visual studio.
Next if there is any busy looping(like infinite while-loop) in your c++ code then you would need to run that busy loop on a separate thread or the program can hang. Dr.Dobb's tutorial on how to create and mange threads in c++/cli, can help a lot.
Finally, if you need to access resources (such as textbox and other controls) of windows form from another thread then a thread-safe call has to be made. Microsoft's "HowTo:Make thread-safe call to windows Form controls" explains how the invoke method can be used for updating textbox from another thread.

Related

Is it possible for a DLL to open explorer?

Im trying to make (what I thought was a simple) extension for Game maker studio 2.
I am restricted to making a DLL app.
I am wondering is there any was to have a dll app open the file explorer have the user locate a file and then return said directory?
I fell like this is a sumb question but one I really need to know the answer too before slaving away coding for hours only to find its not possible.
You do not want to launch the explorer but to open a file dialog that allows the user to select a file.
Depending on the framework you use in your program the solutions may differ.
If you are using Qt framework you may use a QFileDialog for a platform independent mechanism.
If you are okay that it will only works on Windows then you may directly use the WinAPI functions GetOpenFileName or GetSaveFileName (that is a lot easier than the Common Item Dialog that is suggested as replacement on their documentation pages)
On GameMaker terms, you want to use get_open_filename or get_open_filename_ext.
See Dialog Module (marketplace, github) for C++ implementation reference.

Integrating C++ Code with a GUI

I am trying to implement a security system as part of my Microcontrollers term project. I am almost done with the software end of things. I just need to design a GUI that will serve as an indicator or an alarm device for my security system. The microcontroller makes use of the sensing circuitry to check if the system has been breached and it sends the alarm signal serially to a PC. I am reading data off the PC serial port using C++ and the whole thing works wonderfully. However, I am doing it in a console based environment. That's all I have ever known. I am studying to be an Electronic Engineer. :P
That's not acceptable, however, and I need to design a GUI for my project.
Question 1:
Is there anyway I can integrate my existing code with a GUI? I have googled this already without much luck. All the answers seem to point in the direction of external libraries like Qt etc. This is kind of scary because I don't have a lot of time.
Question 2:
As I mention earlier, I am kind of intimidated by the thought of having to use external libraries etc. so I tried to make a GUI on Visual Studio using Visual C++ based Windows Form Applications. I used the serialport utility from the Toolbox. I haven't been able to read data off the COM port that way but I can write data to it just fine.
This is what the form looks like:
I am trying to display the data read from the serial port on a textbox in response to a button press. Here's the code I am using for the button:
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e)
{}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
this->serialPort1->PortName = "COM1";
this->serialPort1->Open();
this->serialPort1->BaudRate = 9600;
try
{
this->textBox1->Text=this->serialPort1->ReadLine();
}
catch(TimeoutException^)
{
this->textBox1->Text="Timeout Exception";
}
this->serialPort1->Close();
Can you tell me what I am doing wrong? I am new to this. This is my preferred method, btw.
Using Windows Forms is definitely the way to go if you don't want to use external libraries, that's how I'd have done it too.
I'm not exactly into these things, but using ReadLine() just once could be a possible source of the problem. Maybe the data coming in over the serial port is sent over more than just one line. Perhaps you'll have to loop through the input, continuously using ReadLine() and collecting the lines in an array until all data has been received.
But perhaps there's also another command, something like ReadAllLines(), which reads all the input at once and returns it to you?
My immediate suspicion is the ReadLine() command - are you sure your MC is writing the endline characters?
Use ReadFile instead and see if you can read single bytes. If not, then maybe you need to adjust more options. If you get to the timeout exception, then you will need to adjust the read timeout (SetCommTimeouts).
Everything you need was described very well by Allen Denver here: http://msdn.microsoft.com/en-us/library/ff802693.aspx
As for which GUI, if whatever Windows Forms project you're using works, then stick with it. I personally used MFC for a similar final year project, but that was because I was already familiar with it.
For next time though, learn C#. C# and Java are far, far easier and quicker for creating GUIs.
As for your question 1, I recommend you to use the MFC library, it is quite easy. Here is a good example for you to start.
http://depts.washington.edu/cmmr/biga/chapter_tutorials/1.C++_MFC_D3DOGL/1.StepByStepGuide/index.html
And based on what I understand, You are already able to read and write the data in console based environment. The GUI is just used to enter or display the data. In this case, you could program a MFC project easily which serves your purpose.
And for your 2nd question, if you tried to display the data read from the serial port on a edit box in response to a button press. You should first add a variable to the edit box, for example edit_box_value. And in that button clicked function, implement a code like following:
edit_box_value.Format(_T("%s"), data_to_be_displayed);
UpdateData(FALSE);

Prevent mixing of console output and written text

I have a C++ console application that prints some output constantly while it also accepts commands (using std::cin) from the user - output and input happen in separate threads.
If I write a text while some output appears the written text is mixed with application output. How can I prevent this behaviour?
To solve this problem, I need to display the program one line above the line where the text is typed. I'd inspire myself in Minecraft Bukkit server's solution - however I need the same for C++.
Assuming you want the output to appear while things are being typed, you'll need some screen control facilities to have the output go somewhere different than the input area. If I were tasked to implement something like this writing to a terminal I would refresh my ncurses experience. I realize you are on a Windows console and I have no idea if the Windows console is capable of the screen control needed to make it happen.
You can possibly tie custom stream buffers into std::cin and std::cout using the curses functionality under the hood but it may not be worth it. In any case, it isn't entirely trivial.
There's a windows port of ncurses called pdcurses. But if you are using visual studio there's a simple function provided called SetConsoleCursorPosition()

Ahk script and C++ communication

I wish to use the functions of autohotkey within a C++ program.
I am currently running my scripts triggered by the c++ program- I just run them as a .bat file. This works well but the problem is that I cannot return values from the script to the c++ program.
I wish to be able to read the position of the mouse from the script and make decisions based upon this in my C++ program. My scripts do quite complex things- so doing this in autohotkey is the best solution for me- I have knowledge of C, but little of C++.
I have read about the Autohotkey .DLL - I know how to trigger it but not how to read values from it. If anyone could instruct me or even post example code of a .dll being loaded and a value sent to a script and a value returned- I would be eternally grateful!!
I have spent hours on this and to no avail!
to return a value, could this possibly work http://www.autohotkey.net/~tinku99/ahkdll/functions/ahkgetvar.htm
I'm not sure about the dll, but you could just write your own application in Autohotkey and package it along with your C++.
The communication takes place over a hidden window with an edit control and a button. You use one application to set text in the edit box, and then to click the submit button. The other application owns the window can process whatever is put into the edit control - as if you were passing a variable. Basically, that's it.
Check out this thread where I've explained it in more detail: How to send a command to a running application via commandline
Now, that's not quite what you wanted, but the effect is the same, and you already know all the api.

How to launch an external application on BN_CLICKED?

I'm fairly new to Windows programming. I'm doing a simple launcher app for WinCE using VC++ (not MFC). So far I've created the basic interface and buttons and stuff. I just wanted to know the best way to launch an external application when the user clicks the button (on BN_CLICKED).
I found some methods such as ShellExecute, CreateProcess and others. But I couldn't get it to work (yet?). Any suitable reference or simple example on this?
The question don't matter that it happens inside the event of a button click, but...
ShellExecute is a good way to start programs (and the default program for any other type of files) in Windows, but use CreateProcess if you need a return code, or if you need the ability to wait for the program to finish.
Nevermind. I found a working example on MSDN - a comment by a user.
For anyone interested, you can go to this CreateProcess() article and scroll down to a comment entitled "Working code for creating two processes "p1.exe" and "p2.exe" using CreateProcess() Method"
Thanks!