Jamesits / SvcGuest

Install any executable as a Windows service. (systemd compatible config!)
Mozilla Public License 2.0
49 stars 2 forks source link

Implement `User=` #1

Closed Jamesits closed 6 years ago

Jamesits commented 6 years ago

Problem:

Solution:

  1. Use CreateProcessAsUser to run another instance of SvcGuest.exe
  2. The new instance starts our object program using Process.Start()
Jamesits commented 6 years ago

Caveat:

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool CreateProcessAsUser(
    IntPtr hToken,
    string lpApplicationName,
    string lpCommandLine,
    ref SECURITY_ATTRIBUTES lpProcessAttributes,
    ref SECURITY_ATTRIBUTES lpThreadAttributes,
    bool bInheritHandles,
    uint dwCreationFlags,
    IntPtr lpEnvironment,
    string lpCurrentDirectory,
    ref STARTUPINFO lpStartupInfo,
    out PROCESS_INFORMATION lpProcessInformation);  

lpCommandLine must have a space as the first character, otherwise it will fail and return error 123. Also it is recommended to write "{Program Path}" --args as lpCommandLine (otherwise the first argument will not be the program path itself, may cause some program to fail).