Thread: Run A Program from within a cpp code

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Run A Program from within a cpp code

    I want to execute a program (say test.exe). But I want to do it from inside a C++ file as I want to have a delay after the program executes, how can I execute a program on disk from inside cpp? it's on my comp, so it doesn't matter about security if i have to turn off some stuff...

    tried
    Code:
     #include <windows.h>
    #include <shellapi.h>
     
    void main() {
    char* URL = "C:\\test.exe>";
     
    ShellExecute(NULL, "open", URL, NULL, NULL, SW_SHOWNORMAL);
    }
    but got errors:


    t1.obj : error LNK2028: unresolved token (0A000017) "struct HINSTANCE__ * __stdcall ShellExecuteA(struct HWND__ *,char const *,char const *,char const *,char const *,int)" (?ShellExecuteA@@$$J224YGPAUHINSTANCE__@@PAUHWND__ @@PBD111H@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

    t1.obj : error LNK2019: unresolved external symbol "struct HINSTANCE__ * __stdcall ShellExecuteA(struct HWND__ *,char const *,char const *,char const *,char const *,int)" (?ShellExecuteA@@$$J224YGPAUHINSTANCE__@@PAUHWND__ @@PBD111H@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)

    C:\Documents and Settings\Vindra\My Documents\Visual Studio 2005\Projects\time\Release\time.exe : fatal error LNK1120: 2 unresolved externals

    Last edited by Hexxx; 01-01-2006 at 10:04 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can fire off another process or use a thread. Why are you wanting to do this?

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    well I don't know what you mean by that...

    I want to be able to show some stuff on the screen (well the console screen anyway), and then make the program load.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    It performs well when I test it on my machine.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    Try linking to `shell32.lib'.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    ok that's it, I know the problem has something to do with linking...how do I link to "shell32.lib" ? I'm a beginner on MS Visual Studio 2005

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In the project settings, the linker option, input tab, there's an "additional libraries" field. Just put the library name in there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a C# program with a manifest file to run in the debugger
    By Xarzu Athanasop in forum C# Programming
    Replies: 0
    Last Post: 01-18-2008, 06:34 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  3. how do I morse code converting program
    By panfilero in forum C Programming
    Replies: 17
    Last Post: 10-29-2005, 09:16 PM
  4. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  5. Trying to make rand different per program run
    By Dreamerv3 in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2002, 03:26 AM