简体   繁体   English

如何使用 C 在 Windows 中执行批处理 (.bat) 文件

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

I need to program something with C that can execute .bat file on Windows.我需要用 C 编写一些可以在 Windows 上执行 .bat 文件的东西。

Naturally, the system() function should come to mind.自然地,应该想到system()函数。 However, there is a huge issue .然而,有一个巨大的问题 Basically, the function system() does not get executed.基本上,函数system()不会被执行。

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. 1)从 Cygwin 终端编译并运行我的代码确实有效,但是,这并不是真正可行的,因为我需要将我的程序移动到不同的 PC,并且必须在每个 PC 中都安装 Cywind 只是一个坏主意。

2) I have tried to cd to C:\\Windows\\System32\\ and still does not work. 2) 我试过 cd 到C:\\Windows\\System32\\还是不行。

3) I have tried with popen() , and still not working. 3)我尝试过popen() ,但仍然无法正常工作。

4) Could it be because of the compiler I am using: Cywin64 on CLion? 4) 可能是因为我使用的编译器:CLion 上的 Cywin64? But even if I changed the environment to Windows Subsystem for Linux, it still does NOT work!但是即使我将环境更改为适用于 Linux 的 Windows 子系统,它仍然不起作用!

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.在尝试了一切之后,我认为原因是system()会尝试从/bin/sh运行命令,这在从普通 cmd 控制台窗口运行时不可用,或者system()完全损坏。 Also, not only running batch, pretty much any other cmd command would not work.此外,不仅运行批处理,几乎任何其他 cmd 命令都不起作用。 Heck, even ls will not work.哎呀,即使是ls也行不通。

Googling does not help at all.谷歌搜索根本没有帮助。

All I really want is to execute .bat file inside a C program.我真正想要的是在 C 程序中执行 .bat 文件。 Basically it.基本上它。 Any advice?有什么建议吗?

ShellExecute() does not work. ShellExecute()不起作用。 Same thing happened with System() . System()发生了同样的事情。

My solution is to download Microsoft Visual Studio, and run Clion through with VS environment (need to fix certain compatibility issue).我的解决方法是下载Microsoft Visual Studio,并在VS环境下运行Clion(需要修复某些兼容性问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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