How to run exe with c prog

I am new to this forum. I need a C program that runs an exe file on 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 successfully compiles into Borland Turbo C. But it does not start 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 startup gives -1 as output. Why am I getting -1.

My Windows XP Borland Turbo C Compiler

Please, help.

+4
source share
6 answers

There are at least two wrong things here:

  • you are using system() ;
  • You hardcode the path.

For the first problem, I wrote a long story for a long time, you can look at it here ; a long story, in order to start the process, you have to go with the platform-specific way, namely in Windows, CreateProcess or, if you want to open the file using the application associated with it, ShellExecute .

For the second problem, you assume (1) that c:\windows exists, (2) that it is the Windows directory of the current Windows instance, (3) that notepad.exe actually exists, and (4) that it is in such a directory.

While notepad.exe is pretty much guaranteed to exist with every Windows installation, it is not clear where to look for it. Since Windows 3.0 it was in the Windows directory, but in the NT family it remained in the system32 subdirectory. Thus, from some version of Windows, Microsoft placed two copies of notepad, both in the windows directory and in the system32 directory (see this blog ).

Additional fun: a copy was deleted from Windows Server 2008 from the Windows directory (the link is, by the way, the message header What idiot will hardcode the path to Notepad?: :D ), so your program will not be able to open notepad , even if Windows is in c:\windows .

But the biggest problem is that Windows is not guaranteed to install in c:\windows ; on every Windows NT family prior to Windows XP, it was actually installed by default in c:\winnt , so your code will not work here.

In addition, if you have more than one copy of Windows installed (for example, Windows 7 64 bit on c: 32-bit version of Windows XP on d: c:\windows may actually exist, but may contain a copy of Windows other than the one is currently running, so you open notepad from another copy of Windows (and if this copy is 64-bit and the working one is 32 bit, it will not work).

This can happen when installing Windows on a disk that already contains the windows directory; in this case, the installation will put Windows in the Windows(01) directory Windows(01) (or something like that), and c:\windows may be empty.

Shortly speaking:

  • avoid using system : among other disadvantages, in all of these scenarios your application would have no clue that notepad not starting;

  • avoid hardcoded paths: c:\windows not guaranteed; if you need to get the path to the Windows directory, you can expand the environment variable %windir% (or %systemroot ) or use the GetWindowsDirectory API;

  • if your application is in PATH , you can take advantage of this fact: the windows and system32 directory is in the PATH environment variable, which means that if you just try to start notepad , you can avoid specifying the full path; on the other hand, you are exposed to vulnerabilities if an attacker puts a dangerous application in the working directory of your application;

  • if you want to open the file, use ShellExecute : it will automatically open this file with the corresponding application.

+12
source

Look where you save the source file, C ++ compilers always generate two files, say your source named "hello.cpp" These files should be in the source path: hello.obj hello.exe <- your distribution program

and

I think you should use the new free IDE / compiler for a better result, for example: CodeBlocks at http://www.codeblocks.org

+2
source

As I understand it, I see no problems with the code, you tried to run the program using some standard IDEs, such as dev-cpp or code-blocks.

And do one thing

first try running the same command on the command line and reporting the result.


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

This is unlikely, but there is a chance.

thanks

Alok Kr.

+1
source

Maybe your path is in some way wrong. I would advise using the Kumar tip and trying to run it on the command line first to make sure that you are using the correct path.

Alternatively, you can try running notepad.exe without a path. Since it is in PATH, you should only specify "notepad.exe".

+1
source

I'm not sure that notepad was always 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; } 
+1
source

use the _wpopen function (popen window version)
source: http://msdn.microsoft.com/en-us/library/96ayss4b.aspx

0
source

Source: https://habr.com/ru/post/1335727/


All Articles