Search code examples
windowscommand-lineregistrystartup

Windows Registry Run Key


According to http://msdn.microsoft.com/en-au/library/aa376977(v=vs.85).aspx:

"Run and RunOnce registry keys cause programs to run each time that a user logs on. The data value for a key is a command line."

Should I then be able to add a key with:

  • Name: MyName
  • Data: START /MIN "Title" "cmd.exe" /c "@echo off && "C:\TestApplication.exe" -Arg1 "Arg2"

with the goal being to start my console application "C:\TestApplication.exe" minimized with arguments "-Arg1 "Arg2"" when Windows starts?

I ask because I cannot seem to get it working.


Solution

  • The documentation is misleading, though not strictly incorrect. The command line is passed directly to CreateProcess() rather than being passed to cmd.exe so commands internal to cmd.exe, such as start, are not valid. This means you need to add cmd /c at the start of the command line.

    You're also missing a quote mark at the end, and you don't need to put quotes around cmd.exe. This works:

    cmd /c START /MIN "Title" cmd /c "@echo off && "C:\TestApp.exe" -Arg1 "Arg2""
    

    However, since the target application is an executable, not a batch file, you don't need the @echo command either:

    cmd /c START /MIN "Title" cmd /c ""C:\TestApp.exe" -Arg1 "Arg2""
    

    (Note that the command line passed to /c is never echoed.)