InstallScript can not read text file containing result from command prompt

余生长醉 提交于 2019-12-24 06:08:26

问题


I am writing a Installscript function to run a command in command prompt, redirect the result from console to a text file, then read the text file for information.

// send command method
STRING szCmdPath, szCmdLine ;
szCmdPath = "C:\\WINDOWS\\system32\\cmd.exe";
szCmdLine = "/c wslconfig /l > D:\\output.txt";
LaunchAppAndWait(szCmdPath, szCmdLine, WAIT);

the send command method did not run the command with szCmdLine as I desired, it failed to recognize the command and produce the following error:

'wslconfig' is not recognized as an internal or external command, operable program or batch file.

However, if I start cmd.exe manually instead of using my script, it runs the command perfectly fine. What is wrong with my script and how to fix these problems? Thank you all in advance.


回答1:


I see two potentially confusing elements here. One is filesystem redirection of 32-bit processes (resulting in loading a 32-bit cmd.exe that cannot find wslconfig). The other is a question of whether command line processing of the output redirection will do what you want.

To test, here are some things you could try:

  • Run a test from an explicitly 32-bit command prompt (c:\Windows\SysWow64\cmd.exe)
  • Run a different command, such as cmd /c echo got-it > D:\output.txt

I suspect you may have to address both, but strongly believe the 32-bit context is problematic. To address the context, consider altering your code to the following, using WINSYSDIR64:

...
szCmdPath = WINSYSDIR64 ^ "cmd.exe";
...
Disable(WOW64FSREDIRECTION);
LaunchAppAndWait(...)
Enable(WOW64FSREDIRECTION);

(As an alternate approach, you can use C:\Windows\Sysnative from a 32-bit context to access the 64-bit folder without disabling WOW64FSREDIRECTION. Unfortunately there isn't a variable populated with that path so you have to construct or hard code that path.)

To address the output potential redirection problem, consider quoting the arguments to /c:

...
szCmdLine = "/c \"wslconfig /l > D:\\output.txt\"";
...


来源:https://stackoverflow.com/questions/50033968/installscript-can-not-read-text-file-containing-result-from-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!