Menu
This C program prints IP (internet protocol) address of your computer, system function is used to execute the command ipconfig which prints IP address, subnet mask, and default gateway. The code given below works for Windows XP and Windows 7.
system() plays a very special role in executing Operating System Commands.
By using this library function we can run all those terminal commands that our Operating System allows us to perform, just by using our C program.
By using this library function we can run all those terminal commands that our Operating System allows us to perform, just by using our C program.
Now, we will learn a very simple code to fetch the IP Address – to identify each computer using that very internet Protocol to communicate.
Let’s see how.
Let’s see how.
Windows
// Windows system. //<stdlib.h> library has system() library function. { } |
Linux
// Linux system. //<stdlib.h> library has system() library function. { } |
Output :
Now we are looking at a code to ShutDown your system.
Windows
#include<stdlib.h> { system ( 'C:WINDOWSSystem32shutdown /s' ); // For Windows XP } |
Linux
// Linux system. //<stdlib.h> library has system() library function. { } |
Output :
Run these codes on your System and have fun. ?
This article is contributed by Mohit Gupta_OMG ?. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
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:
The above code compiles successfully in Borland Turbo C. But it fails to run Notepad.
2Code:
The above code on running gives -1 as output. Why am I getting -1.
My OS Windows XPBorland Turbo C Compiler
Please help.
UjjwalUjjwal
6 Answers
There are at least two wrong things here:
- you're using
system()
; - 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:
- avoid using
system
: apart from its other flaws, in all these scenarios your application wouldn't have any clue thatnotepad
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 APIGetWindowsDirectory
; - if your app is in
PATH
, you may exploit this fact: theWindows
andsystem32
directory are in thePATH
environment variable, which means that, if you just try to startnotepad
, 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.
Community♦
Matteo ItaliaMatteo Italia
I'm not sure notepad has ever been stored in the Windows directory. This code works under WinXP.
Mike Sherrill 'Cat Recall'Mike Sherrill 'Cat Recall'
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.objhello.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
alamalam
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.
Kumar AlokKumar Alok
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'.
mikabytesmikabytes
use _wpopen function (windows version of popen)
source: http://msdn.microsoft.com/en-us/library/96ayss4b.aspx
source: http://msdn.microsoft.com/en-us/library/96ayss4b.aspx
anilbeyanilbey