4

When trying to retrieve current process command line info using wmic via the system2 interface:

system2(
  command = "wmic",
  args = paste0("process where processid=", Sys.getpid(), " get commandline"),
  stdout = TRUE
)

I get the following output and warnings:

[1] "ÿþC" ""    ""    ""    ""   
Warning messages:
1: In readLines(rf) : line 1 appears to contain an embedded nul
2: In readLines(rf) : line 2 appears to contain an embedded nul
3: In readLines(rf) : line 3 appears to contain an embedded nul
4: In readLines(rf) : line 4 appears to contain an embedded nul
5: In readLines(rf) : line 5 appears to contain an embedded nul
6: In readLines(rf) :
  incomplete final line found on '...\Temp\RtmpaWqnBy\file1284c476b1'

Interestingly, when using system, to get the same desired output (command line info on the current process) it seems to work fine:

system(
  command = paste0("wmic process where processid=", Sys.getpid(), " get commandline"),
  intern = TRUE
)

Output:

[1] "CommandLine                                                     \r"         "\"C:\\Program Files\\R\\R-3.6.2\\bin\\x64\\Rgui.exe\" --cd-to-userdocs  \r"
[3] "\r"  

Also, system2 with stdout = "" prints the output to console fine:

system2(
  command = "wmic",
  args = paste0("process where processid=", Sys.getpid(), " get commandline"),
  stdout = ""
)
CommandLine                                                     

"C:\Program Files\R\R-3.6.2\bin\x64\Rgui.exe" --cd-to-userdocs

I tried specifying the args argument to system2 in different ways, but with no luck. Could someone provide an explanation of the warnings? Some sessionInfo below:

  • R version 3.6.2 (2019-12-12), Running under: Windows 10 x64
  • Platform: x86_64-w64-mingw32/x64 (64-bit)
  • locale: LC_COLLATE=English_United Kingdom.1252

Some details on reproducing:

  • In an active R session, this issue only manifests for me within RGui, not e.g. in RStudio or an R session ran from command prompt (cmd.exe)
  • When the code is executed on startup (e.g. via .Rprofile), it manifests in both RGui and RStudio, but not in an R processes created from command prompt (cmd.exe)
Jozef
  • 2,617
  • 14
  • 19
  • What do you expect `wmic` to return? It looks like you are getting binary data but trying to convert it to character strings. – MrFlick Apr 06 '20 at 19:36
  • For that specific process it is: CommandLine "C:\Program Files\R\R-3.6.2\bin\x64\Rgui.exe" --cd-to-userdocs ... `system` returns it correctly, system2 also works fine - unless `stdout = TRUE` – Jozef Apr 06 '20 at 19:37
  • Oddly it seems to work just fine inside Rstudio. I can only get the error when running it inside the default R GUI. – MrFlick Apr 06 '20 at 19:48
  • 1
    Hmm. I wonder if it's related to [this bug](https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17508). It seems you can set `stdout="file.out"` and the data is sent there. But the data seems to be encoded as UTF-16 which seems to be causing the problem. You can't just `readLines()` on that file which is what `system2` is trying to do. You would need to do something like `readLines(file("file.out", encoding="UTF-16"))`. I see there is a check for `.Platform$GUI == "Rgui"` in the `system2` code, so maybe that branch is causing the problem and not causing a problem with RStudio? – MrFlick Apr 06 '20 at 20:08
  • Thanks for checking, I added some details on reproducing to the original question. – Jozef Apr 06 '20 at 20:17
  • 1
    Interestingly, it happens for me in R3.6.3 but not in R3.5.2 and I see that `system2()` got a major overhaul. – Bob Jansen Apr 07 '20 at 18:41
  • 2
    Additionally, if I `debug()` `system2()` and look at the tempfile in a Notepad++ or in a hex editor I see that the file is encoded in 'UCS-2 LE BOM'. Converting the file and continuing gives the desired result. So, I say it's a bug in `system2()`: it creates a file for reading back which it can't actually do. A patch would be to replace the `readLines()` call with `readLines(file(rf, encoding = "UCS-2LE"))` but that might (will?) not work in general. – Bob Jansen Apr 07 '20 at 18:59

0 Answers0