command-promptinstallshieldinstallscript

InstallScript can not read text file containing result from command prompt


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.


Solution

  • 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:

    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\"";
    ...