Using FlashWindowEx in a CMD batch file

102 Views Asked by At

I have a CMD batch file which takes a long time to process and I wish to alert the user that some input is required by flashing the CMD window.

This example works great in powershell but if I try to compile it from within CMD by

powershell -c "Add-Type -TypeDefinition @\";"^
 "using System;"^
..... and so on

it fails at the very first line. Similar problem discussed here but looks like there was never a solution given.

So, does anyone have ideas as to how I can make this work and get my CMD window to flash?

edit: @mklement0's answer got me in the right direction. THANK YOU.

1

There are 1 best solutions below

2
On

Passing multiline PowerShell commands from cmd.exe is nontrivial and requires lots of careful escaping.

Here's a minimal example with Add-Type: Using C# code, it defines class [Foo] with a static Bar() method and then calls it from PowerShell:

powershell -noprofile -c Add-Type -TypeDefinition ' ^
  public class Foo { ^
    public static string Bar() { return \"hi!\"; } ^
  } ^
  '; ^
  [Foo]::Bar()
  • An overview of the escaping rules is in this answer; a few things worth noting here:

    • Here-strings (@"<newline>...<newline>"@ and @'<newline>...<neline>'@) cannot be used, because they have strict whitespace requirements, notably requiring a newline both immediately after the opening @" / @' as well as before the closing @" / @', whereas cmd.exe's line-continuation with ^ does not result in newlines. Therefore, your approach won't work (even with the extraneous ; after the opening @" removed).

    • Use '...' (verbatim (single-quoted) strings), where possible, which simplifies escaping.
      Note that PowerShell's regular string literals, not just here-strings, are also capable of spanning multiple lines, which the solution above takes advantage of.

    • When you do need to use ", escape them as \" - if a " occurs alone or there is an uneven number of them on a given interior line, escape it / the last one as \^" (sic).

      • Note that anything outside \"...\" is seen as unquoted by cmd.exe, so that metacharacters such as & and | require individual ^-escaping there.
  • You should be able to apply these rules to your FlashWindowEx P/Invoke declaration.