Thread: Need help on code to execute the program

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    1

    Need help on code to execute the program

    i need some help, iam a newbie who just start to learn c++.

    I want to write a program in c++ that would execute program files. Any files, like .doc, .jpg, or any others.

    I also want to set a timmer on when to execute each program.

    can some one help me out and show me some pointer on where to look

    Thank you very much,
    Have a happy Holiday.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If by execute, you mean open... cause you can't execute a .doc, but you can execute the program to view a .doc

    Your best bet would be the system(file or directory) function which will open anything in the parameter with its default editor. Don't get used to this command too much, you could end up writing a security nightmare if it's used improperly.

    As for making a timer, you could look into the Sleep(seconds) function... which will halt a program and return as soon as the time elapsed.

    There are better ways of doing this program, but this is the most Quick and Dirty method. It's also the easiest to understand for a beginner.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    91
    shellexecute is't too hard to understand,

    Code:
    #include <windows.h>  //You need shell32.lib for this one 
    
    int main(void)
    {
      char szPath[] = "C:\\WINDOWS\\desktop\\yourapp.doc"; put your filepath here
    
      HINSTANCE hRet = ShellExecute(
            HWND_DESKTOP, //Parent window 
            "open",       //Operation to perform  
            szPath,       //Path to program
            NULL,         //Parameters
            NULL,         //Default directory
            SW_SHOW);     //How to open ' this param here can be minimised, normal, maximised etc..
    
      /*
      The function returns a HINSTANCE (not really useful in this case)
      So therefore, to test its result, we cast it to a LONG.
      Any value over 32 represents success!
      */
    
      if((LONG)hRet <= 32)
      {
        MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
        return 1;
      }
    
      return 0;
    }

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why'd you change the path in the string variable?
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Here is a good site I think its what he wants. It talks about what everything does like on this siteXD here

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ah... no he's not looking for File I/O. He's looking to run a program in his program at a set time.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how execute code residing in data segment??
    By ravindragjoshi in forum C++ Programming
    Replies: 11
    Last Post: 06-14-2007, 11:06 PM
  2. execute a code in data segment...
    By ravindragjoshi in forum Windows Programming
    Replies: 1
    Last Post: 06-12-2007, 11:43 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM