Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Running a program inside a program

  1. #1
    Constant Bitrate
    Join Date
    Jul 2001
    Location
    Richmond, VA USA
    Posts
    217

    Running a program inside a program

    I have no idea what this would be called, but im sure there is a technical term like nested execution or something. Anyway I am using Visual C++ and learning all I can... I know the general structure and functions and all so if you give me code, I will more than likely understand it. All I want to know is how to call a program like I would on a command line (like the run command).

    So if I went to run and typed in
    "C:\Program FIles\Winamp\winamp.exe"

    Winamp would run. How can I do this in Vis C++??? (not neccesarily just like that, but how can a program be called to run from inside of a program?)

    Edit: I want to know how to do this in general for any program, not just winamp

    Thanks in advance for any help.

    -Josh
    Josh Karger
    By reading this post you agree to overlook all grammatical, spelling, and typographical errors! ;)

  2. #2
    Constant Bitrate
    Join Date
    May 2002
    Location
    Gorge of eternal peril
    Posts
    134
    What type of project are you building? You have two options:

    Code:
    WinExec("C:\\Program Files\\Winamp\\winampa.exe", SW_SHOWMINIMIZED);
    This executes something as if you had just dbl clicked its icon.

    or

    Code:
    system("C:\\Program Files\\Winamp\\winampa.exe");
    This will pop up a command prompt window and executes what you typed as if you had typed it. Youll see what I mean when you try it.

    Also make sure you use "\\" to represent one \. Thats similiar to typing "\n" or "\t" etc...

    Hope that helps
    '03 BMW M3 6 spd.
    Black/Imola Red
    19's

    Shuttle XPC w/ AMD 1800+
    Opus 150w
    Lilliput 7"
    Wireless internet through my P900 and bluetooth

  3. #3
    Constant Bitrate samael's Avatar
    Join Date
    May 2001
    Location
    new zealand
    Posts
    130

    fork

    fork the code using the command fork. This will create another process (i have only done this in linux , i think in windows it creates a thread which is kind of the same). The use exec.
    pseudocode :

    doSomeStuff();
    fork();

    if (i am not parent process) {
    exec(c:\programfiles\winamp);
    }

    if you exec something , the process is terminated. so this :

    doSomeStuff();
    exec(aprogram);
    printf("hello"):

    hello will never appear as when you exec , the exec-ing processs is terminated. If you dont care about what happens to the program after you exec then dont bugger around with fork
    and BTW take this with a grain of salt , this is from memory and may be slightly wrong
    stupid quote # 1 : "i dont do drugs , i only ever smoke pot when i'm coming down" , Me , 6 september , 2001.

  4. #4
    Constant Bitrate
    Join Date
    Jul 2001
    Location
    Richmond, VA USA
    Posts
    217
    InFix, This looks like it will work (the first option (such as dbl click) is what I was going for). Once again you have helped me tremendously.

    samael it really doesn't matter in this case because the program will always be running in the background.

    I used winamp as an example... but what I am actually doing is making a call to my home automation program. I always turn my house into a haunted house for halloween, and I want to be able to activate some madules and then turn them off seconds later... unfortunately the software (ActiveHome) only allows incrementing in minutes so I could have something come on and I couldn't have it automatically go off for at least 1 min. With this program and system there are housecodes A-P and module numbers 1-16. So K10 could be a lamp and K5 could be the coffee maker. With ActiveHome you can address the path like this:

    D:\Program Files\Home Control\X10COMM.EXE k10 on

    and that would turn my lamp on. So using the code InFix provided doing this would turn the lamp on from my program:

    WinExec("D:\\Program Files\\Home Control\\X10COMM.EXE k10 on", SW_SHOWMINIMIZED);

    now I can just do some filling in to make it wait and then do the same except it says "k10 off " and that will turn my lamp off.

    I know my methods aren't exactly the best coding, but im only going to whip this together to use for halloween, so as long as it works for me... im happy.

    Of course I just got another cool idea while writing this. Turn a light on and open winamp to play a sound such a a scream. Then close winamp, turn light off, and wait a predetermined time later then repeat. So I guess Samael I will have to check to see what is running and all that. For those interested,and want more info on X10 check out:

    www.x10.com -I dont really reccommend this site too much, but they have some products... a better is:
    www.smarthome.com
    comp.home.automation

    Also if anyone is interested, let me know and I'll post the code showing the full effect.

    I think a high tech edge is going to get some trick-or-treaters this year!!!

    -Josh
    Josh Karger
    By reading this post you agree to overlook all grammatical, spelling, and typographical errors! ;)

  5. #5
    Constant Bitrate
    Join Date
    May 2002
    Location
    Gorge of eternal peril
    Posts
    134
    Glad to help. If you want winamp to play a file, you have to do this:

    Code:
    WinExec("C:\\Program Files\\Winamp\\winampa.exe \"d:\\music\\file.mp3\"", SW_SHOWMINIMIZED);
    Notice the \", thats the escape sequence for the ". So if you were to run winamp on the command line you would type:

    winamp "myfile.mp3"

    So to get that effect you do the \"myfile.mp3\" as the paramater

    Also, I would recommend NOT hardcoding that in, so you could do this:

    Code:
    	CString path;
    	path.Format("%s \"%s\"", "C:\\Program Files\\Winamp\\winamp.exe ", someFunction());
    	
    	WinExec(path, SW_SHOWMINIMIZED);
    where someFunction() will return either a char* or CString with the file name that you want in it.

    Anyway, let me know how that turns out, sounds very intresting! :P
    '03 BMW M3 6 spd.
    Black/Imola Red
    19's

    Shuttle XPC w/ AMD 1800+
    Opus 150w
    Lilliput 7"
    Wireless internet through my P900 and bluetooth

  6. #6
    Constant Bitrate
    Join Date
    Jul 2001
    Location
    Richmond, VA USA
    Posts
    217
    You are a mind reader... and you're scaring me. I just tried to get it to play a file and then open a playlist and couldn't get it figured out, searching google didn't help and I was about to ask when I refreshed the post to see if there were any replies... and boom there you are. Anyway one question, I have been trying to decipher the windows.h file but no luck there... is there any sites that lists all of the little pieces of syntax (for example one search did reveal a "SW_SHOWNORMAL") Which I see loads it just like well it hurts to point out, normal. I'm sure more searching on my part will yield something.

    I fully understand your point about leaving it flexible to load any file through a string (ie not hardcoding) but I don't understand why you put:
    Code:
    path.Format("%s \"%s\"", "C:\\Program Files\\Winamp\\winamp.exe ", someFunction());
    What is the point of the "%s \"%s\"" ?

    -Josh

    BTW I tried the code and didn't comprehend what you said about the escape sequence (/") then I re-read it and thought it over and remembered seeing something like that before and found my error and needless to say it works! Thanks again!
    Josh Karger
    By reading this post you agree to overlook all grammatical, spelling, and typographical errors! ;)

  7. #7
    Low Bitrate
    Join Date
    Sep 2002
    Location
    aychamo land
    Posts
    92
    rofl

    a lot of this was covered in my "Tips For Developers" thread that got turned into a "you are a sucky programmer" thread, then a big fight about somethign MFC or some junk I have no idea bout.

    god bless that communist

  8. #8
    Constant Bitrate
    Join Date
    May 2002
    Location
    Gorge of eternal peril
    Posts
    134
    As far as all the little syntax things go, there's a ton of sites out there, but my main source is the 2 msdn discs that come with vc++. It has everything you could possibly every want, as far as windows programming goes. Online though, for MFC, try www.codeguru.com great site, and has tons of info. Also, www.codeproject.com

    As for the %s stuff, that is identical to the oldschool C printf function. Like printf("%s", string); where string is a char*, will print whatever is in string. printf("%d", aNumber); where aNumber is an int, will print the number. Its a little hard to explain, but just lookup printf + format in google and I guarentee there will be a million pages explaining it
    '03 BMW M3 6 spd.
    Black/Imola Red
    19's

    Shuttle XPC w/ AMD 1800+
    Opus 150w
    Lilliput 7"
    Wireless internet through my P900 and bluetooth

  9. #9
    Constant Bitrate
    Join Date
    Jul 2001
    Location
    Richmond, VA USA
    Posts
    217
    Thanks fo rthe help tonight, the only thing about the tips for dev thread (which I was looking at and trying to relate) was that it didn't touch too much on the exact of c++ and was more vis basic I beleive. Either way call it sucky programming if you want (and I know you aren't and that charles was) but considering im whipping it up for one night (halloween) and it won't be any mroe than 15-20 lines MAX, I really don't care!

    All I want is a little something that will run my show so I don't have to push the buttons...lol I might expand it later to use in the future

    -Josh
    Josh Karger
    By reading this post you agree to overlook all grammatical, spelling, and typographical errors! ;)

  10. #10
    Constant Bitrate
    Join Date
    Sep 2002
    Location
    Redmond, WA
    Posts
    171
    ShellExecute is the best way to execute a program and get information about the resulting process. (i.e. you can wait for it to spin up using WaitForInputIdle). With the handle you can use WaitForSingleObject to detect when the process has exited.

    You could also use CreateProcess (which ShellExecute, WinExec, etc probably all map down to behind the scenes) , but it requires a zillion parameters that you probably don't care about (like security context, etc)

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •