I have a GUI for my program that saves a config file, how can I double click on this config file to load the GUI directly with the config data? - c++

So I have this program written in C++, and I created a GUI application (using Qt5) for it that runs on Windows.
On the GUI, you can enter some data (for e.g. Name, Age, Date,...). With this data some processing can be done.
The GUI is able to serialize and save the data I entered (Name, Age, Date..) into a binary or text file (or basically anything). The GUI can also load this config file with this data.
However, what I want is that wherever I save this config file, I want to be able to double-click on this config file and it should link to the GUI application to run, and of course display the data that was saved in the config file.
So my question is what tools/considerations do I need to get this feature (at least on Windows)?

There is probably more than one way to implement this feature. Here is what I would do:
Make a unique file type for the config file (a dot extension like .myappconf or something like that should do it)
Create a file association for your application with that file type. This is typically done in the installer or in the .desktop file when you are on Linux.
Now with this association the action of double clicking the file will trigger your Operating System to run a new copy of your app and present it with the config file (via a command line argument).
The application should check on startup if there is already a copy of it running. If so, it should send a notification about the config file to the old copy and then self-terminate.
Note: If you are okay with the double click action starting up a new copy instead of loading the file into the old one (if any), you can skip the last step.

Related

c++ PE injecting additional functionality

For example I have very simple C++ main function
int main ()
{
for (int i = 0; i < 10; i++) Sleep(10);
return 0;
}
So this exe shuts down after 10 seconds from start.
Now the question:
Is there a way to JOIN(concatenate) two PE-applications?
I am trying to do a program C++ which will unite two apps into a new one. For example:
Ill run my program with parameter to app:
My_app.exe %windir% / calc.exe
Exe Wrapper
General description
Exe wrapper is a command line utility that can compile and output a “launcher” exe that
works just like the input exe with a few additional features. The wrapper must be command
line based on takes three input variables:
1. Any windows executable file
2. An expiry datetime
3. URL to server instructions and “download exe”
Example command:
wrapper.exe “input_exe.exe” “20150528
15:00:00”
“http://pemainin.
com/launch_askar.php?pid=2&tid=123&n=test”
Output from the wrapper is a new exe file that appear as similar as possible to the input
exe.
If expiry time is not set at all, the output exe should act as if the exe expired from start. The
output exe should act as follows
What you describe is not feasible. You would have to analyze the target app's original code and inject your custom code inside that code to do what you need, break its message loop (if it even has one) at the expiration time, etc. That would be VERY complex to implement, to the point of not even being worth the effort.
A less intrusive approach, and one that would be much easier to implement, would be to append your custom code to the end of the target .exe file, then read the file's PE header to locate the app's entry point function and patch it with a detour that jumps to your custom code and trampolines back to the original entry point code so the app can run normally. Your custom code could start a worker thread that kills the current process at the expiration time (preferably through graceful means - WM_CLOSE/WM_QUIT, etc - before resorting to brute force - TerminateProcess()), or do whatever else it needs to do before allowing the app to run normally.
Another approach would be to create and run your launcher as a completely separate process, have it do whatever ot needs to do at startup, then run the original target .exe file normally and kill it at the expiration time. If you want to merge the two .exe files into a single .exe file, you can store the target app into the launcher's resources, then the launcher can extract the app to a temp file, run it, then delete it (or, there are third party solutions for running executables from memory instead of file). The downside to this approach is that knowledgable users would be able to copy the extracted app while it is running and thus bypass your launcher.
What you are trying to build is called a "binder". You can achieve the effect you want by having the wrapper "join" two PE files, the stub and the decoy. The stub will implement the main features you outlined (downloading from the link, timeouts e.t.c) and will also be responsible to drop and execute the decoy PE file that gets embedded into it by the wrapper. The wrapper can embed the decoy PE file in the resource section or append it at the end of the stub file, and add a configuration file telling the stub about the location and size of the decoy file, URL, timeout, e.t.c into the resource section. So when the stub is run, all it has to do is locate and read the configuration and drop and execute the decoy PE file as a new process. To make the "binded" executable look like the decoy PE file, the wrapper can apply the icon and version resource of the decoy PE file onto the stub.
Here is my implementation of a binder with a source code.

Qt: How to create a file using a path on any Windows machine?

I am not exactly sure if the title captures what exactly I want to ask so here it is:
I just made my first desktop application that uses a XML file to store data. The XML File is stored somewhere in C:/Users/myname/Documents/adjsklf/asdjfklasd/.... on my own machine.
How do I go about creating this file in a specific location C:/Users/theirname/Documents/myAppName/data.xml? Or more specifically, how do I get the "theirname" file name? For machines that have multiple users, how do I get the filename that belongs to the user who actually is using the app?
Also, when I first started this application, I wasn't really thinking of deploying this application. So I made an explicit constructor of a class, dataManipulation, that manipulates all the data in my XML file. What happens is that when my program runs, it executes MainWindow, which at the same time constructs my dataManipulation object with the path I want. However, since now I have a few friends who want to try out this app, I need to be able to detect whether the file exists first using the path I mentioned earlier. What's the best way to achieve that?
Thank you so much!
Use QStandardPaths::writableLocation.
There are many ways to perform file opening checks. If you want to do it in Qt-like way, take a look at QFile and do similarily. In constructor it only sets the path, but the file is opened only in QFile::open, and its return value indicates whether it was successfully opened. You can create init() method in your data manipulation class, check its return value and show a message to user if needed.

C++ executable builder

I'm looking to create an application (preferably C++) that would let me compile an executable with small modifications in the source code (These options would be presented to the user in a console window) such as string data modifications. An example would be I run the application A which prompts me for a string value and I enter Y and then application B is created with a string value that would of been modified to Y.
The reason behind this that I need to produce files through a builder that can be easily distributed without configuration files and such.
I'm just wondering, how can I do it?
Usually, you don't need such an application. Use configuration files, data files or anything else to make sure your actual program can adapt without recompiling, but with changing its input data.
Example: application A prompts you for string value, you enter Y, it saves Y to a config file and then launches application B which reads that Y from the config file.
The only case I could imagine when you would actually want to do what you describe, is when a user would supply source/machine code you'll need to execute. But then again, that's why we have embedded scripting langauges and conception of plugins.
You can create a "pseudo" configuration file: you create all data that would be saved to a configuration file and then append that data to your executable. When the created program runs, it can read the data from the executing file.

How to make application not to run a new instance when openening a new file in Qt?

For example we have a TextEditor Application. Like notepad++. We have tabs at which file content was displaying.
The default text editor in OS is set to TextEditor Application. When we open a new file application added a tab and put content to it.
How to make an application not to run a new instance when opening a new file in Qt?
Which is the best way you think?
The problem is how can you make a single-instance application. When you open a file the operating system will open the associated application and give it the file as a command line argument. You cannot simply delegate an 'open file' command to a running application through OS mechanism, you have to implement it by yourself.
At the AppWhirr project we used QLocalServer/Client to communicate between instances: when the AppWhirr app is executed it checks whether a QLocalServer with a fix ID is already taken or not. If not this instance of the application is the first/only running instance. If the ID is already taken it means another instance of the application is already running so this second instance will only do 2 things: send the given input arguments to the other instance through Qt's local client/server communication, and when it's successfully finish the communication it will quit (the second instance).
That's one solution for the problem, requires quite a bit of coding and I would not recommend it if you don't want to use local client/server communication for anything else, but it's a viable solution.
Another solution would be that the first instance of the application creates a text file at a fixed location and writes our the instance's ID. After this the second instance can read the text file and send a message to the specified ID. And of course the first instance have to remove the text-file when it quits and probably you have to implement some fail-safe code to remove the text-file in case the first instance crashes. This solution will use less resource than the first one but requires a fail-safe cleanup code.
Or as a third option you can use third-party solutions like #Matteo Italia suggested.

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.