system()函数未按预期执行shell命令

时间:2018-10-15 10:11:03

标签: c printf registry command-prompt

我有下面的command,它从注册表中读取 OS 名称数据,如下所示:

  

for /f "tokens=2*" %a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName ^| findstr "REG_"') do @echo %b

output :    Windows 10 Enterprise

所以现在,此命令在 命令提示符 中可以使用,但是当我在system()函数中使用它时,它无法按预期运行。 / p>

我的code

CHAR szCommandLine[MAX_PATH * 4] = { 0 };
StrCpyA(szCommandLine, "for /f \"tokens = 2*\" %%a in ('reg query \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\" /v ProductName ^| findstr \"REG_\"') do @echo %%b");
// print the command
printf(szCommandLine);
printf("\n");
// run the command
system(szCommandLine);

输出:

  

for /f "tokens=2*" %a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName ^| findstr "REG_"') do @echo %b

     

%%a was unexpected at this time.

现在,我不确切知道system()函数中发生的行为错误的command。实际上,我程序中打印的command 命令提示符 中正确运行的命令完全相同。

1 个答案:

答案 0 :(得分:0)

根据IanAbbott 的要点:

我在命令参数中替换了%a而不是%%a,因为%%%%函数中的system()是相同的,并且在{{ 1}}。

command

相关问题