How do I Open a Program Through C++ (Code here)

Pages: 12
May 30, 2010 at 12:24am
Hi, I want open a program through C++. Ex. Like I compile the code and when I do it opens up Internet Explorer. Is this possible to do? Would I use a fin.open?

I've found this code and tried it. It does work, but I would like it to automatically open it without having to be specified.

The open name for Internet Explorer is iexplore

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
char s1[256],s2[256];
cout<<"Program name: ";
gets(s1);
sprintf(s2,"START %s",s1);
const char* prgm=s2;
system(prgm);
}
Last edited on May 30, 2010 at 12:43am
May 30, 2010 at 12:53am
I would like it to automatically open it without having to be specified.
Huh?

Also, that code has a security hole so big, I'm afraid I'll fall in just by looking at it.
May 30, 2010 at 12:59am
So it's not possible? What exactly do you mean? lol I was able to open it with the code above, which I think it used command prompt.
May 30, 2010 at 1:19am
What do you mean by "automatically open it without [specifying it]"?
May 30, 2010 at 1:50am
In the code above I have to actually write iexplore into the compiler to have it open Internet Explorer. But I want the compiler to open Internet Explore right when the compiler runs without have to type anything in. Just run & compile then Internet Explorer Opens up. Does that make sense?
May 30, 2010 at 2:03am
closed account (S6k9GNh0)
1) Don't use conio.h ffs.
2) Why are we using sprintf, gets, and cout in the same program? This is C++.
3) system will work but you might as well use batch commands at that point. This is a native program so we should use native calls if possible, not some function that executes commands indirectly (which is a huge security hole anyways).
4) What the hell is fin.open? There is no predefined object called fin.
5) Even if there was a predefined object called fin (assuming its of type ifstream), the open function does not open the file you think it does. It opens the files contents and read / writes to it.
6) When you double click an executable file, it doesn't "open" the file. It creates a new process with dedicated memory just for that program, loads the program into memory, and begins processing the programs stored instructions (or a similar process. May not be exact. I'll have to look at some documentation for exact process.) You have to use an OS-specific function provided by the actual OS API. To do this on Windows, you need to call the CreateProcess function documented here: http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx

Ironically, there is actually an OpenProcess function that will give you a handle to an already running process. This is relative to accessing a file that is already stored on your hard drive. To open something, it must already exist. You must Create your process in order to Open it. Get it?


7 and most important) There is actually a more correct way to start a web browser natively other than hardcoding the location of iexplore.exe and starting it (which is STUPID. I do not EVER want to see ANYONE do that. I get pissed off when I have a program open Internet Explorer over my default browser which is STUPID). Some suggest that you should use WinAPI's ShellExecute, however you can find the default browser in registry and execute it from there which I think would be more correct in our case.
Last edited on May 30, 2010 at 2:11am
May 30, 2010 at 2:05am
You want the compiler to run Internet Explorer? Are you sure you're not mixing up the terms "compiler" and "program"?
May 30, 2010 at 2:28am
Thanks for both replies, but I'm still kind of new so I don't know a lot of function calls and stuff. As for helios I mean the program the .exe that the compiler creates. I'd also like to be able to choose how many Internet Explorers open, maybe use a count function and choose the amount of times it opens.
May 30, 2010 at 2:40am
closed account (S6k9GNh0)
Algorithms, you need to learn how to use the language first. I suggest you also learn the basic structure of how a computer works, how an OS works, and how a program works inside of the OS. A good assembly book will often explain most of these before hand of teaching.
May 30, 2010 at 2:52am
That's excessive, computerquip, and... how does something so overblown really help? A book on Assembly will only discourage the OP, as while it could teach him/her all he/she needs to know, and more provided he/she had the patience... no.

However, computerquip, essentially, is right. Algorithms... you... seem to have some trouble differentiating between a compiler and the produced executable automatically. That's a problem. I don't wish to humiliate you in any way, but... you have quite some learning to do before attempting to use any element of the Windows API. Here's a site I pulled up in a few seconds.
http://computer.howstuffworks.com/pc4.htm

Also, the C++ tutorial on this site will help.
http://cplusplus.com/doc/tutorial/

And when you get a bit better:
*link deleted temporarily while computerquip and I discuss this*

-Albatross
Last edited on May 30, 2010 at 3:39am
May 30, 2010 at 3:19am
closed account (S6k9GNh0)
I wasn't saying he should learn assembly, I was saying that the essence of what is the basic requirements of writing assembly is very helpful for everday programming and is often found in the beginning of an assembly book. I also hate referencing a newbie to WinAPI since he will most likely cling to it like its his blankey. Being Windows-only tends to come from these situations. They won't use cross-platform libraries or more feature filled libraries because all they want to learn is the WinAPI because they think its hot shit.

May 30, 2010 at 3:24am
You want him to use a cross-platform library to launch a single platform application?
I'm all for portability, but really, the right tool for the right job.
May 30, 2010 at 3:31am
Weeelll... the basic requirements of writing Assembly are rather high relative to the basic requirements of writing C++, no?

Though, you have a point about WinAPI. It's not cross-platform, it's a terrible API, and... well...

-Albatross
Last edited on May 30, 2010 at 3:31am
May 30, 2010 at 3:37am
closed account (S6k9GNh0)
It depends. It doesn't take much to make your basic assembly program (however easy it is to also cause your program to crash and burn). But to understand what assembly is doing when you type in add eax, 3;, you need to understand all that I described.

@ helios:

WTF did you EVEN read I wrote? Where did I say you should never use WinAPI? I didn't of course, I just mentioned that newbies tend to cling to it for everything, even non-OS-specific function calls which is why I don't like telling a newbie to just go read the WinAPI, especially for GUI libraries.
Last edited on May 30, 2010 at 3:38am
May 30, 2010 at 3:43am
The links I provided should help him with those.

-Albatross
May 30, 2010 at 3:58am
Where did I say you should never use WinAPI?
When did I say you said that?
All I said was that your phrasing seemed to imply that this could a better job for a portable library.
Last edited on May 30, 2010 at 4:01am
May 30, 2010 at 4:21am
Alright guys I appreciate all the info and help. As for programming I said I was new not a noob, I have a teacher at my college who's the Hitler of C++ programming and she is crazy hard with C++, but because of her teaching methods I'm learning C++ quite well. I have written plenty of programs so I don't need to go to a book. I have written programs that tell you all sorts of math questions and calculations, interest rates and loan amounts, and calenders..I totally get the programming I'm just trying to widen my understanding of it and trying to take on more complicated programming, like working with the stuff I discussed above. Anyways I am somewhat new to the windows based programming with C++, yet I was able to write a program that could delete any file I specified just by opening the program, so I do get it. So how would i do the above, I'm somewhat familiar with some of the functions above, but I'd love to be able to use and understand them.
May 30, 2010 at 10:10am
Algorithms wrote:
...but because of her teaching methods I'm learning C++ quite well.

What are her teaching methods? Could you elaborate on this?... :D
May 30, 2010 at 7:03pm
Well she gives us very difficult projects and only brushes on the stuff that we will be encountering in our projects. Then when we are working on our projects she expects us to tare apart our books looking for every function and answer. She doesn't give us examples of the projects like my other teachers have done, she just expects you to dig through the book and find stuff even if that means reading the 50 page chapters 10x through. But because of this I've memorized a lot of stuff and have learned C++ well. I took Python before this and didn't understand it like I do with C++. I can do 5x more with C++ then I could with Python.
May 31, 2010 at 11:36pm
Anyone? Still unanswered..
Pages: 12