module Process

def create_process_as_user(token, app, cmd, process_security,

def create_process_as_user(token, app, cmd, process_security,
  thread_security, inherit, creation_flags, env, cwd, startinfo, procinfo)
  bool = CreateProcessAsUserW(
    token,            # User token handle
    app,              # App name
    cmd,              # Command line
    process_security, # Process attributes
    thread_security,  # Thread attributes
    inherit,          # Inherit handles
    creation_flags,   # Creation Flags
    env,              # Environment
    cwd,              # Working directory
    startinfo,        # Startup Info
    procinfo          # Process Info
  )
  unless bool
    msg = case FFI.errno
          when ERROR_PRIVILEGE_NOT_HELD
            [
              %{CreateProcessAsUserW (User '%s' must hold the 'Replace a process},
              %{level token' and 'Adjust Memory Quotas for a process' permissions.},
              %{Logoff the user after adding this right to make it effective.)},
            ].join(" ") % ::ENV["USERNAME"]
          else
            "CreateProcessAsUserW failed."
          end
    raise SystemCallError.new(msg, FFI.errno)
  end
end