Win32桌面编程:ACLUI.DLL,EditSecurity(IntPtr hwndOwner, ISecurityInformation psi)

news2025/3/26 8:14:42

在Windows编程中,我们通常需要借助通用对话框的力量,今天我们就聊一下“安全属性表”通用对话框的使用心得。

当我们调用EditSecurity函数时:

1.EditSecurity将调用ISecurityInformation中的GetObjectInformation函数

在编写 ISecurityInformation.GetObjectInformation(out SI_OBJECT_INFO pObjectInfo)时

我们需要将结构返还给函数,GetObjectInformation用于初始化属性表的样式

看一下结构:

    public struct SI_OBJECT_INFO
    {
        public uint dwFlags;
        public IntPtr hInstance;
        public string pszServerName;
        public string pszObjectName;
        public string pszPageTitle;
        public Guid guidObjectType;
    }

解释一下:dwFlags为属性表样式的标志SI_OBJECT_INFO 结构

pszObjectName为属性表的标题

pszPageTitle为属性页的标题

2.接着EditSecurity将调用ISecurityInformation中的GetSecurity函数

GetSecurity(uint RequestedInformation, out IntPtr ppSecurityDescriptor, bool fDefault)

其他我们不需要过于处理,专注于“ppSecurityDescriptor”,一个句柄,

例如:

public string _filePath;

            public void GetSecurity(uint RequestedInformation, out IntPtr ppSecurityDescriptor, bool fDefault)
            {
            ppSecurityDescriptor = IntPtr.Zero;

            // 获取文件的安全描述符
            var fileSecurity = File.GetAccessControl(_filePath);
            var rawSecurityDescriptor = fileSecurity.GetSecurityDescriptorBinaryForm();

            // 将安全描述符复制到非托管内存
            ppSecurityDescriptor = Marshal.AllocHGlobal(rawSecurityDescriptor.Length);
            Marshal.Copy(rawSecurityDescriptor, 0, ppSecurityDescriptor, rawSecurityDescriptor.Length);
            }

3.当我们将这两个函数处理好以后基本就可以弹出这个对话框了

如下(Powershell+C#):

Add-Type -TypeDefinition @"
using System;
using System.IO;
using System.Runtime.InteropServices;
// 定义必要的结构和接口
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct SI_OBJECT_INFO
    {
        public uint dwFlags;
        public IntPtr hInstance;
        public string pszServerName;
        public string pszObjectName;
        public string pszPageTitle;
        public Guid guidObjectType;
    }

    [ComImport, Guid("965FC360-16FF-11d0-91CB-00AA00BBB723"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface ISecurityInformation
    {
        void GetObjectInformation(out SI_OBJECT_INFO pObjectInfo);
        void GetSecurity(uint RequestedInformation, out IntPtr ppSecurityDescriptor, [MarshalAs(UnmanagedType.Bool)] bool fDefault);
        void SetSecurity(uint SecurityInformation, IntPtr pSecurityDescriptor);
        void GetAccessRights(ref Guid pguidObjectType, uint dwFlags, out IntPtr ppAccess, out uint pcAccesses, out uint piDefaultAccess);
        void MapGeneric(ref Guid pguidObjectType, ref byte pAceFlags, ref uint pMask);
        void GetInheritTypes(out IntPtr ppInheritTypes, out uint pcInheritTypes);
        void PropertySheetPageCallback(IntPtr hwnd, uint uMsg, uint uPage);
    }

    public class FileSecurityInfo : ISecurityInformation
    {

        public FileSecurityInfo()
        {
        }
        public const uint SI_PAGE_TITLE = 0x00000800;
        public const uint SI_EDIT_ALL = 0x00000003;

        // 实现接口方法
        public void GetObjectInformation(out SI_OBJECT_INFO pObjectInfo)
        {
            pObjectInfo = new SI_OBJECT_INFO()
            {
                dwFlags = Flags,  // SI_EDIT_PERMS
                pszObjectName = ObjectName,
                pszServerName = null,
                pszPageTitle = PageTitle,
                guidObjectType = Guid.Empty
            };
        }
        public string PageTitle;
        public string ObjectName;
        public string _filePath;
        public uint Flags = SI_PAGE_TITLE | SI_EDIT_ALL;



            public void GetSecurity(uint RequestedInformation, out IntPtr ppSecurityDescriptor, bool fDefault)
            {
            ppSecurityDescriptor = IntPtr.Zero;

            // 获取文件的安全描述符
            
            var fileSecurity = File.GetAccessControl(_filePath);
            var rawSecurityDescriptor = fileSecurity.GetSecurityDescriptorBinaryForm();

            // 将安全描述符复制到非托管内存
            ppSecurityDescriptor = Marshal.AllocHGlobal(rawSecurityDescriptor.Length);
            Marshal.Copy(rawSecurityDescriptor, 0, ppSecurityDescriptor, rawSecurityDescriptor.Length);
        }

        public void SetSecurity(uint SecurityInformation, IntPtr pSecurityDescriptor)
        {
            //if (!SetFileSecurity(_filePath, SecurityInformation, pSecurityDescriptor))
            //{
            //    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            //}
        }

        // 其他接口方法留空(需根据需求实现)
        public void GetAccessRights(ref Guid pguidObjectType, uint dwFlags, out IntPtr ppAccess, out uint pcAccesses, out uint piDefaultAccess)
        {
            ppAccess = IntPtr.Zero;
            pcAccesses = 0;
            piDefaultAccess = 0;
        }
        public void MapGeneric(ref Guid pguidObjectType, ref byte pAceFlags, ref uint pMask) { }
        public void GetInheritTypes(out IntPtr ppInheritTypes, out uint pcInheritTypes)
        {
            ppInheritTypes = IntPtr.Zero;
            pcInheritTypes = 0;
        }
        public void PropertySheetPageCallback(IntPtr hwnd, uint uMsg, uint uPage) { }

        [DllImport("aclui.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EditSecurity(IntPtr hwndOwner, ISecurityInformation psi);
    }

"@ 

# 使用示例

# 创建安全信息对象
$securityInfo = New-Object FileSecurityInfo
$securityInfo.PageTitle = "于子轩的安全属性对话框"
$securityInfo.Flags = 0x00000010L  #高级选项
$securityInfo.Flags += 0x00000080L  #高级选项
$securityInfo.Flags += 0x00000001l  #高级选项-所有者
$securityInfo.Flags += 0x00000800L  #PageTitle属性作为标题
#$securityInfo.Flags += 0x00000008l  #禁止修改
$securityInfo.Flags += 0x00000020L  #高级选项-还原默认所有者
$securityInfo.Flags += 0x00040000L  #高级选项-还原默认值按钮
$securityInfo.Flags += 0x08000000L  #高级选项-还原默认值按钮
$securityInfo.ObjectName =  "AclUI.h";
#$securityInfo.Flags += 0x00200000l  #隐藏“高级安全设置”页面上的“特殊权限”选项卡

#$securityInfo.Flags = 0x02000000L  #所有者
$securityInfo._filePath = "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.22621.0\\um\\AclUI.h";

[FileSecurityInfo]::EditSecurity([IntPtr]::Zero, $securityInfo)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2320949.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据分析异步进阶:aiohttp与Asyncio性能提升

一、时间轴呈现方案进程 2023-04-01:需求确认 确定目标:使用aiohttp与Asyncio提升采集性能,目标采集今日头条网站的新闻数据(标题、内容、时间等)。同时要求在程序中加入代理IP、Cookie和UserAgent的设置,…

《AI大模型趣味实战 》第8集:多端适配 个人新闻头条 基于大模型和RSS聚合打造个人新闻电台(Flask WEB版) 2

《AI大模型趣味实战 》第8集:多端适配 个人新闻头条 基于大模型和RSS聚合打造个人新闻电台(Flask WEB版) 2 摘要 本文末尾介绍了如何实现新闻智能体的方法。在信息爆炸的时代,如何高效获取和筛选感兴趣的新闻内容成为一个现实问题。本文将带领读者通过P…

低配电脑畅玩《怪物猎人:荒野》,ToDesk云电脑优化从30帧到144帧?

《怪物猎人:荒野(Monster Hunter Wilds)》自2025年正式发售以来已取得相当亮眼的成绩,仅用三天时间便轻松突破800万销量,目前顺利蝉联周榜冠军;凭借着开放世界的宏大场景和丰富的狩猎玩法,该游戏…

【js逆向入门】图灵爬虫练习平台 第九题

地址:aHR0cHM6Ly9zdHUudHVsaW5ncHl0b24uY24vcHJvYmxlbS1kZXRhaWwvOS8 f12进入了debugger,右击选择一律不在此处暂停, 点击继续执行 查看请求信息 查看载荷,2个加密参数,m和tt 查看启动器,打上断点 进来 往…

NET6 WebApi第5讲:中间件(源码理解,俄罗斯套娃怎么来的?);Web 服务器 (Nginx / IIS / Kestrel)、WSL、SSL/TSL

一、NET6的启动流程 区别: .NET6 WebApi第1讲:VSCode开发.NET项目、区别.NET5框架【两个框架启动流程详解】_vscode webapi-CSDN博客 2、WebApplicationBuilder:是NET6引入的一个类,是建造者模式的典型应用 1>建造者模式的…

Nginx及前端部署全流程:初始化配置到生产环境部署(附Nginx常用命令)

nginx&前端从初始化配置到部署(xshell) 前言下载nginx前端打包与创建具体文件夹路径配置nginx.nginx.conf文件配置项内容 配置nginx.service文件配置项内容 启动nginx常用nginx命令 前言 目标:在xshell中部署前端包。 第一步&#xff1a…

python 实现一个简单的window 任务管理器

import tkinter as tk from tkinter import ttk import psutil# 运行此代码前,请确保已经安装了 psutil 库,可以使用 pip install psutil 进行安装。 # 由于获取进程信息可能会受到权限限制,某些进程的信息可能无法获取,代码中已经…

【xiaozhi赎回之路-2:语音可以自己配置就是用GPT本地API】

固件作用 打通了网络和硬件的沟通 修改固件实现【改变连接到小智服务器的】 回答逻辑LLM自定义 自定义了Coze(比较高级,自定义程度比较高,包括知识库,虚拟脚色-恋人-雅思老师-娃娃玩具{可能需要使用显卡对开源模型进行微调-产…

WX小程序

下载 package com.sky.utils;import com.alibaba.fastjson.JSONObject; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.Cl…

Spring boot 3.4 后 SDK 升级,暨 UI API/MCP 计划

PS 写这篇文章后看到 A Deep Dive Into MCP and the Future of AI Tooling | Andreessen HorowitzWe explore what MCP is, how it changes the way AI interacts with tools, what developers are already building, and the challenges that still need solving. https://a1…

Linux下JDK1.8安装配置

目录 1.下载完上传到Linux系统中 2.解压JDK压缩包 3.配置JDK环境变量 4.设置环境变量生效 5.查看环境变量是否配置成功 官网下载地址:Java Downloads | Oracle 1.下载完上传到Linux系统中 2.解压JDK压缩包 tar -zxvf jdk-8u151-linux-x64.tar.gz -C /usr/local (解压…

Python OCR文本识别详细步骤及代码示例

光学字符识别(OCR)是将图像中的文字转换为可编辑文本的技术。在Python中,我们可以利用多种库实现OCR功能。本文将详细介绍使用Tesseract和EasyOCR进行文本识别的步骤,并提供完整的代码示例。 一、OCR简介 OCR(Optical…

Linux固定IP方法(RedHat+Net模式)

1、查看当前网关 ip route | grep default 2、配置静态IP 双击重启 3、验证

210、【图论】课程表(Python)

题目 思路 这道题本质上是一个拓扑排序。每次先统计每个点的入度个数、然后再统计点与点之间的邻接关系,找到入度为0的点作为起始遍历点。之后每遍历到这个点之后,就把这个点后续的邻接关系边的点入度减去一。当某个点入度为0时,继续被加入其…

跟着StatQuest学知识07-张量与PyTorch

一、张量tensor 张量重新命名一些数据概念,存储数据以及权重和偏置。 张量还允许与数据相关的数学计算能够相对快速的完成。 通常,张量及其进行的数学计算会通过成为图形处理单元(GPUs)的特殊芯片来加速。但还有张量处理单元&am…

前端字段名和后端不一致?解锁 JSON 映射的“隐藏规则” !!!

🚀 前端字段名和后端不一致?解锁 JSON 映射的“隐藏规则” 🌟 嘿,技术冒险家们!👋 今天我们要聊一个开发中常见的“坑”:前端传来的 JSON 参数字段名和后端对象字段名不一致,会发生…

基于springboot的新闻推荐系统(045)

摘要 随着信息互联网购物的飞速发展,国内放开了自媒体的政策,一般企业都开始开发属于自己内容分发平台的网站。本文介绍了新闻推荐系统的开发全过程。通过分析企业对于新闻推荐系统的需求,创建了一个计算机管理新闻推荐系统的方案。文章介绍了…

2024年数维杯数学建模C题天然气水合物资源量评价解题全过程论文及程序

2024年数维杯数学建模 C题 天然气水合物资源量评价 原题再现: 天然气水合物(Natural Gas Hydrate/Gas Hydrate)即可燃冰,是天然气与水在高压低温条件下形成的类冰状结晶物质,因其外观像冰,遇火即燃&#…

Linux与HTTP中的Cookie和Session

HTTP中的Cookie和Session 本篇介绍 前面几篇已经基本介绍了HTTP协议的大部分内容,但是前面提到了一点「HTTP是无连接、无状态的协议」,那么到底有什么无连接以及什么是无状态。基于这两个问题,随后解释什么是Cookie和Session,以…

linux 备份工具,常用的Linux备份工具及其备份数据的语法

在Linux系统中,备份数据是确保数据安全性和完整性的关键步骤。以下是一些常用的Linux备份工具及其备份数据的语法: 1. tar命令 tar命令是Linux系统中常用的打包和压缩工具,可以将多个文件或目录打包成一个文件,并可以选择添加压…