开始一个过程 [英] Start a process

查看:70
本文介绍了开始一个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,当用户登录时总是必须运行。

因为用户可以关闭这个程序我必须创建一个程序,总是检查

如果这个程序正在运行。

所以我用C#创建了一个程序(我的第一个!!),它适用于一个主要的

问题。

该程序检测桌面程序何时未运行并再次启动它b / b $ b。所有工作都按预期工作

但是

桌面程序作为SYSTEM用户运行。

这不是我想要的...... br />
所以我需要找到一种方法来启动这个桌面程序,因为用户当前记录了

(它可以是不同的用户)


这可以在C#中完成吗?


GTi


使用系统;

使用System.Collections;

使用System.ComponentModel;

使用System.Data;

使用System.Diagnostics;

使用System.ServiceProcess;

使用System.Runtime.InteropServices;

使用System.Windows.Forms;

使用System.Xml;


公共类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& quot;)] 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 ();

}


//流程的主要入口点

static void Main()

{

System.ServiceProcess.ServiceBase [] ServicesToRun;

ServicesToRun = new System.ServiceProcess.ServiceBase [] {new Service1()};

System.ServiceProcess.ServiceBase.Run(ServicesToRu n);

}


///< summary>

/// Re设计器支持的请求方法 - 不要使用代码编辑器修改

///此方法的内容。

///< / summary>

private void InitializeComponent()

{

this.timer1 = new System.Timers.Timer();

this.newFileVersionChecker = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.t imer1))。BeginInit();

((系统。 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>

///清理正在使用的所有资源。

///< / summary>

protected override void Dispose(bool disposing)

{

if(disposing)

{

if(components!= null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}


///< summary>

///设置动态以便您的服务可以正常工作。

///< / summary>


protected override void OnStart(string [] args)

{

// TODO:在这里添加代码以开始服务。

}

///< summary>

/ //停止此服务。

///< / summary>

protected override void OnStop()

{

// TODO:在这里添加代码以执行任何必要的拆卸以停止

服务。

}


///////////////////////////////////////////////// ///////////////////////////////////////

private void timer1_Elapsed_1 (对象发送者,System.Timers.ElapsedEventArgs

e)

{

if((Win32.GetDesktopWindow()!= 0)&& ;

(Win32.FindWindow(" TUN.Attendance.Windows.Class",n ull)== 0))

{

处理myProcess = new Process();

try

{

string myDocumentsPath =

Environment.GetFolderPath(Environment .SpecialFolde r.ProgramFiles);

//需要找到在当前用户环境中启动此程序的方法

myProcess.StartInfo.Verb =" Open" ;;

myProcess.StartInfo.UseShellExecute = true;

// myProcess.StartInfo.FileName = myDocumentsPath + \\tun\\attendance.exe;

// myProcess.Start();

}

catch(Win32Exception错误)

{

string dummyString = err.Message; //避免警告消息

// MessageBox.Show(err.Message +" \\\\ n" + myProcess.Start Info.FileName," Check

路径。,MessageBoxButtons.OK);

}

}

}


private void newFileVersionChecker_Elapsed(object sender,

System.Timers.ElapsedEventArgs e)

{

// Application.StartupPath;

}

}

}

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;
}
}
}

推荐答案

您的桌面程序如何?刚开始?它是由

探测器程序启动的吗?或者你有一个捷径给所有用户/启动

文件夹?


-
在** @ dowhileloop.com
http:// dowhileloop.com 网站开发
http://publicjoe.dowhileloop.com - C#教程

" GTi" < gt*@gti.com>在消息新闻中写道:42 ******** @ news.wineasy.se ...
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...
我有一个小程序,当用户登录时总是必须运行。
由于用户可以关闭这个程序,我必须创建一个总是
的程序来检查这个程序是否正在运行。
所以我用C#创建了一个程序(我的第一个!!)并且它适用于一个主要的此程序检测桌面程序何时未运行并再次启动它。所有工作都按预期工作
但是桌面程序作为SYSTEM用户运行。
这不是我想要的......
所以我需要找到一种方法来启动这个桌面程序作为当前
登录用户(可以是不同的用户)

这可以用C#完成吗?

GTi
使用System.Collections;
使用System.ComponentModel;
使用System.Data;
使用System.Diagnostics;
使用System.ServiceProcess;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
使用System.Xml;

公共类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( "û ser32.dll")] public static extern int GetDesktopWindow();
}

namespace attenWat
公共类Service1:System.ServiceProcess.ServiceBase {

私有System.Timers.Timer timer1 = null;
私有System.Timers.Timer newFileVersionChecker = null;
私有System.ComponentModel.IContainer components = null;
public Service1()
{
InitializeComponent();
}
//进程的主要入口点
static void Main()
{System / ServiceProcess.ServiceBase [] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase [] {new
Service1()}; System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}
///< summary>
/// Designer支持所需的方法 - 不要修改
///使用代码编辑器的方法的内容。
///< / 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))。结束

Init(); }

///< summary>
///清理正在使用的所有资源。
///< / summary>
protected override void Dispose (bool处理)
{
if(disposing)
{
if(components!= null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
///< summary>
///设置动态因此,您的服务可以完成其工作。
///< / summary>

受保护的覆盖void OnStart(string [] args)
{
// TODO :在此处添加代码以启动您的服务。
}

///< summary>
///停止此服务。
///< /摘要>
受保护的覆盖void OnStop()
//
// TODO:在此处添加代码以执行停止服务所需的任何拆卸。
}


/////////////////////////////////////// /////////////////////////////////////

///// /////// private void timer1_Elapsed_1(对象发送者,
System.Timers.ElapsedEve ntArgs e)
{
if((Win32.GetDesktopWindow()!= 0)&&
(Win32.FindWindow(" TUN.Attendance.Windows.Class",nll )== 0))
{
处理myProcess = new Process();
尝试
字符串myDocumentsPath =
Environment.GetFolderPath(环境。 SpecialFolde r.ProgramFiles);
//需要找到一种方法在当前用户
环境中启动该程序myProcess.StartInfo.Verb =" Open";
myProcess.StartInfo.UseShellExecute = true;
// myProcess.StartInfo.FileName = myDocumentsPath + \\tun\\attendance.exe;
// myProcess.Start();
}
catch(Win32Exception错误)
{string / dummy dummyString = err.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;
}
}

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一个好点...如果你的探测器应用程序作为

启动序列的一部分启动,而不是作为服务,然后它将在当前用户的

凭证下运行,这意味着它将启动任何

下游应用程序作为当前用户。


-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn。 com / nickmalik


免责声明:本论坛中表达的意见是我自己的意见,而不是我雇主的b $ b代表。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-

" GTi" < gt*@gti.com>在消息新闻中写道:42 ******** @ news.wineasy.se ...
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...
我有一个小程序,当用户登录时总是必须运行。
由于用户可以关闭这个程序,我必须创建一个程序,总是检查这个程序是否正在运行。
所以我用C#创建了一个程序(我的第一个!!),它与一个主要的程序一起工作问题。
此程序检测桌面程序何时未运行并再次启动它。所有工作都按预期工作
但是桌面程序作为SYSTEM用户运行。
这不是我想要的......
所以我需要找到一种方法来启动这个桌面程序作为当前登录的用户(可以是不同的用户)

这可以用C#完成吗?

GTi
使用System.Collections;
使用System.ComponentModel;
使用System.Data;
使用System.Diagnostics;
使用System.ServiceProcess;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
使用System.Xml;

公共类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
公共类Service1:System.ServiceProcess.ServiceBase {

私有System.Timers.Timer timer1 = null;
私有System.Timers.Timer newFileVersionChecker = null;
私有System.ComponentModel.IContainer components = null;
public Service1()
{
InitializeComponent();
}
//进程的主要入口点
static void Main()
{System / ServiceProcess.ServiceBase [] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase [] {new
Service1()};
System.ServiceProcess .ServiceBase.Run(ServicesToRu n);
}
///< summary>
/// Designer支持所需的方法 - 不要修改
/ //使用代码编辑器的方法的内容。
///< / 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>
///清理任何正在使用的资源。
///< / summary>
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components!= null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

///< summary>
///设置动态以便您的服务可以正常工作。
///< / summary>

protected override void OnStart(string [] args)
//
// TODO:在这里添加代码以启动您的服务。
}

///< ;摘要>
///停止此服务。
///< / summary>
受保护的覆盖void OnStop()
//
// TODO:添加代码来执行任何必要的拆卸以停止您的服务。
}

/////////////////// ////////////////////////////////////////////////// ///////////////////
private void timer1_Elapsed_1(object sender,
System.Timers.ElapsedEventArgs e)

if((Win32.GetDesktopWindow()!= 0)&&
(Win32.FindWindow(" TUN.Attendance.Windows.Class",nll)== 0))
{
处理myProcess = new Process();
尝试
字符串myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolde r.ProgramFiles);
//需要找到一种方法在当前用户中启动此程序
环境
myProcess.StartInfo.Verb ="打开";
myProcess.StartInfo.UseShellExecute = true;
// myProcess.StartInfo.FileName = myDocumentsPath + \\tun \ \attendance.exe;
// myProcess.Start();
}
catch(Win32Exception错误)
{string / dummy dummyString = err.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;
}
}
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;
}
}
}



探测器是一个Windows服务。

桌面程序由注册表RUN键启动。


因此检测器在SERVICE用户下运行

和桌面程序作为登录用户运行。


任何想法探测器如何启动程序运行

作为登录用户不知道用户/密码。

" Nick Malik [Microsoft]" < NI ******* @ hotmail.nospam.com> skrev i melding

新闻:Za ******************** @ comcast.com ...
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有一个好点...如果你的探测器...应用程序作为启动序列的一部分启动,而不是作为服务,然后它将在当前用户的凭证下运行,这意味着它将启动任何下游应用程序作为当前用户。

-
--- Nick Malik [微软]
MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik

免责声明:本论坛发表的意见均为我的意见拥有,而不是代表我的雇主。
我不代表我的雇主回答问题。我只是一名帮助程序员的程序员。
-
GTi < gt*@gti.com>在消息新闻中写道:42 ******** @ news.wineasy.se ...
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...
我有一个小程序,当用户登录时总是必须运行。
由于用户可以关闭这个程序,我必须创建一个程序,总是检查这个程序是否正在运行。
所以我用C#创建了一个程序(我的第一个!!),它与一个主要的程序一起工作问题。
此程序检测桌面程序何时未运行并再次启动它。所有工作都按预期工作
但是桌面程序作为SYSTEM用户运行。
这不是我想要的......
所以我需要找到一种方法来启动这个桌面程序作为当前登录的用户(可以是不同的用户)

这可以用C#完成吗?

GTi
使用System.Collections;
使用System.ComponentModel;
使用System.Data;
使用System.Diagnostics;
使用System.ServiceProcess;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
使用System.Xml;

公共类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
公共类Service1:System.ServiceProcess.ServiceBase {

私有System.Timers.Timer timer1 = null;
私有System.Timers.Timer newFileVersionChecker = null;
私有System.ComponentModel.IContainer components = null;
public Service1()
{
InitializeComponent();
}
//进程的主要入口点
static void Main()
{System / ServiceProcess.ServiceBase [] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase [] {new
Service1()};
System.ServiceProcess .ServiceBase.Run(ServicesToRu n);
}
///< summary>
/// Designer支持所需的方法 - 不要修改
/ //使用代码编辑器的方法的内容。
///< / 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>
///清理任何正在使用的资源。
///< / summary>
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components!= null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

///< summary>
///设置动态以便您的服务可以正常工作。
///< / summary>

protected override void OnStart(string [] args)
//
// TODO:在这里添加代码以启动您的服务。
}

///< ;摘要>
///停止此服务。
///< / summary>
受保护的覆盖void OnStop()
//
// TODO:添加代码来执行任何必要的拆卸以停止您的服务。
}

/////////////////// ////////////////////////////////////////////////// ///////////////////
private void timer1_Elapsed_1(object sender,
System.Timers.ElapsedEventArgs e)

if((Win32.GetDesktopWindow()!= 0)&&
(Win32.FindWindow(" TUN.Attendance.Windows.Class",nll)== 0))
{
处理myProcess = new Process();
尝试
字符串myDocumentsPath =
Environment.GetFolderPath(Environment.SpecialFolde r.ProgramFiles);
//需要找到一种方法在当前用户中启动此程序
environment
myProcess.StartInfo.Verb ="打开";
myProcess.StartInfo.UseShellExecute = true;
// myProcess.StartInfo.FileName = myDocumentsPath +
\\\ \ tt \\\\\\\\\\\\\\\\\\\\\\\\\\\\\信息; //避免警告消息
// MessageBox.Show(err.Message +" \\\\ n" + myProcess.Start Info.FileName,
" Check the path。",MessageBoxButtons .OK);
}
}

private void newFileVersionChecker_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// Application.StartupPath;
}
}

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;
}
}
}




这篇关于开始一个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆