To invoke vb.net exe from windows service in vb.net

Hello,
I have a VB.net exe which I want to invoke or run from a windows service implemented in VB.NET.
When timer fires, at that time i want to invoke vb.net exe.
 I tried the normal way of invoking applications thruough the process command but it doesn't seem to work in case of Windows services in VB.NET. Please help me out...this is urgent,

code for the above is as:
 Public Sub TimerFired(ByVal sender As Object, ByVal e As ElapsedEventArgs)
        Try
            Dim psList() As Process
            Dim str_Proc As String

            psList = Process.GetProcesses

            For Each p As Process In psList
                str_Proc = p.ProcessName
                If str_Proc.Contains("AutoBackup-Icon") Then p.Kill()
            Next p

            mf_ChkIconExe()
str_IconExePath="C:\Hazel Infotech\AutoBackup Utility\AutoBackup-Icon.exe"

                        Process.Start(str_IconExePath, "Start")
 Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, str_MsgTitle)
        End Try
    End Sub

but,  AutoBackup-Icon.exe doesn't get start..
please help me out, its very urgent......
gopalhazelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
Is the program you're trying to start a GUI program?
CodeCruiserCommented:
>but,  AutoBackup-Icon.exe doesn't get start..
You mean it does not show up in the Processes list or you are unable to see the interface? You would not be able to see its interface because the services, and the processes started by services are run in a separate environment than the current user's desktop.

nffvrxqgrcfqvvcCommented:
<< but,  AutoBackup-Icon.exe doesn't get start.. >>

So are you using the NotifyIcon control? Your service is running under the services session *NOT the desktop session. If your application needs to interacts with the user then you have to open the executable in the desktop session. There is no managed counterpart yet so you will have to use P/Invoke. If your service is running as LocalSystem then use WTSGetActiveConsoleSessionId, WTSQueryUserToken and CreateProcessAsUser. It's also important to create the environment block if the application references special location or uses common dialogs.

Example
http:Q_26273307.html
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

gopalhazelAuthor Commented:
yes, I m using notify control in AutoBackup-Icon.exe  and form size for this exe is of 0 size. so only icon gets displayed in system tray on running AutoBackup-Icon.exe .

so can anybody give me the sample code to start this exe.
CodeCruiserCommented:
Did you try the link posted by eql1044?
nffvrxqgrcfqvvcCommented:
There is an example @ http:Q_26273307.html on how to accomplish opening a process from LocalSystem service onto the current desktop session. Just noticed the example is classic VB6 so it has to be converted.
nffvrxqgrcfqvvcCommented:
Create a new class in your service named ServiceShell.vb then add the following code.

'// Usage
ServiceShell.Start("notepad.exe", Nothing, Nothing)

Open in new window

Imports System.Runtime.InteropServices
Imports System.Text

Public Class ServiceShell
    Private Sub New()
    End Sub
    ' egl1044
    ' Windows XP or later
    Private Const CreateUnicodeEnvironment As Integer = &H400

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    Private Structure StartupInfo
        Dim cb As Integer
        <MarshalAs(UnmanagedType.LPTStr)> Dim lpReserved As String
        <MarshalAs(UnmanagedType.LPTStr)> Dim lpDesktop As String
        <MarshalAs(UnmanagedType.LPTStr)> Dim lpTitle As String
        Dim dwX As Integer
        Dim dwY As Integer
        Dim dwXSize 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
        Dim lpReserved2 As IntPtr
        Dim hStdInput As IntPtr
        Dim hStdOutput As IntPtr
        Dim hStdError As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    Private Structure ProcessInformation
        Dim hProcess As IntPtr
        Dim hThread As IntPtr
        Dim dwProcessID As Integer
        Dim dwThreadID As Integer
    End Structure
     

    <DllImport("Advapi32.dll", ExactSpelling:=False, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function CreateProcessAsUser(ByVal hToken As IntPtr, ByVal lpApplicationName As String, <[In](), Out(), [Optional]()> ByRef lpCommandLine As StringBuilder, ByVal lpProcessAttributes As IntPtr, ByVal lpThreadAttributes As IntPtr, <MarshalAs(UnmanagedType.Bool)> ByVal bInheritHandles As Boolean, ByVal dwCreationFlags As Integer, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, <[In]()> ByRef lpStartupInfo As StartupInfo, <Out()> ByRef lpProcessInformation As ProcessInformation) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("Userenv.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function CreateEnvironmentBlock(ByRef lpEnvironment As IntPtr, ByVal hToken As IntPtr, <MarshalAs(UnmanagedType.Bool)> ByVal bInherit As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("Userenv.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function DestroyEnvironmentBlock(ByVal lpEnvironment As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("Wtsapi32.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function WTSQueryUserToken(ByVal SessionId As Integer, ByRef phToken As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("Kernel32.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function WTSGetActiveConsoleSessionId() As Integer
    End Function
    <DllImport("Kernel32.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Public Shared Sub Start(ByVal filename As String, ByVal commandLine As String, ByVal workingDirectory As String, Optional ByVal sessionId As Integer = -1)

        Dim hToken As IntPtr
        Dim pEnvBlock As IntPtr
        Dim dwSessionId As Integer
        Dim pi As ProcessInformation = Nothing
        Dim si As StartupInfo = Nothing

        '   If no session Id was specified defaults to using the session Id
        '   that is currently attached to the physical console.
        If sessionId = (-1) Then

            dwSessionId = WTSGetActiveConsoleSessionId()

        Else

            dwSessionId = sessionId

        End If


        '   Obtain the primary access token of the logged-on user specified by the session
        '   * Note: This can only be called successfully under LocalSystem context.
        If WTSQueryUserToken(dwSessionId, hToken) Then

            '   Get the enviorment block pointer for the user session.
            If CreateEnvironmentBlock(pEnvBlock, hToken, False) Then

                ' Setup command line buffer for unicode version
                Dim cmdLine As New StringBuilder(commandLine, 32768)

                If CreateProcessAsUser(hToken, filename, cmdLine, IntPtr.Zero, IntPtr.Zero, False, _
                                       CreateUnicodeEnvironment, pEnvBlock, workingDirectory, si, pi) Then

                    '   cleanup handle information
                    CloseHandle(pi.hProcess)
                    CloseHandle(pi.hThread)

                End If

                '   Destroy enviorment block pointer.
                DestroyEnvironmentBlock(pEnvBlock)

            End If

            '   cleanup user session token handle.
            CloseHandle(hToken)

        End If

    End Sub

End Class

Open in new window

gopalhazelAuthor Commented:
egl1044,

Thanks a lot..above example helped me a lot..It worked.
But, I have more query, I want start exe alongwith parameters, how do i start that using ServiceShell.Start
for e.g.
"C:\Hazel Infotech\AutoBackup Utility\AutoBackup-Icon.exe Start" where "start" is parameter.
nffvrxqgrcfqvvcCommented:
Try using second parameter.
ServiceShell.Start("C:\Hazel Infotech\AutoBackup Utility\AutoBackup-Icon.exe", " Start", Nothing)

Open in new window

gopalhazelAuthor Commented:
egl1044,

I tried the way you said, but it is not accepting parameter as "Start" instead it takes as blank parameter only..
any idea..what else we can do????
nffvrxqgrcfqvvcCommented:
Replace the CreateProcessAsUser function with this
<DllImport("Advapi32.dll", ExactSpelling:=False, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function CreateProcessAsUser(ByVal hToken As IntPtr, ByVal lpApplicationName As String, <[In](), Out(), [Optional]()> Byval lpCommandLine As StringBuilder, ByVal lpProcessAttributes As IntPtr, ByVal lpThreadAttributes As IntPtr, <MarshalAs(UnmanagedType.Bool)> ByVal bInheritHandles As Boolean, ByVal dwCreationFlags As Integer, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, <[In]()> ByRef lpStartupInfo As StartupInfo, <Out()> ByRef lpProcessInformation As ProcessInformation) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gopalhazelAuthor Commented:
egl1044,

thanks a lot.it worked...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.