从C ++运行批处理文件 [英] Running a Batch File From C++

查看:91
本文介绍了从C ++运行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C ++代码运行批处理文件.系统("sample.bat").我的问题是,当我完成C ++程序并将其转换为exe时.有没有办法包含bat文件,使其包含在程序中?谢谢.

I''m running a batch file from C++ code. system("sample.bat"). My question is, when I finish my C++ program and turn it into an exe. Is there a way to include the bat file so that it is included in the program? Thanks.

推荐答案

一个想法是将其添加为exe中的(文本)资源.然后,如果可能,逐行执行,或者将其写入临时文件并从那里运行.
One idea is to add it as a (text-) resource in your exe. Then either execute it row by row if possible, or write it to a temporary file and run it from there.


如果您使用批处理文件来运行任何应用程序,则可以在自己的代码中编写代码.相应操作程序

或者,您可以使用此批处理文件创建部署设置,并将其创建为安装文件以安装到其他计算机上.
If you are using batch file for running any application you can write code in your program for corresponding operation

Or you can create a deployment setup with this batch file and create it as a setup file to install to other machines


通常,从程序中调用system 来执行系统命令不是良好实践":不能保证系统确实按照您的要求进行操作,特别是在有人将其弄乱以其他程序替换命令的情况下.
基本上,每次对系统"的调用都是外部钩子病毒,编写者可以使用它们来附加自己的操作.

有C库功能来管理文件以及操作系统本机API.你应该依靠他们.

当然,所有这些都需要上下文化:如果您的程序在有限且受控的环境中运行(可以假定您知道系统将做的所有事情并且可以授予它将不做其他任何事情)就可以做到.
In general calling system from a program to execute system commands is not a "good practice": there is no way to be granted that the system really do what you asked, especially in the case someone has messed it up replacing commands with other programs.

Basically, every call to "system" is an external hook virus writers can use to attach their own actions.

There are C library fuctions to manage files, as well as operating system native APIs. You should rely on them.

Of course, all that needs to be contextualized: if your program runs in a limited and controlled environment, (where it can be assumed you know everything the system will do and can be granted it will not do anything else) that can be done.


这篇关于从C ++运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆