G
GTi
I have small program that always must be running when a user is logged on.
Since users can close this program I must create a program that always check
if this program is running.
So I created a program in C# (my first!!) and it works with one major
problem.
This program detects when the desktop program is not running and starts it
again. All works as expected
BUT
The desktop program is running as a SYSTEM user.
That is not what I wanted....
So I need to find a way to start this desktop program as the current logged
on user (it can be different users)
Can this be done in C#?
GTi
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
[DllImport("user32.dll")] public static extern int FindWindow(string
lpClassName, string lpWindowName);
[DllImport("user32.dll")] public static extern int SendMessage(int hWnd,
uint Msg, int wParam, int lParam);
[DllImport("user32.dll")] public static extern int GetDesktopWindow();
}
namespace attenWat
{
public class Service1 : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer1=null;
private System.Timers.Timer newFileVersionChecker=null;
private System.ComponentModel.IContainer components=null;
public Service1()
{
InitializeComponent();
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
this.newFileVersionChecker = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.newFileVersionChecker)).BeginInit();
// timer1
this.timer1.Enabled = true;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elapsed_1);
// newFileVersionChecker
this.newFileVersionChecker.Enabled = true;
this.newFileVersionChecker.Interval = 60000;
this.newFileVersionChecker.Elapsed += new
System.Timers.ElapsedEventHandler(this.newFileVersionChecker_Elapsed);
// Service1
this.ServiceName = "AttendanceService";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.newFileVersionChecker)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
}
////////////////////////////////////////////////////////////////////////////////////////
private void timer1_Elapsed_1(object sender, System.Timers.ElapsedEventArgs
e)
{
if((Win32.GetDesktopWindow()!=0) &&
(Win32.FindWindow("TUN.Attendance.Windows.Class",null)==0))
{
Process myProcess = new Process();
try
{
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
// need to find a way to start this program in the current user environment
myProcess.StartInfo.Verb = "Open";
myProcess.StartInfo.UseShellExecute = true;
// myProcess.StartInfo.FileName = myDocumentsPath + \\tun\\attendance.exe;
// myProcess.Start();
}
catch(Win32Exception err)
{
string dummyString = err.Message; // Avoid warning message
// MessageBox.Show(err.Message+"\r\n"+myProcess.StartInfo.FileName, "Check
the path.", MessageBoxButtons.OK);
}
}
}
private void newFileVersionChecker_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// Application.StartupPath;
}
}
}
Since users can close this program I must create a program that always check
if this program is running.
So I created a program in C# (my first!!) and it works with one major
problem.
This program detects when the desktop program is not running and starts it
again. All works as expected
BUT
The desktop program is running as a SYSTEM user.
That is not what I wanted....
So I need to find a way to start this desktop program as the current logged
on user (it can be different users)
Can this be done in C#?
GTi
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
[DllImport("user32.dll")] public static extern int FindWindow(string
lpClassName, string lpWindowName);
[DllImport("user32.dll")] public static extern int SendMessage(int hWnd,
uint Msg, int wParam, int lParam);
[DllImport("user32.dll")] public static extern int GetDesktopWindow();
}
namespace attenWat
{
public class Service1 : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer1=null;
private System.Timers.Timer newFileVersionChecker=null;
private System.ComponentModel.IContainer components=null;
public Service1()
{
InitializeComponent();
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
this.newFileVersionChecker = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.newFileVersionChecker)).BeginInit();
// timer1
this.timer1.Enabled = true;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elapsed_1);
// newFileVersionChecker
this.newFileVersionChecker.Enabled = true;
this.newFileVersionChecker.Interval = 60000;
this.newFileVersionChecker.Elapsed += new
System.Timers.ElapsedEventHandler(this.newFileVersionChecker_Elapsed);
// Service1
this.ServiceName = "AttendanceService";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.newFileVersionChecker)).EndInit();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
}
////////////////////////////////////////////////////////////////////////////////////////
private void timer1_Elapsed_1(object sender, System.Timers.ElapsedEventArgs
e)
{
if((Win32.GetDesktopWindow()!=0) &&
(Win32.FindWindow("TUN.Attendance.Windows.Class",null)==0))
{
Process myProcess = new Process();
try
{
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
// need to find a way to start this program in the current user environment
myProcess.StartInfo.Verb = "Open";
myProcess.StartInfo.UseShellExecute = true;
// myProcess.StartInfo.FileName = myDocumentsPath + \\tun\\attendance.exe;
// myProcess.Start();
}
catch(Win32Exception err)
{
string dummyString = err.Message; // Avoid warning message
// MessageBox.Show(err.Message+"\r\n"+myProcess.StartInfo.FileName, "Check
the path.", MessageBoxButtons.OK);
}
}
}
private void newFileVersionChecker_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// Application.StartupPath;
}
}
}