How can I pass information from a launcher to my program? - c++

Here is the deal : I have created a data transfer program that sends info to my Arduino over Serial Port. I have also created a simple "launcher" program with a basic UI for the user to Select the COM Port and launch the program. It is basically just a Windows Forms Application with a DropDown Combo Box for the COM Port and a "Launch" Button that starts the Transfer Program.
The Launcher and the Transfer Program are separate, so I need a way for the Transfer Program to get the information of the Serial Port the user chooses on the Launcher before starting the program.
I've looked into config files, shared txt files etc. but found nothing but overly-complicated programs.
Any help would be appreciated!
Thanks,
Frazic

There are some ways to do that.
1. Start your program with parameters using int main(int argc, char* argv[])
For more Information: How to parse command line parameters.
And you can run your tool with: system("C:\\Program Files (x86)\\MyProgram\\transfer.exe ParamCOM");
2. Put your transfer program in to a dll and call it from your GUI. With that solution you could transfer data in both directions while the transfer program is running. Walkthrough: Creating and Using a Dynamic Link Library (C++)
3. Write the COM information from your GUI in a config.txt file before you call the transfer program. Now your transfer program could read the information from the file.
4. Read memory from a different process using WinAPI. That is not a common way but it works. Searching for the right values is not that easy so I would prefer Point 1 - 3.

You can pass the data as commandline arguments.
You can pass the data in the environment.
You can pass the data in a file on disk.
You can pass the data through pipes or sockets between the two programs.
You can pass the data via shared memory.
And more ...
There are many options.

Related

Kernel Driver program to get notified when a file is created in a directory

I want create a kernel driver program to monitor certain kernel events such as creating files, so that I receive a notification whenever a file is created, including the process ID of the process that created the file. I already created a program where I can see the files which are opened in the system, by creating an handler for IRP_MJ_CREATE.
I used this project as a reference.
There are several ways to do it, with the file system filter being the most complete. However for simple notifications, the easiest way to do it is through a minifilter.
You have several examples by microsoft:
https://github.com/microsoft/Windows-driver-samples/tree/master/filesys/miniFilter
Here is the reference: https://learn.microsoft.com/en-us/windows-hardware/drivers/ifs/ifs-reference
Do note however that you can do this without a driver, so unless you have a very good reason to do it in kernel space, you can do it from userspace. See FindFirstChangeNotification:
https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstchangenotificationa

Creating a Launcher and sending info to second program

I have created a program that extracts data from a racing game and sends it to a speed gauge cluster. I call it the transfer program.
I need a simple user-friendly User Interface to start the transfer program, set some variables and choose a COM Port. At the moment I'm trying to do it with a C++ Windows Forms Application in a CLR Project on Microsoft Visual Studio 2015. When I tried to do it directly (creating the UI in the same project as the transfer program) I just get too many errors that I have no idea where they come from or why they are there.
So I've decided maybe I could try creating some sort of launcher, i.e. a completely different program that is only the UI to start the transfer program and send a few user-set variables to it on startup as well as choose a COM Port to communicate on.
Any idea on how to start on this? How do I execute the transfer program from the Launcher? How do I send variables and data to it?
Thank you very much!
There are basically two ways to pass information to your transfer program.
For simple use cases, just pass the values along on the command line. If you're still using the CLR, this is done using System.Diagnostics.Process. A nice example can be found in this answer: https://stackoverflow.com/a/33633148/127826
Use a shared configuration file. So the user interface loads the values from the config and also saves the file before executing the transfer program. The transfer program then reads the same configuration file to get the values it needs.
The second approach is far more flexible and is what I would use.

Wrapping executable's input and output in c++

There is a program in java (Minecraft) and for the server part of it, it opens up in a terminal and prompts the user for commands, as well as give feedback for loading progress and other stuff. How can I make a c++ "wrapper" to automatically send commands to the terminal, and receive the response?
I could automate commands such as say and kick with GUI elements. I am running a mac with OSX Lion.
It presumably reads and writes stdin/stdout. You should look up executing a binary (in this case java.exe) in c++ and how to read and write to it. Alternatively, you could write a wrapper in Java that gives you control without having to parse the text output.
The answer to this question would depend on the operating system as different systems use different approach how to deal with the standard input and output channels. On a UNIX system you'd create a pipe(2) (or two if you want to capture standard output and standard error separately), fork(2) the "server", use close(2) and dup(2) to put the various file descriptors into place, and then execve(2) the actual program. After this you can read/write to various descriptors.

How to interface with an executable in C++

I have an executable that I need to run some tests on in C++ - and the testing is going to take place on all of Windows, Linux and Mac OSes.
I was hoping for input on:
How would I interface with the previously built executable from my code? Is there some kind of command functionality that I can use? Also, since I think the commands change between OSes, I'd need some guidance in figuring out how I could structure for all three OSes.
EDIT - Interface = I need to be able to run the executable with a command line argument from my C++ code.
The executable when called from the commandline also ouputs some text onto a console - how would I be able to grab that ouput stream (I'd need to record those outputted values as part of my tests).
Feel free to ask me follow up questios.
Cheers!
If you use qt to develop your code, you'll find QProcess will allow you to spawn a command line program in a platform-agnostic way.
Essentially:
QObject *parent;
QString program = "yourcommandlineprogram";
QStringList arguments;
QProcess *myProcess = new QProcess(parent);
myProcess->start(program, arguments);
You can then read from the process with various function calls such as readAllStandardOutput (), and write to the input of the process with QProcess::write(QString).
Alternatively, if you prefer Boost to Qt, Boost.Process will also let you launch processes. I confess I don't like the syntax as much...
boost::process::command_line cl("yourcommandlineprogram");
cl.argument("someargument");
boost::process::launcher l;
l.set_stdout_behavior(bp::redirect_stream);
l.set_merge_out_err(true);
l.set_work_directory(dir);
boost::process::child c = l.start(cl);
You can then work with your subprocess 'c' by using stream operators << and >> to read and write.
All those OSes support some form of "subprocess" calling technique, where your tester creates a new child process and executes the code under test there. You get to not only pass a command line, but also have the opportunity to attach pipes to the child process' standard input and output streams.
Unfortunately, there is no standard C++ API to create child processes. You'll have to find the appropriate API for each OS. For example, in Windows you could use the CreateProcess function: MSDN: Creating Processes (Windows).
See also Stackoverflow: How do you spawn another process in C?
As I understand, you want to:
Spawn a new process with arguments not known at runtime.
Retrieve the information printed to stdout by the new process.
Libraries such as QProcess can spawn processes, however, I would recommend doing it by hand for both Windows and MacOS/Linux as using QProcess for this case is probably overkill.
For MacOS/Linux, here's what I would do:
Set up a pipe in the parent process. Set the read end of the pipe to a new file descriptor in the parent.
fork.
In newly created child process, set stdout (file descriptor #1) to the write end of the pipe.
execvp in the newly created child process and pass the target executable along with what arguments you want to give it.
From the parent process, wait for the child (optional).
From the parent process, read from the file descriptor you indicated in Step 1.
First of all, is it possible that you simply need to want to make your original code reusable? In that case you can build it as library and link it in your new application.
If you really want to communicate with another executable then you can need start it as a subprocess of the main application. I would recommend the Process class of the Poco C++ libraries.
Looks like a job for popen(), available on Linux, Windows, and OS X
Sounds like you are only planning to do functional testing at the executable level. That is not enough. If you plane to do thorough testing, you should also write unit tests. For that there is some excellent frameworks. My prefered one (by far) for C++ is BOOST::Testing.
If you control source code there is also common tricks for functional testing beside launching exe from an external process : embed functional tests. You just add an option to your program that execute tests. This is cool because tests are embedded in code and autocheck can easily be launched in any execution environment.
That means that in the test environment, as you call your program with some test dedicated arguments, nothing keeps you from going the full way and redirect the content of stdout and even check the tests results from within the program. It will make the whole testing much easier than calling from an external launcher, then analysing the results from than launcher.

Intercept windows open file

I'm trying to make a small program that could intercept the open process of a file.
The purpose is when an user double-click on a file in a given folder, windows would inform to the software, then it process that petition and return windows the data of the file.
Maybe there would be another solution like monitoring Open messages and force Windows to wait while the program prepare the contents of the file.
One application of this concept, could be to manage desencryption of a file in a transparent way to the user.
In this context, the encrypted file would be on the disk and when the user open it ( with double-click on it or with some application such as notepad ), the background process would intercept that open event, desencrypt the file and give the contents of that file to the asking application.
It's a little bit strange concept, it could be like "Man In The Middle" network concept, but with files instead of network packets.
Thanks for reading.
The best way to do it to cover all cases of opening from any program would be via a file system filter driver. This may be too complex for your needs though.
You can use the trick that Process Explorer uses to replace itself with task manager. Basically create a key like this:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\taskmgr.exe
Where you replace 'taskmgr.exe' with the name of the process to intercept. Then add a string value called 'Debugger' that has the path to your executable. E.g:
Debugger -> "C:\windows\system32\notepad.exe"
Every a process is run that matches the image name your process will actually be called as a debugger for that process with the path to the actual process as an argument.
You could use code injection and API redirection. You'd start your target process and then inject a DLL which hooks the windows API functions that you want to intercept. You then get called when the target process thinks it's calling OpenFile() or whatever and you can do what you like before passing the call on to the real API.
Google for "IAT hooking".
Windows has an option to encrypt files on the disk (file->properties->advanced->encrypt) and this option is completely transparent to the applications.
Maybe to encrypt decrypt file portions of a disk you should consider softwares like criptainer?
There is this software as well http://www.truecrypt.org/downloads (free and open source) but I haven't tried it.
Developing a custom solution sounds very difficult.