【发布时间】:2015-02-11 11:40:07
【问题描述】:
在我的一个 C# 应用程序 (.NET 2) 中,我使用了一个非托管模式库 (C),它调用了一些 OpenProcess()s。我最近添加了一些使用 Process.GetProcessById() 的 C# 代码。在此更改之后,OpenProcess() 开始成功,即使应用于使它们失败的 PID。经过一番调查,我发现Process.GetProcessById() 隐式将SeDebugPrivilege 设置为应用程序(注意:当前用户具有管理员权限)。由于在我的特定应用程序中这种行为是不可取的,我最终通过调用 Process.LeavDebugMode() 恢复了正常权限。
由于我找不到任何描述,我对我的程序的正确性有些担心。调用Process.LeavDebugMode() 来“调整”Process.GetProcessById() 的工作是否正确?简而言之,我的 Process.GetProcessById() 是否按预期工作(例如,由 MSDN 记录)或者我观察到的行为是否隐藏了我的应用程序中的一些细微错误?
我的操作系统是用于嵌入式系统的 Windows 7 SP1 64 位。
编辑:更多信息:进程以 32 位模式运行(即它运行 32 位版本的 .NET 引擎)。另外,我添加了这个“.config”文件以确保使用的 .NET 版本是 2:
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
EDIT2:更多实验:编写了一个普通的 C#/WindowsForm 应用程序(感谢 MS 向导 ;-)),添加了两个按钮,一个用于调用 Process.GetProcessById(),一个用于调用Process.LeaveDebugMode()。以下是相关代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
namespace testproc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process proc = Process.GetProcessById(Process.GetCurrentProcess().Id);
}
private void button2_Click(object sender, EventArgs e)
{
Process.LeaveDebugMode();
}
}
}
启动应用程序(在 VS 之外),然后我启动了进程资源管理器。它证实了奇怪的“功能”:
- 应用程序刚刚启动:SeDebugPrivilege 已禁用
- 按下按钮 1:SeDebugPrivilege 已启用!
- 按下按钮 2:SeDebugPrivilege 再次禁用
- 按下按钮 1:SeDebugPrivilege 仍处于禁用状态(嗯...)
所以,我只能确认我的断言(至少对于Process.GetProcessById() 的第一次调用)。有什么想法吗?
【问题讨论】: