首页 > 解决方案 > 将 uint 传递给 IntPtr 参数

问题描述

我正在尝试使用 RegCreateKeyExA 函数创建注册表项。我对 C# 很陌生,所以我可能错了,但我得出的结论是第一个参数是 IntPtr。问题是我不知道如何使用 IntPtr 数据类型,并且对我在该主题上找到的所有不同线程感到有点不知所措。我需要澄清我应该使用的数据类型以及如何处理它们。这是我的代码:

using System;
using System.Runtime.InteropServices;

namespace Win32Application
{
    public class Win32
    {
        public static uint KEY_CREATE_SUB_KEY = 4;
        public static uint HKEY_CURRENT_USER = 80000001;
        public static uint REG_OPTION_NON_VOLATILE = 0;

        [DllImport("Advapi32.dll", CharSet = CharSet.Ansi)]
        public static extern int RegCreateKeyExA(IntPtr hKey, string lpSubKey, uint Reserved,
            string lpClass, uint dwOptions, uint samDesired, ref SECURITY_ATTRIBUTES lpSecurityAttributes,
            ref uint phkResult, ref uint lpdwDisposition);

        public struct SECURITY_ATTRIBUTES
        {
            ulong nLenght;
            IntPtr lpSecurityDescriptor;
            bool bInheritHandle;
        }

        static void Main()
        {
            uint result = 0;
            uint created_or_existing = 0;
            string subKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
            SECURITY_ATTRIBUTES secAttrs = new SECURITY_ATTRIBUTES();

            RegCreateKeyExA(ref HKEY_CURRENT_USER, subKey, 0, null, REG_OPTION_NON_VOLATILE,
                KEY_CREATE_SUB_KEY, ref secAttrs, ref result, ref created_or_existing);
        }
    }
}

标签: c#winapiregistry

解决方案


推荐阅读