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(ServicesToRu n);
}
/// <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.t imer1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).BeginInit();
// timer1
this.timer1.Enabled = true;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed_1);
// newFileVersionChecker
this.newFileVersionChecker.Enabled = true;
this.newFileVersionChecker.Interval = 60000;
this.newFileVersionChecker.Elapsed += new
System.Timers.ElapsedEventHandler(this.newFileVers ionChecker_Elapsed);
// Service1
this.ServiceName = "AttendanceService";
((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).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",n ull)==0))
{
Process myProcess = new Process();
try
{
string myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolde r.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.Start Info.FileName, "Check
the path.", MessageBoxButtons.OK);
}
}
}
private void newFileVersionChecker_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// Application.StartupPath;
}
}
} 12 4077
How is your desktop program being started at first? Is it being started by
the detector program? Or do you have a short cut to the all users/startup
folder?
-- in**@dowhileloop.com http://dowhileloop.com website development http://publicjoe.dowhileloop.com -- C# Tutorials
"GTi" <gt*@gti.com> wrote in message news:42********@news.wineasy.se... 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(ServicesToRu n); }
/// <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.t imer1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).Beg
inInit(); // timer1 this.timer1.Enabled = true; this.timer1.Interval = 10000; this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elap sed_1);
// newFileVersionChecker this.newFileVersionChecker.Enabled = true; this.newFileVersionChecker.Interval = 60000; this.newFileVersionChecker.Elapsed += new System.Timers.ElapsedEventHandler(this.newFileVers ionChecker_Elapsed); // Service1 this.ServiceName = "AttendanceService"; ((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).End
Init(); }
/// <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",n ull)==0)) { Process myProcess = new Process(); try { string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolde r.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.Start Info.FileName, "Check the path.", MessageBoxButtons.OK); } } }
private void newFileVersionChecker_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // Application.StartupPath; } } }
Landi has a good point... if your "detector" app is started as part of a
startup sequence, and not as a service, then it will run under the
credentials of the current user, which means that it will start any
downstream app as the current user.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"GTi" <gt*@gti.com> wrote in message news:42********@news.wineasy.se... 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(ServicesToRu n); }
/// <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.t imer1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).BeginInit(); // timer1 this.timer1.Enabled = true; this.timer1.Interval = 10000; this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elap sed_1);
// newFileVersionChecker this.newFileVersionChecker.Enabled = true; this.newFileVersionChecker.Interval = 60000; this.newFileVersionChecker.Elapsed += new System.Timers.ElapsedEventHandler(this.newFileVers ionChecker_Elapsed); // Service1 this.ServiceName = "AttendanceService"; ((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).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",n ull)==0)) { Process myProcess = new Process(); try { string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolde r.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.Start Info.FileName, "Check the path.", MessageBoxButtons.OK); } } }
private void newFileVersionChecker_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // Application.StartupPath; } } }
The detector is a windows service.
The desktop program is started by the registry RUN key.
So the detector is running under the SERVICE user
and the desktop program is running as the logged on user.
Any idea how the detector can start a progrm running
as the logged on user without knowing the user/password.
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> skrev i melding
news:Za********************@comcast.com... Landi has a good point... if your "detector" app is started as part of a startup sequence, and not as a service, then it will run under the credentials of the current user, which means that it will start any downstream app as the current user.
-- --- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I'm just a programmer helping programmers. -- "GTi" <gt*@gti.com> wrote in message news:42********@news.wineasy.se...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(ServicesToRu n); }
/// <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.t imer1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).BeginInit(); // timer1 this.timer1.Enabled = true; this.timer1.Interval = 10000; this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elap sed_1);
// newFileVersionChecker this.newFileVersionChecker.Enabled = true; this.newFileVersionChecker.Interval = 60000; this.newFileVersionChecker.Elapsed += new System.Timers.ElapsedEventHandler(this.newFileVers ionChecker_Elapsed); // Service1 this.ServiceName = "AttendanceService"; ((System.ComponentModel.ISupportInitialize)(this.t imer1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.n ewFileVersionChecker)).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",n ull)==0)) { Process myProcess = new Process(); try { string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolde r.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.Start Info.FileName, "Check the path.", MessageBoxButtons.OK); } } }
private void newFileVersionChecker_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // Application.StartupPath; } } }
"GTi" <bi**@gates.com> wrote in message
news:uw*************@TK2MSFTNGP12.phx.gbl... The detector is a windows service. The desktop program is started by the registry RUN key.
So the detector is running under the SERVICE user and the desktop program is running as the logged on user.
Any idea how the detector can start a progrm running as the logged on user without knowing the user/password.
Yes, but you have to PInvoke "CreateProcessWithToken" Win32 API supplying
the token handle of the interactive logon.
Willy.
"Willy Denoyette [MVP]" <wi*************@telenet.be> skrev i melding
news:%2****************@TK2MSFTNGP10.phx.gbl... "GTi" <bi**@gates.com> wrote in message news:uw*************@TK2MSFTNGP12.phx.gbl... The detector is a windows service. The desktop program is started by the registry RUN key.
So the detector is running under the SERVICE user and the desktop program is running as the logged on user.
Any idea how the detector can start a progrm running as the logged on user without knowing the user/password.
Yes, but you have to PInvoke "CreateProcessWithToken" Win32 API supplying the token handle of the interactive logon.
Willy.
Willy,
Ok.... how?
Sorry, but I'm not that familiar with C# yet
And I have never used CreateProcessWithToken before.
How can I get the hToken for the current Desktop User?
And why do I have to PInvoke(*) a Win32 function into C#
Why doesn't C# have this function available in the Namespace(*).
Looks like C# is not finish yet so it have to borrow win32 functions.
Any hints?
(*)
I'm not so familiar with all the C# expression yet, so if I use wrong
phrases please correct me.
GTi
Hello GTi, The detector is a windows service. The desktop program is started by the registry RUN key.
This is what Landi was suggesting you should change.
Don't run the detector as a windows service. Run it as a console app in the
backgroud using the same registry RUN key.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"GTi" <bi**@gates.com> wrote in message
news:uw*************@TK2MSFTNGP12.phx.gbl...
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> skrev i melding
news:2b********************@comcast.com... Hello GTi,
The detector is a windows service. The desktop program is started by the registry RUN key.
This is what Landi was suggesting you should change.
Don't run the detector as a windows service. Run it as a console app in the backgroud using the same registry RUN key.
-- --- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I'm just a programmer helping programmers. -- "GTi" <bi**@gates.com> wrote in message news:uw*************@TK2MSFTNGP12.phx.gbl...
OK - then the detector is a non invisible window.
The desktop program will check if the detector is running,
If not started, then start the detector program.
Same to the detector program. If the desktop program is not
running, then start the desktop program.
In that way both will watch each other.
No I must find out how I create a window program in C#
It's easy with C++...
Any help?
"GTi" <bi**@gates.com> skrev i melding
news:Om*************@TK2MSFTNGP10.phx.gbl... "Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> skrev i melding news:2b********************@comcast.com... Hello GTi,
The detector is a windows service. The desktop program is started by the registry RUN key.
This is what Landi was suggesting you should change.
Don't run the detector as a windows service. Run it as a console app in the backgroud using the same registry RUN key.
-- --- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I'm just a programmer helping programmers. -- "GTi" <bi**@gates.com> wrote in message news:uw*************@TK2MSFTNGP12.phx.gbl...
OK - then the detector is a non invisible window. The desktop program will check if the detector is running, If not started, then start the detector program. Same to the detector program. If the desktop program is not running, then start the desktop program. In that way both will watch each other.
No I must find out how I create a window program in C# It's easy with C++...
Any help?
Ok I got it....
But how can I change the class name.
the class name for the application is
WindowsForms10.Window.8.app4
I want to change it to a more familiar name.
A litle correction:
OK - then the detector is a non visible window.
"GTi" <bi**@gates.com> skrev i melding
news:Om*************@TK2MSFTNGP10.phx.gbl... "Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> skrev i melding news:2b********************@comcast.com... Hello GTi,
The detector is a windows service. The desktop program is started by the registry RUN key.
This is what Landi was suggesting you should change.
Don't run the detector as a windows service. Run it as a console app in the backgroud using the same registry RUN key.
-- --- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I'm just a programmer helping programmers. -- "GTi" <bi**@gates.com> wrote in message news:uw*************@TK2MSFTNGP12.phx.gbl...
OK - then the detector is a non invisible window. The desktop program will check if the detector is running, If not started, then start the detector program. Same to the detector program. If the desktop program is not running, then start the desktop program. In that way both will watch each other.
No I must find out how I create a window program in C# It's easy with C++...
Any help?
"GTi" <bi**@gates.com> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl... "Willy Denoyette [MVP]" <wi*************@telenet.be> skrev i melding news:%2****************@TK2MSFTNGP10.phx.gbl... "GTi" <bi**@gates.com> wrote in message news:uw*************@TK2MSFTNGP12.phx.gbl... The detector is a windows service. The desktop program is started by the registry RUN key.
So the detector is running under the SERVICE user and the desktop program is running as the logged on user.
Any idea how the detector can start a progrm running as the logged on user without knowing the user/password.
Yes, but you have to PInvoke "CreateProcessWithToken" Win32 API supplying the token handle of the interactive logon.
Willy.
Willy, Ok.... how? Sorry, but I'm not that familiar with C# yet And I have never used CreateProcessWithToken before. How can I get the hToken for the current Desktop User?
And why do I have to PInvoke(*) a Win32 function into C# Why doesn't C# have this function available in the Namespace(*). Looks like C# is not finish yet so it have to borrow win32 functions.
Any hints?
(*) I'm not so familiar with all the C# expression yet, so if I use wrong phrases please correct me.
GTi
First, C# is a programming language, ad as such it doesn't have "functions",
this is why .NET has the framework class library, just like C/C++ has an
extensive library set.
In another reply you pretend this is easy in C++, but that's not true as
it's not a language issue, your problem stems from the fact that you spawn
a process from a service that runs as SYSTEM, the result is that the newly
created process run's with the same identity.
Second, not all Win32 API's are wrapped by the framework, and will probably
never will (thank God), especially Win32 API's which are very rarely used,
are new and only available on a restricted number of OS'ses are not
included. However, to make it possible to call such API's .NET provides a
facility called PInvoke.
Now, CreateProcessWithToken is such an exotic Win32 API (only available on
W2K3), another API is CreateProcessAsUser and and CreateProcessWithLogon (XP
and W2K and W2K3) but these require login credentials, so if you are not
running W2K3 you are stuck anyway.
So, I would suggest you run the detector in the session of the logon user
(started from a login script) as a console program with an hidden command
windows, that way the processes you spawn inherit the context of the logon
user.
Willy.
"Willy Denoyette [MVP]" <wi*************@telenet.be> skrev i melding
news:ui**************@TK2MSFTNGP10.phx.gbl... "GTi" <bi**@gates.com> wrote in message news:eU**************@TK2MSFTNGP09.phx.gbl... "Willy Denoyette [MVP]" <wi*************@telenet.be> skrev i melding news:%2****************@TK2MSFTNGP10.phx.gbl... "GTi" <bi**@gates.com> wrote in message news:uw*************@TK2MSFTNGP12.phx.gbl... The detector is a windows service. The desktop program is started by the registry RUN key.
So the detector is running under the SERVICE user and the desktop program is running as the logged on user.
Any idea how the detector can start a progrm running as the logged on user without knowing the user/password.
Yes, but you have to PInvoke "CreateProcessWithToken" Win32 API supplying the token handle of the interactive logon.
Willy.
Willy, Ok.... how? Sorry, but I'm not that familiar with C# yet And I have never used CreateProcessWithToken before. How can I get the hToken for the current Desktop User?
And why do I have to PInvoke(*) a Win32 function into C# Why doesn't C# have this function available in the Namespace(*). Looks like C# is not finish yet so it have to borrow win32 functions.
Any hints?
(*) I'm not so familiar with all the C# expression yet, so if I use wrong phrases please correct me.
GTi
First, C# is a programming language, ad as such it doesn't have "functions", this is why .NET has the framework class library, just like C/C++ has an extensive library set. In another reply you pretend this is easy in C++, but that's not true as it's not a language issue, your problem stems from the fact that you spawn a process from a service that runs as SYSTEM, the result is that the newly created process run's with the same identity. Second, not all Win32 API's are wrapped by the framework, and will probably never will (thank God), especially Win32 API's which are very rarely used, are new and only available on a restricted number of OS'ses are not included. However, to make it possible to call such API's .NET provides a facility called PInvoke. Now, CreateProcessWithToken is such an exotic Win32 API (only available on W2K3), another API is CreateProcessAsUser and and CreateProcessWithLogon (XP and W2K and W2K3) but these require login credentials, so if you are not running W2K3 you are stuck anyway. So, I would suggest you run the detector in the session of the logon user (started from a login script) as a console program with an hidden command windows, that way the processes you spawn inherit the context of the logon user.
Willy.
Willy,
Thanks for the info
In another reply you pretend this is easy in C++,...
C/C++ is not a easier language than C#
Only that I have used it in 15 years and "have it in my fingers".
But in time I'm sure I will love C# more than C/C++.
GTi
Hello GTi, OK - then the detector is a non visible window. The desktop program will check if the detector is running, If not started, then start the detector program. Same to the detector program. If the desktop program is not running, then start the desktop program. In that way both will watch each other.
No I must find out how I create a window program in C# It's easy with C++...
Any help?
Ok I got it.... But how can I change the class name. the class name for the application is WindowsForms10.Window.8.app4 I want to change it to a more familiar name.
One of my assumptions was that the desktop app was not written by you.
However, from the statement above, it appears clear that the desktop app was
written by you. That changes a few things.
If it's your app, and the user isn't allowed to close it, why not just
reject the event to close the window except in cases where the OS is
shutting down?
The event you would want is the .Closing event
See the example in this page of the documentation: http://msdn.microsoft.com/library/en...classtopic.asp
As for how to create a windows app in C#: try google with "tutorial .net
windows app c#"
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
-- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mark Adams |
last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail
server running for several months. It took me a week or so to get it up
and running in September. Now, I did a clean upgrade to...
|
by: Marc |
last post by:
I want to write a C#/ASP.NET application where a user can go to a web page, start running a job, close their browser, and then come back later and see the results. The purpose for this application is...
|
by: Sin Jeong-hun |
last post by:
Many applications need to lanch a web browser to show its homepage to
users. I used
System.Diagnosis.Process.Start("http://www.google.com");
Or I launched other application like...
|
by: Dmitri Shvetsov |
last post by:
Hi,
Can I start an external process from the Web Service?
I'm using a code, compiler keeps silence, compiles ok and starts the
project. When I trace in Debugger it doesn't start an external...
|
by: Yiu |
last post by:
upgent help
i want to start IE explorer using C#
i try many code such as below:
ProcessStartInfo startInfo = new ProcessStartInfo("IEXPLORE.EXE");
Process.Start(startInfo);
or
Process...
| |
by: Tony |
last post by:
I am running an application called AcroComm.exe to poll time clocks here at
our company. I have written a small C# app that will poll the clocks based
on information found in a DB. My problem is...
|
by: Dean Slindee |
last post by:
I have looked everywhere I can think of for the .exe name of the Windows
Picture and Fax Viewer. Anybody know what it's named?
Thanks,
Dean Slindee
|
by: richwood |
last post by:
Ok....Super newbie here in C#...
I am trying to write an application (myapp.exe) that will start an
application called abc.exe and if it is terminated, will restart it.
(or rerun myapp.exe) I...
|
by: henning.friese |
last post by:
Hello NG,
I'm need to write some code which creates tiff files from various
document types (doc, pdf, xls). I want to do this by ShellExecuting
(via System.Diagnostics.Process) the doc-files...
|
by: =?Utf-8?B?Vmlua2k=?= |
last post by:
Hello Everyone,
I have an exe application that I want to start remotely.Once I start the
application,
I want to press ok to that application. To start the application, I did...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |