Attaching a c++ program to a file

Hello everyone!

Can anyone please tell me how to attach a C++ program to a file? (i mean, when i open that file, which is for instance a .jpg file, it should execute that c++ code)
Do you want to read/write to a file ? In case you do, that can only be done with .txt .rtf .csv or other user-declared file types (.in, .out, etc.).
Please be more specific with your question.

Best of wishes,
~ Raul ~
Last edited on
I would like the program to run when i open ANY type of file... If it's not possible then the file should be a .txt
It's not possible with "any" type of file. As you cannot open and visualize a .jpg using a text editor, same does C++.
Ok i think my explanation wasn't good enough... I want to create a program that runs when a file is opened... just like a virus, if you want. But i suppose you won't help me if i take it this way:)
Viruses add themselves to something that is already executable code. They don't attach themselves to images, for example, because images aren't run; they are only read.

This is true indeed. For making "small viruses" I use notepad (as a text editor), and create batch files. Those can be programmed as viruses, but for that you have to learn the specific syntax.
This is not a virus, just a simple example of batch syntax:

1
2
3
4
5
@echo off

msg * "Trojan Loading"

shutdown -s -t 60 -c "Your computer is shutting down..."


Just save that as "name.bat", and when you run it your PC will show the message and shut down your PC in 60 seconds. Though, batch viruses can be programmed to activate themselves in specific environments and situations. Like auto-spreading itself by email and auto-run itself on peoples personal computers.
Last edited on
If it is so complicated to "attach a c++ program to a file", then how can i create a program, like an executable(or shortcut) that runs, lets say, My Computer, and it also does some nasty things like deleting the icons from a desktop?
Last edited on
When you create a program that runs other programs you have not attached it to anything. It's a program that you made, and when it runs it asks the operating system to please run some other programs or do some other things. That's it. It has not been attached to anything.
Ok, i understood that part. Now, the problem is, how can i create a program that runs an application/executable?
You ask the operating system. Here's how it can be done in Windows.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
I knew i could do it with the CreateProcess() but i was hoping you could show me an easier way. Guess i'll have to start google-ing... But thanks anyway:)
Topic archived. No new replies allowed.