c# - 检测另一个进程是否启动为 "Run as Administrator"

标签 c# vb.net

我们的应用程序需要通过 COM 接口(interface)与另一个程序通信。如果其他程序以“以管理员身份运行”启动,则该界面将不起作用。想要检测此其他进程是否处于此状态并警告用户。有任何想法吗?

寻找 .NET 语言(C# 或 VB.NET)。

TIA

最佳答案

你可以尝试这样的事情:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Security.Principal;
using System.Reflection;

namespace WindowsFormsApplication2
{

    public class ProcessHelper
    {
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr hObject);

        private const int STANDARD_RIGHTS_REQUIRED      = 0xF0000;
        private const int TOKEN_ASSIGN_PRIMARY           =0x1;
        private const int TOKEN_DUPLICATE                 = 0x2;
        private const int TOKEN_IMPERSONATE              = 0x4;
        private const int TOKEN_QUERY                     = 0x8;
        private const int TOKEN_QUERY_SOURCE             = 0x10;
        private const int TOKEN_ADJUST_GROUPS           = 0x40;
        private const int TOKEN_ADJUST_PRIVILEGES        = 0x20;
        private const int TOKEN_ADJUST_SESSIONID          = 0x100;
        private const int TOKEN_ADJUST_DEFAULT          = 0x80;
        private const int TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_QUERY_SOURCE | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_SESSIONID | TOKEN_ADJUST_DEFAULT);

        public static bool IsProcessOwnerAdmin(string processName)
        {
            Process proc = Process.GetProcessesByName(processName)[0];

            IntPtr ph = IntPtr.Zero;

            OpenProcessToken(proc.Handle, TOKEN_ALL_ACCESS, out ph);

            WindowsIdentity iden = new WindowsIdentity(ph);

            bool result = false;

            foreach (IdentityReference role in iden.Groups)
            {
                if (role.IsValidTargetType(typeof(SecurityIdentifier)))
                {
                    SecurityIdentifier sid = role as SecurityIdentifier;

                    if (sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) || sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid))
                    {
                        result = true;
                        break;
                    }
                }
            }

            CloseHandle(ph);

            return result;
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            bool isAdmin = ProcessHelper.IsProcessOwnerAdmin("outlook");
        }
    }
}

这也可能是一件好事:Well-known security identifiers in Windows operating systems

这应该是一个很好的起点:-)

关于c# - 检测另一个进程是否启动为 "Run as Administrator",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5479551/

相关文章:

c# - EF Core 日期时间偏移

c# - 如何为我的 ASP.NET Web 应用程序获取 Google Api OAuth 2 刷新 token ?

c# - 将 vb.net 转换为 c#-优化参数传递

vb.net - 如何在 vb.net 的 MVC 4 中创建并返回带有 Controller 的 Excel 文件?

.net - VB.NET:可以将功能分成几个部分吗?

c# - 我如何在我的项目中解决初始化字符串的格式不符合从索引 17' 开始的规范

c# - 隐藏或禁用 DataGridView 最后一行中的复选框

c# - SOAPUI 未显示来自 WSDL 的操作

vb.net - 在范围的基础上获取记录(将是 char,将是 int)

vb.net - "Expression of type ""is not queryable"错误