简体   繁体   中英

How to execute batch (.bat) file in Windows with C

I need to program something with C that can execute .bat file on Windows.

Naturally, the system() function should come to mind. However, there is a huge issue . Basically, the function system() does not get executed.

Things I tried:

1) Compile and run my code from Cygwin terminal does work, however, this is not really viable because I need to move my program to different PC, and having to install Cywind in every single one of then is just a bad idea.

2) I have tried to cd to C:\\Windows\\System32\\ and still does not work.

3) I have tried with popen() , and still not working.

4) Could it be because of the compiler I am using: Cywin64 on CLion? But even if I changed the environment to Windows Subsystem for Linux, it still does NOT work!

At this point, i have lost many brain cell and have pretty much given up.

After trying everything, I think the reason is either system() would tried to run the command from /bin/sh , which is not available when running from a normal cmd console window, or system() is just outright broken. Also, not only running batch, pretty much any other cmd command would not work. Heck, even ls will not work.

Googling does not help at all.

All I really want is to execute .bat file inside a C program. Basically it. Any advice?

ShellExecute() does not work. Same thing happened with System() .

My solution is to download Microsoft Visual Studio, and run Clion through with VS environment (need to fix certain compatibility issue).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM