# Opening a Windows program

This is a discussion on Opening a Windows program within the C++ Programming forums, part of the General Programming Boards category; what can I use to open up a program on windows.... like say... when I run the program it opens ...

1. ## Opening a Windows program

what can I use to open up a program on windows.... like say... when I run the program it opens up msn?

Alvin

2. System("X:\path\to\exe\folder\file.xxx")

3. Make sure you use a lowercase, 's' - C is case-sensitive, and you'll find the function in cstdio.

Simply put, system() takes a pointer to a C-style string (or a string literal, as above) as a parameter and executes that string as a command in the OS shell. Just be aware of the dangers of using it. What if the program isn't there? What if you're on a different OS? What if the program has been replaced with something else?

4. ok here is what I'm trying to do..... I want to write a program that puts a very simple password on a program..... after I write the program that for the password I'm going to change the name of the windows program that I want to put a password on... then change the program that I write to the name of the windows program... did ya get all that? or is my explanation a little too.... off... HAHA

so far here is what I have with the program....

Code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main () {

cout << "Enter First Character. " << endl;
cin.ignore();

if ( password == 'a') {

char pw;

cout << "Enter Second Character. " << endl;
cin >> pw;
cin.ignore();

if ( pw == 'm' ) {
system("C:\location\of\program.xxx");

}
}
cin.get();
return 0;
}
and there are two errors when I hit the build button.

fatal error LNK1169: one or more multiply defined symbols found

and....

can anyone help? I know that I can look it up on microsoft.com but I'm kind of running short on time for tonight and the next time I can get on is sunday... :'( so when I get back if there is no reply then I can just look up things and try to figure it out on my own using microsoft.com... and help is greatly appreciated...

5. It means that you have got two copies of the main function in your code. Find the second copy and rename it, delete it or comment it out. Also, remember to double up the back slashes in string literals.

6. so you basically want to hide the real program, and use your program as a sort of gateway to the other program...

judging from your code, you want a user to have to have to run your program, then type 'a', then type 'm' to get another program to run?

in that case, you don't need the string library, and here's some fully functional code:
Code:
#include<iostream>  //for IO stuff
#include<cstdlib>   //for exit() and system()

int main()
{
char pw;    //it's okay to reuse variables

std::cout<<"Enter the first character: ";   //prompt for input
std::cin>>pw;       //take in the character
std::cin.ignore(1); //take in the '\n'

if(pw!='a')     //if the 'password' is wrong
exit(1);    //exit with a failure code

std::cout<<"Enter the second character: ";  //need input...
std::cin>>pw;       //take it in
std::cin.ignore(1); //this one is pretty useless...

if(pw!='m')     //if the second 'password' is wrong
exit(1);    //exit with a failure code

return 0;                   //exit right after notepad is started
}
by the way, this is in NO WAY secure. instead, you might consider encrypting the program you want to run using simple XOR encryption, then when the user enters your program, they have to provide the key (password) that will then (again) be XOR'ed into the original program. then simply have your program run the original. when the original is finished executing, have your program XOR it back to it's useless state.

of course, keep a backup of this original program, and the method I just suggested is not very secure either, but it is a giant leap in the right direction. especially if you're relying on something as simple as renaming the original program to keep it harder to find...

7. Actually, major_small, given the weird default behaviour of Windows consoles, your code will show the entered password in clear text, and ignore(1) doesn't do what you seem to think it does.

8. Originally Posted by CornedBee
Actually, major_small, given the weird default behaviour of Windows consoles, your code will show the entered password in clear text, and ignore(1) doesn't do what you seem to think it does.
eh, I never said anything about hiding the text input... and ignore(1) does exactly what I think it does... it's clearing out the '\n' left in the stream by cin (although now that I think about it, it really doesn't matter becaues the program exits right after notepad is opened)

@Pyroteh: CornedBee does bring up a good point though: the password you put in will be in plain-text and everybody will be able to see what you write, unless you use something like getch() instead of cin.

edit: I just realized that I used bad wording on my comments of cin.ignore()... fixing that now...
edit2: wow, that first comment was waaaaay off... I really should be getting myself to sleep now...

9. Originally Posted by major_small
this is in NO WAY secure. instead, you might consider encrypting the program you want to run using simple XOR encryption, then when the user enters your program, they have to provide the key (password) that will then (again) be XOR'ed into the original program. then simply have your program run the original. when the original is finished executing, have your program XOR it back to it's useless state.
If i understand correctly, how can you make a portable program that can run on other windows boxes that is safe? Considering you cant really just go grab other applications and encrypt etc that are not yours. Not looking for a step by step guide just maybe rattle off some concepts, searching didnt do me too well here.

10. or you can use ShellExecute()

11. Originally Posted by dan20n
Considering you cant really just go grab other applications and encrypt etc that are not yours.
why not?

with one run of the following code, you will render test.exe, which is a simple 'hello world' program, utterly useless. with a second run, however, test.exe is restored to it's useable state.

if you're wondering how it's being done, the following code, in effect, corrupts the target program and then uncorrupts it later on. you can change they key to be any C-style string you want.

Code:
#include<iostream>
#include<fstream>

int main()
{
char ch;
unsigned int index=0;

std::fstream file("test.exe",std::ios::binary|std::ios::in|std::ios::out);

for(;;)
{
for(register int i=0;i<strlen(key);i++)
{
file.seekg(index,std::ios::beg);

if(file.eof())
{
file.close();
return 0;
}

ch^=key&#091;i&#093;;     //the heart of the XOR encryption

file.seekp(index,std::ios::beg);
file.write(static_cast<char*>(&ch),sizeof(ch));

++index;
}
}

file.close();

return 0;
}
and the test.exe source:
Code:
#include<iostream>

int main()
{
std::cout<<"Hello World";
std::cin.get();
return 0;
}
a few notes:

first, if any moderators deem that this code is malicious, then please feel free to take it down.

second, this code will take a little while to complete in case you do try to run it (it takes a while to parse the entire file)

third, please excuse spelling/grammar/coding mistakes... I've been awake way too long...

12. But, major_small, given that ch is just a single character, ignoring one might not suffice.

13. Originally Posted by CornedBee
But, major_small, given that ch is just a single character, ignoring one might not suffice.
oh, I see what you're saying now... I was thinking more simply, as in he is the only user that will be using this, so he doesn't have to put much effort into user-proofing his code...

14. I was thinking more simply, as in he is the only user that will be using this, so he doesn't have to put much effort into user-proofing his code...
But if he's the only one, what the use of the thing in the first place?

If he wants to prevent other users on the same computer from using the program, log in as admin and deny them read/execute on the program directory. Far easier and more secure. Especially as some programs don't react well to being moved away from their installation directory.

15. Originally Posted by CornedBee
But if he's the only one, what the use of the thing in the first place?
you've never done things like that before when you just started programming?

Page 1 of 2 12 Last