6

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.

2
  • 3
    Do not assume that C:\Windows exists.
    – SLaks
    Jan 16, 2011 at 14:02
  • 2
    Beware that system() is a blocking call, and probably what you want here, but it is not always what someone finding this question by it's title might need :)
    – user50049
    Jan 16, 2011 at 14:26

5 Answers 5

15

There are at least two wrong things here:

  1. you're using system();
  2. you're hardcoding a path.

For the first problem, I already wrote a long rant some time ago, you can have a look at it here; long story short, to start a process you should go with the platform-specific way, namely, on Windows, CreateProcess or, if you want to open a file with it's associated application, ShellExecute.

For the second problem, you're assuming (1) that c:\windows exists, (2) that it is the windows directory of the currently running windows instance (3) that notepad.exe actually exists and (4) that it is in such directory.

While notepad.exe is pretty much guaranteed to exist on every Windows installation, it's not clear where you should search it. Since Windows 3.0 it was in the Windows directory, but on the NT family it used to stay in the system32 subdirectory. So, from some Windows version onward Microsoft put two copies of notepad, both in the windows directory and in the system32 directory (see this blog post).

Additional fun: from Windows Server 2008 the copy from the Windows directory has been removed (link - incidentally, the title of the post is What idiot would hard-code the path to Notepad? :D), so your program will fail to open notepad even if Windows resides in c:\windows.

But the biggest problem here is that Windows isn't guaranteed to be installed in c:\windows; on every NT-family Windows before Windows XP it was actually installed by default in c:\winnt, so your code would fail here.

Moreover, if you have more than one copy of Windows installed (say Windows 7 64 bit on c:, Windows XP 32 bit on d:) c:\windows may actually exist, but it may contain a copy of Windows different from the one currently executing, so you'd be opening the notepad from another copy of Windows (and if that copy is 64 bit and the running one is 32 bit it won't run).

Similar stuff may happen also if you install Windows on a disk that already contains a windows directory; in that case the setup will put Windows in a Windows(01) directory (or something like that), and c:\windows may be empty.

Long story short:

  1. avoid using system: apart from its other flaws, in all these scenarios your application wouldn't have any clue that notepad didn't start;

  2. avoid hardcoding paths: c:\windows isn't guaranteed to exist; if you need to get the path of the Windows directory, you can expand the environment variable %windir% (or %systemroot), or use the API GetWindowsDirectory;

  3. if your app is in PATH, you may exploit this fact: the Windows and system32 directory are in the PATH environment variable, which means that, if you just try to start notepad, you can avoid to specify it's full path; on the other hand, you're exposing yourself to vulnerabilities if a malicious user put a dangerous application in the working directory of your application;

  4. if you want to open a file, use ShellExecute: it will automatically open that file with the associated application.

1
  • 1
    I think this is the best answer, much more useful than all others.. ;)
    – BlackBear
    Jan 16, 2011 at 14:44
2

I'm not sure notepad has ever been stored in the Windows directory. This code works under WinXP.

#include<stdlib.h>
#include<stdio.h>

/* main() returns int, not void. */
int main( void ) {

  int result ;
  result=system("C:/Windows/system32/notepad.exe");
  printf("%d",result);
  return 0;
}
4
  • at least it works if your windows is on C: if not give %windir% a try
    – mbx
    Aug 14, 2011 at 14:13
  • Not all compilers will expand %windir% correctly. GCC under Cygwin won't. (That's an observation, not a criticism.) Aug 14, 2011 at 20:44
  • As on my Systems C: aint the system drive, your code does not work for me. Compiling it with %windir% at least works on my dev system (win32/msys) and another one. I guess %windir% should be expanded at runtime.
    – mbx
    Aug 14, 2011 at 20:55
  • The OP was having trouble calling system() with a literal path name using Turbo C on his own computer. If I were teaching him in a class, I'd make sure he could do that manually without errors before I'd introduce him to environment variables. (shrug) Aug 14, 2011 at 23:10
2

Look where you save your source file, alway C++ Compilers generate two files, let say your source named "hello.cpp" These files should be in your source path: hello.obj hello.exe <--your prgram to distribut

ALSO

I think you should use new free IDE/Compiler for better result such as:CodeBlocks at http://www.codeblocks.org

1

As per me I dont see any problem with the code, did you try running the program with some standard IDE like, dev-cpp or code-blocks.

And do one thing

try running the same command on the command prompt first and tell the result.


I would also like to tell you to go inside the Windows directory and check if Notepad.exe is there or not.

It is not likely but there is a chance.

Thanks

Alok Kr.

1

Could be that your path is wrong in some way. I would suggest following Kumar's advice and try running it in the command prompt first just to see that you are using the right path.

Also, you might want to try running notepad.exe without a path at all. As it is located in the PATH, you should be able to specify just "notepad.exe".

2
  • I'd let the user set up a different editor during installation, but it is better to call system() with a full path (if only to eliminate obvious reasons for failure).
    – user50049
    Jan 16, 2011 at 14:24
  • @Tim: full path is good if your program verifies that the full path is accurate before using it. Otherwise, full path leads to failure when some program is not where you expect it. Jan 16, 2011 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.