用C++实现批处理中的每一行指令

YKGFRJ 2013-05-08 08:17:14

@echo off
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ControlStorageDevicePolicies" /v WriteProtect /t reg_dword /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR" /v Start /t reg_dword /d 4 /f
copy %Windir%\inf\usbstor.inf %Windir%\usbstor.inf /y >nul
copy %Windir%\inf\usbstor.pnf %Windir%\usbstor.pnf /y >nul
del %Windir%\inf\usbstor.pnf /q/f >nul
del %Windir%\inf\usbstor.inf /q/f >nul
@echo on
这几行命令行,如何在C++中用system函数实现
...全文
1285 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanae 2013-05-13
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
#include <stdio.h>
#include <stdlib.h>
FILE *f;
int main() {
 f=fopen("a.bat","w");
 fprintf(f,
 "@echo off\n"
 "reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\ControlStorageDevicePolicies\" /v WriteProtect /t reg_dword /d 1 /f\n"
 "reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\USBSTOR\" /v Start /t reg_dword /d 4 /f\n"
 "copy %%Windir%%\\inf\\usbstor.inf %%Windir%%\\usbstor.inf /y >nul\n"
 "copy %%Windir%%\\inf\\usbstor.pnf %%Windir%%\\usbstor.pnf /y >nul\n"
 "del %%Windir%%\\inf\\usbstor.pnf /q/f >nul\n"
 "del %%Windir%%\\inf\\usbstor.inf /q/f >nul\n"
 "@echo on\n"
 );
 fclose(f);
 system("a.bat");
 remove("a.bat");
 return 0;
}
楼上的代码可能会有找不到批处理文件的错误,因为windows下的C运行时并不会等system返回,于是remove干掉bat之后bat没有执行完的语句已经不能起作用了。 假设你的bat叫做1.bat:下面的代码不总是能工作正常。

echo 1
echo 2
del 1.bat
echo 3
echo 4
一叶之舟 2013-05-08
  • 打赏
  • 举报
回复
将这些命令写到一个a.bat文件中,C++中调用system("a.bat"); 也可以用system执行每一条命令。
buyong 2013-05-08
  • 打赏
  • 举报
回复
system("@echo off reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ControlStorageDevicePolicies" /v WriteProtect /t reg_dword /d 1 /f reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR" /v Start /t reg_dword /d 4 /f copy %Windir%\inf\usbstor.inf %Windir%\usbstor.inf /y >nul copy %Windir%\inf\usbstor.pnf %Windir%\usbstor.pnf /y >nul del %Windir%\inf\usbstor.pnf /q/f >nul del %Windir%\inf\usbstor.inf /q/f >nul @echo on");
qj5656 2013-05-08
  • 打赏
  • 举报
回复
上楼说的对,直接调用批出理文件就可以了。
图灵狗 2013-05-08
  • 打赏
  • 举报
回复
用system函数直接调用整个批处理文件,不要单独去调用某一行。
赵4老师 2013-05-08
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
FILE *f;
int main() {
 f=fopen("a.bat","w");
 fprintf(f,
 "@echo off\n"
 "reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\ControlStorageDevicePolicies\" /v WriteProtect /t reg_dword /d 1 /f\n"
 "reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\USBSTOR\" /v Start /t reg_dword /d 4 /f\n"
 "copy %%Windir%%\\inf\\usbstor.inf %%Windir%%\\usbstor.inf /y >nul\n"
 "copy %%Windir%%\\inf\\usbstor.pnf %%Windir%%\\usbstor.pnf /y >nul\n"
 "del %%Windir%%\\inf\\usbstor.pnf /q/f >nul\n"
 "del %%Windir%%\\inf\\usbstor.inf /q/f >nul\n"
 "@echo on\n"
 );
 fclose(f);
 system("a.bat");
 remove("a.bat");
 return 0;
}

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧