1

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.

Thế Long
  • 516
  • 7
  • 19
  • 1
    wslconfig is not in the default path on my Windows 10 Pro. The path variable might be missing whatever path was added for manual launch of cmd.exe when InstallShield is running. MSI has quirks like that too, – Dave S Apr 26 '18 at 03:50
  • well, i did turn on windows feature "Window Sub System for Linux", thus enable wslconfig.exe in the same folder as cmd.exe: C:\WINDOWS\system32 Was I missing something? could you explain the MSI quirks further, please? I really appreciate it. – Thế Long Apr 26 '18 at 04:05
  • I would add the full path to wslconfig to take that out of the picture with your troubleshooting. – Doc Apr 26 '18 at 13:54
  • I followed your suggestion, but result is still the same. I noticed that the only way I can run wslconfig is from command prompt. Directly executing wslconfig.exe does nothing at all. – Thế Long Apr 27 '18 at 03:33

1 Answers1

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\"";
...
Michael Urman
  • 15,737
  • 2
  • 28
  • 44
  • This is indeed the problem, and your answer just save my day. Thanks a bunch. – Thế Long May 02 '18 at 06:39
  • However, I ran into an other problem: How can I read the file using Installscript afterward? Neither GetLine() nor ListReadFromFile() works, even though I did open the file in FILE_MODE_NORMAL (read only mode). But when I manually open, edit and save the file manually, my script can read the file normally. It seems that after I run the command line, the pointer of the file was pointing at the end of file. That brings up an other confusion: Is isn't the FILE_MODE_NORMAL supposes to set the file pointer at the beginning of the file? Your suggestion is highly appreciated. – Thế Long May 02 '18 at 06:45
  • About the last problem (read text file after run command line), someone suggested that the stream or the file could possibly being open so that I can not read the file from my script. But the OpenFile() returned no error at all. Is there a way to check this out/ fix this? – Thế Long May 02 '18 at 06:49
  • If your code is analogous to the [OpenFile example](http://helpnet.flexerasoftware.com/installshield23helplib/Subsystems/LangRef/LangRef.htm#helplibrary/LangrefOpenFile_Example.htm), I wouldn't expect a problem with the file position; they don't persist across file opens, much less separate processes. Perhaps the newlines in the file are unusual? For more eyes, you should ask this as a new question. – Michael Urman May 02 '18 at 15:48
  • I have already posted the question with details, hope you can take a look a tell me what you think. Thank you for your advice. [link](https://stackoverflow.com/questions/50146034/installscript-getline-can-not-read-text-file-contains-result-from-command-prom) – Thế Long May 03 '18 at 02:24