troubleshooting Question

Native code for app/service that checks and restarts a process if it's stopped?

Avatar of KTN-IT
KTN-ITFlag for United States of America asked on
CC++Visual Basic Classic
15 Comments1 Solution374 ViewsLast Modified:
At my organization, I am turning old XP machines into RDP clients by changing the Windows shell (at HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell) from explorer.exe to mstsc.exe.

However, the AutoRestartShell value in that same registry key only works for a process called "explorer.exe."  I would like to create my own application or service that launches a process (like mstsc.exe or notepad.exe) and then monitors it and restarts that process when it terminates.  (Yes, I know I could just rename 'mstsc' as 'explorer' but I'm looking for a more elegant solution.)

Below I found some VB6 code that does this from this site.  However, I would like to code this in native Windows code and compile it into a small .exe or .dll without .NET.

I have Visual Studio 2010 and experience in C#, but almost no experience in C++ and in coding native Windows apps.  Can anyone get me pointed in the right direction?

Private param As String
Option Explicit

Const STILL_ACTIVE = &H103

Private Declare Function GetExitCodeProcess Lib "Kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long

Private Declare Function OpenProcess Lib "Kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Sub Sleep Lib "Kernel32" _
(ByVal dwMilliseconds As Long)
'a shell subroutine that will re-launch a program if it is down
Public Sub Relauncher()
Dim llProcess As Long
Dim llReturn As Long

llProcess = OpenProcess(PROCESS_QUERY_INFORMATION, _
False, Shell(param, 1))

GetExitCodeProcess llProcess, llReturn
Sleep 50
Loop While llReturn = STILL_ACTIVE
End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 15 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros