starting interactive process from windows service

Hello there,

Don't give any suggestions or ask why i need to do this because i have spent too much time with it witout any success. I have a windows service running under system account, interacting with desktop. i need to launch a Gui application written in vb.net(or any Gui app) from my service(also written in vb.net). i am monitoring a program and need to restart when/if it dies. I can launch application and it does show up the gui, user can interact with it as well but only problem is that it runs under system account and i want to make it run under current logged in user(there is going to be only one user logged in to the system where this service is going to run, as we are providing a complete solution where we control all the attributes of system). Operating system is going to be win2000 pro and winxp pro sp2.

Qaiser.



Answer this question

starting interactive process from windows service

  • JWSUN

    The service is going to know user/password but i m not sure i can do that what u refer, i did have red this in a couple of articles but have not found any source code to do this, can you please refer me to some working code example for vb.net .net framework 1.1

    thanx in advance.


  • Yeshia

  • Rajah

    This is a bit on the challenging side. The service has to know if there is a current user and WHO. By WHO I mean it has to have knowledge of or access to the user username and password. If it has this... it can interact with the desktop.

    This work is done at the API level. I am unsure if you can do anything with tokens and impersonation or not.



  • Kumar Mridesh

    Hi there,

     

    thanx  for quick response but i did come across that article before, i fully understand  interactive services. I did try createprocessasuser and createprocesswithlogon but somehow they are not working. The first method i tried is

     

    #Region "Structs"

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure PROCESS_INFORMATION

    Dim hProcess As System.IntPtr

    Dim hThread As System.IntPtr

    Dim dwProcessId As Integer

    Dim dwThreadId As Integer

    End Structure

     

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure STARTUPINFO

    Dim cb As Integer

    Dim lpReserved As System.IntPtr

    Dim lpDesktop As System.IntPtr

    Dim lpTitle As System.IntPtr

    Dim dwX As Integer

    Dim dwY As Integer

    Dim dwXSize As Integer

    Dim dwYSize As Integer

    Dim dwXCountChars As Integer

    Dim dwYCountChars As Integer

    Dim dwFillAttribute As Integer

    Dim dwFlags As Integer

    Dim wShowWindow As Short

    Dim cbReserved2 As Short

     

    ' you had this as a byte, but it is LPBYTE or byte*

    ' so should be an IntPtr

    Dim lpReserved2 As System.IntPtr

     

    Dim hStdInput As System.IntPtr

    Dim hStdOutput As System.IntPtr

    Dim hStdError As System.IntPtr

    End Structure

    #End Region

    #Region "APIINFO"

    Private Const LOGON_NETCREDENTIALS_ONLY As Integer = &H2

    Private Const NORMAL_PRIORITY_CLASS As Integer = &H20

    Private Const CREATE_DEFAULT_ERROR_MODE As Integer = &H4000000

    Private Const CREATE_NEW_CONSOLE As Integer = &H10

    Private Const CREATE_NEW_PROCESS_GROUP As Integer = &H200

    Private Const LOGON_WITH_PROFILE As Integer = &H1

     

    Private Declare Unicode Function CreateProcessWithLogon Lib "Advapi32" Alias "CreateProcessWithLogonW" _

    (ByVal lpUsername As String, _

    ByVal lpDomain As String, _

    ByVal lpPassword As String, _

    ByVal dwLogonFlags As Integer, _

    ByVal lpApplicationName As String, _

    ByVal lpCommandLine As String, _

    ByVal dwCreationFlags As Integer, _

    ByVal lpEnvironment As System.IntPtr, _

    ByVal lpCurrentDirectory As System.IntPtr, _

    ByRef lpStartupInfo As STARTUPINFO, _

    ByRef lpProcessInfo As PROCESS_INFORMATION) As Integer

     

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As System.IntPtr) As Integer

    #End Region

     

    and in my service somewhere i need to start process

    Dim szApp As String = "notepad.exe"

    Dim szCmdLine As String = String.Empty

    Dim szUser As String = "user"

    Dim szPass As String = "pass"

    Dim szDomain As String = "home"

    Dim siStartup As STARTUPINFO

    Dim piProcess As PROCESS_INFORMATION

     

    siStartup.cb = Marshal.SizeOf(siStartup)

    siStartup.dwFlags = 0

     

    Dim ret As Integer = CreateProcessWithLogon(szUser, szDomain, szPass, LOGON_WITH_PROFILE, szApp, szCmdLine, _

    NORMAL_PRIORITY_CLASS Or CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, _

    IntPtr.Zero, IntPtr.Zero, siStartup, piProcess)

     

    If ret = 0 Then

    MsgBox((Marshal.GetLastWin32Error()).ToString)

    End If

     

    CloseHandle(piProcess.hProcess)

    CloseHandle(piProcess.hThread)

     

    With this process i start don't show any GUI but process start under the desired user account

     

    The second method i tried is

     

    Public Const NORMAL_PRIORITY_CLASS As Integer = &H20

     

    Public Structure SECURITY_ATTRIBUTES

    Dim nLength As Integer

    Dim lpSecurityDescriptor As IntPtr

    Dim bInheritHandle As Boolean

    End Structure

     

    Public Structure STARTUPINFO

    Dim cb As Integer

    Dim lpReserved As String

    Dim lpDesktop As String

    Dim lpTitle As String

    Dim dwX As Integer

    Dim dwY As Integer

    Dim dwXSize As Integer

    Dim dwYSize As Integer

    Dim dwXCountChars As Integer

    Dim dwYCountChars As Integer

    Dim dwFillAttribute As Integer

    Dim dwFlags As Integer

    Dim wShowWindow As Int16

    Dim cbReserved2 As Int16

    Dim lpReserved2 As Byte

    Dim hStdInput As IntPtr

    Dim hStdOutput As IntPtr

    Dim hStdError As IntPtr

    End Structure

     

    Public Structure PROCESS_INFORMATION

    Dim hProcess As IntPtr

    Dim hThread As IntPtr

    Dim dwProcessId As Integer

    Dim dwThreadId As IntPtr

    End Structure

     

    <DllImport("kernel32.dll", SetlastError:=True, CharSet:=CharSet.Auto)> _

    Public Shared Function CreateProcess(ByVal lpApplicationName As String, _

    ByVal lpCommandLine As String, _

    ByRef lpProcessAttributes As SECURITY_ATTRIBUTES, _

    ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, _

    ByVal bInheritHandles As Boolean, _

    ByVal dwCreationFlags As Integer, _

    ByVal lpEnvironment As IntPtr, _

    ByVal lpCurrentDirectory As IntPtr, _

    ByRef lpStartupInfo As STARTUPINFO, _

    ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean

    End Function

     

    and in my service when i need to start process

     

    Dim sa As SECURITY_ATTRIBUTES

    Dim si As STARTUPINFO

    Dim pi As PROCESS_INFORMATION

    si.lpDesktop = Nothing

     

    sa.nLength = Marshal.SizeOf(sa)

    sa.lpSecurityDescriptor = IntPtr.Zero

     

    si.cb = Marshal.SizeOf(si)

     

    CreateProcess(Nothing, "notepad.exe", sa, sa, False, NORMAL_PRIORITY_CLASS, IntPtr.Zero, IntPtr.Zero, si, pi)

     

    this way i can see GUI but the started process runs under System account and i want it to run under logged in user account.


  • starting interactive process from windows service