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.
There are at least two wrong things here:
system()
;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:
avoid using system
: apart from its other flaws, in all these scenarios your application wouldn't have any clue that notepad
didn't start;
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
;
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;
if you want to open a file, use ShellExecute
: it will automatically open that file with the associated application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With