Thread: How to run an exe using c prog

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Question How to run an exe using c prog

    Hi All,
    I am new to this forum. I am in need of a program in C that runs an exe file in Windows.
    While googling I found the code below :

    1.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    int main()
    {
     (void)system("C:\\Windows\\notepad.exe");
     return 0;
    }
    The above code compiles successfully in Borland Turbo C. But it fails to run Notepad.

    2
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    void main()
    {
     int result;
     result=system("C:\\Windows\\notepad.exe");
     printf("%d",result);
    }
    The above code on running gives -1 as output. Why am I getting -1.

    My OS Windows XP
    Borland Turbo C Compiler

    Please help.

    Ujjwal

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Your biggest problem is that your 20+ year old 16-bit (FAT) compiler doesn't have a clue about your 32-bit (NTFS) operating system.

    Get a proper compiler for your actual OS.
    Microsoft Express Downloads - Visual Studio Express and SQL Server Express
    Code::Blocks
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually Salem... the filesystem shouldn't matter, the OS should be handling that.

    His biggest problem is that starting with Windows 2000 notepad.exe is in the system32 folder.

    system("c:\\windows\\system32\\notepad.exe") should work.

    But yes, he does need to get rid of that dinosaur compiler....

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Thumbs up

    Quote Originally Posted by Salem View Post
    Your biggest problem is that your 20+ year old 16-bit (FAT) compiler doesn't have a clue about your 32-bit (NTFS) operating system.

    Get a proper compiler for your actual OS.
    Microsoft Express Downloads - Visual Studio Express and SQL Server Express
    Code::Blocks

    Perhaps you are right . As I said earlier it compiles successfully but the editor (Borland) fails to run the exe .I manually ran the compiled exe from command prompt then it opened the notepad successfully.



    Quote Originally Posted by CommonTater View Post
    Actually Salem... the filesystem shouldn't matter, the OS should be handling that.

    His biggest problem is that starting with Windows 2000 notepad.exe is in the system32 folder.

    system("c:\\windows\\system32\\notepad.exe") should work.

    But yes, he does need to get rid of that dinosaur compiler....

    No path is not the problem. See this post
    Why are there two copies of Notepad? - The Old New Thing - Site Home - MSDN Blogs

    I Think the real problem is with the compiler as both of you suggested.

    Thanks for your replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Run program on webserver
    By Yarin in forum Tech Board
    Replies: 14
    Last Post: 08-28-2009, 11:30 AM
  2. how to run a C executable on Windows/Linux
    By Chandana in forum C Programming
    Replies: 5
    Last Post: 08-11-2009, 12:02 AM
  3. Dev C++: can't run code
    By blurrymadness in forum C++ Programming
    Replies: 0
    Last Post: 08-07-2009, 07:33 PM
  4. triggering another program to run using serial port
    By infineonintern in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2009, 05:16 AM
  5. Making an exe run on a diff. machine
    By Garnock in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 01:50 PM