吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 664|回复: 0
收起左侧

[求助] C# P/Invoke Delegate 调用 C++ dll 报错 234

[复制链接]
MIAIONE 发表于 2023-1-15 17:47
错误信息:
[C#] 纯文本查看 复制代码
Unhandled exception. System.ComponentModel.Win32Exception (234): 有更多数据可用。
   at OffineRegistry.RegistryKey.GetSubKeyNames() in D:\.NET_7\OffineRegistry\RegistryKey.cs:line 212
   at OffineRegistry.RegistryKey.DeleteSubKeyTreePrivate(RegistryKey key) in D:\.NET_7\OffineRegistry\RegistryKey.cs:line 353
   at OffineRegistry.RegistryKey.DeleteSubKey(String name) in D:\.NET_7\OffineRegistry\RegistryKey.cs:line 336
   at Offine.Test.Program.Main() in D:\.NET_7\OffineRegistry\test\Offine.Test\Program.cs:line 23


代码仓库: https://github.com/MIAIONE/OffineRegistry

原作者仓库: https://github.com/LordMike/OffregLib

改动:

Createsubkey多层级创建是没问题的, 原仓库使用 DllImport 调用 offreg.dll , 我修改了调用方法, 使用委托动态加载(不然会报错)
delegate不能重载同名函数, 也不知道是不是null传参的问题, 即便是 改成 IntPtr? 传进去null, 和原函数一样仍然报错(而且不知道错误是什么)
[C#] 纯文本查看 复制代码
public string[] GetSubKeyNames()
        {
            string[] results = new string[_metadata.SubKeysCount];

            for (uint item = 0; item < _metadata.SubKeysCount; item++)
            {
                uint sizeName = _metadata.MaxSubKeyLen + 1;

                StringBuilder sbName = new StringBuilder((int)sizeName);
                Win32Result result = InitOffreg.NativeApi.Syscall<Native.PtrClass.OREnumValue>()(_intPtr, item, sbName, ref sizeName, IntPtr.Zero, (IntPtr)null, IntPtr.Zero);

                if (result != Win32Result.ERROR_SUCCESS)
                    throw new Win32Exception((int)result);

                results[item] = sbName.ToString();
            }

            return results;
        }


Native方法已经大改:

[C#] 纯文本查看 复制代码
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Security;
using System.Text;

namespace OffineRegistry
{
    public enum RegValueType : uint
    {
        REG_NONE = 0,
        REG_SZ = 1,
        REG_EXPAND_SZ = 2,
        REG_BINARY = 3,
        REG_DWORD = 4,
        REG_DWORD_LITTLE_ENDIAN = 4,
        REG_DWORD_BIG_ENDIAN = 5,
        REG_LINK = 6,
        REG_MULTI_SZ = 7,
        REG_RESOURCE_LIST = 8,
        REG_FULL_RESOURCE_DESCRIPTOR = 9,
        REG_RESOURCE_REQUIREMENTS_LIST = 10,
        REG_QWORD = 11,
        REG_QWORD_LITTLE_ENDIAN = 11
    }

    public enum RegPredefinedKeys
    {
        HKEY_CLASSES_ROOT = unchecked((int)0x80000000),
        HKEY_CURRENT_USER = unchecked((int)0x80000001),
        HKEY_LOCAL_MACHINE = unchecked((int)0x80000002),
        HKEY_USERS = unchecked((int)0x80000003),
        HKEY_PERFORMANCE_DATA = unchecked((int)0x80000004),
        HKEY_CURRENT_CONFIG = unchecked((int)0x80000005),
        HKEY_DYN_DATA = unchecked((int)0x80000006),
        HKEY_CURRENT_USER_LOCAL_SETTINGS = unchecked((int)0x80000007)
    }

    public enum KeyDisposition : long
    {
        REG_CREATED_NEW_KEY = 0x00000001,
        REG_OPENED_EXISTING_KEY = 0x00000002
    }

    public enum KeySecurity
    {
        KEY_QUERY_VALUE = 0x0001,
        KEY_SET_VALUE = 0x0002,
        KEY_ENUMERATE_SUB_KEYS = 0x0008,
        KEY_NOTIFY = 0x0010,
        DELETE = 0x10000,
        STANDARD_RIGHTS_READ = 0x20000,
        KEY_READ = 0x20019,
        KEY_WRITE = 0x20006,
        KEY_ALL_ACCESS = 0xF003F,
        MAXIMUM_ALLOWED = 0x2000000
    }

    [Flags]
    public enum RegOption : uint
    {
        REG_OPTION_RESERVED = 0x00000000,
        REG_OPTION_NON_VOLATILE = 0x00000000,
        REG_OPTION_VOLATILE = 0x00000001,
        REG_OPTION_CREATE_LINK = 0x00000002,
        REG_OPTION_BACKUP_RESTORE = 0x00000004,
        REG_OPTION_OPEN_LINK = 0x00000008
    }

    public enum SECURITY_INFORMATION : uint
    {
        OWNER_SECURITY_INFORMATION = 0x00000001,
        GROUP_SECURITY_INFORMATION = 0x00000002,
        DACL_SECURITY_INFORMATION = 0x00000004,
        SACL_SECURITY_INFORMATION = 0x00000008,
        LABEL_SECURITY_INFORMATION = 0x00000010,
        PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000,
        PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000,
        UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000,
        UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000,
    }
    [SuppressUnmanagedCodeSecurity]
    internal class Native
    {
        //internal const string OffRegDllName = "offreg.dll";

        //[DllImport(OffRegDllName, EntryPoint = "ORCreateHive", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORCreateHive(out IntPtr rootKeyHandle);

        //[DllImport(OffRegDllName, EntryPoint = "OROpenHive", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result OROpenHive(string path, out IntPtr rootKeyHandle);

        //[DllImport(OffRegDllName, EntryPoint = "ORCloseHive", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORCloseHive(IntPtr rootKeyHandle);

        //[DllImport(OffRegDllName, EntryPoint = "ORSaveHive", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORSaveHive(
            IntPtr rootKeyHandle,
            string path,
            uint dwOsMajorVersion,
            uint dwOsMinorVersion);

        //[DllImport(OffRegDllName, EntryPoint = "ORCloseKey")]
        internal delegate Win32Result ORCloseKey(IntPtr hKey);

        //[DllImport(OffRegDllName, EntryPoint = "ORCreateKey", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORCreateKey(
            IntPtr hKey,
            string lpSubKey,
            string lpClass,
            RegOption dwOptions,
            /*ref SECURITY_DESCRIPTOR*/ IntPtr lpSecurityDescriptor,
            /*ref IntPtr*/ out IntPtr phkResult,
            out KeyDisposition lpdwDisposition);

        //[DllImport(OffRegDllName, EntryPoint = "ORDeleteKey", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORDeleteKey(
            IntPtr hKey,
            string lpSubKey);

        //[DllImport(OffRegDllName, EntryPoint = "ORDeleteValue", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORDeleteValue(
            IntPtr hKey,
            string lpValueName);

        //[DllImport(OffRegDllName, EntryPoint = "OREnumKey", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result OREnumKey(
            IntPtr hKey,
            uint dwIndex,
            StringBuilder lpName,
            ref uint lpcchName,
            StringBuilder lpClass,
            ref uint lpcchClass,
            ref FILETIME lpftLastWriteTime);

        //[DllImport(OffRegDllName, EntryPoint = "OREnumValue", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result OREnumValue(
            IntPtr hKey,
            uint dwIndex,
            StringBuilder lpValueName,
            ref uint lpcchValueName,
            out RegValueType lpType,
            IntPtr lpData,
            ref uint lpcbData);



        //[DllImport(OffRegDllName, EntryPoint = "ORGetKeySecurity")]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORGetKeySecurity(
            IntPtr hKey,
            SECURITY_INFORMATION securityInformation,
            IntPtr pSecurityDescriptor,
            ref uint lpcbSecurityDescriptor);

        //[DllImport(OffRegDllName, EntryPoint = "ORGetValue", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORGetValue(
            IntPtr hKey,
            string lpSubKey,
            string lpValue,
            out RegValueType pdwType,
            IntPtr pvData,
            ref uint pcbData);



        //[DllImport(OffRegDllName, EntryPoint = "OROpenKey", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result OROpenKey(
            IntPtr hKey,
            string lpSubKey,
            out IntPtr phkResult);

        //[DllImport(OffRegDllName, EntryPoint = "ORQueryInfoKey", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORQueryInfoKey(
            IntPtr hKey,
            StringBuilder lpClass,
            ref uint lpcchClass,
            ref uint lpcSubKeys,
            ref uint lpcbMaxSubKeyLen,
            ref uint lpcbMaxClassLen,
            ref uint lpcValues,
            ref uint lpcbMaxValueNameLen,
            ref uint lpcbMaxValueLen,
            ref uint lpcbSecurityDescriptor,
            ref FILETIME lpftLastWriteTime);

        //[DllImport(OffRegDllName, EntryPoint = "ORSetValue", CharSet = CharSet.Unicode)]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORSetValue(
            IntPtr hKey,
            string lpValueName,
            RegValueType dwType,
            IntPtr lpData,
            uint cbData);

        //[DllImport(OffRegDllName, EntryPoint = "ORSetKeySecurity")]
        [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
        internal delegate Win32Result ORSetKeySecurity(
            IntPtr hKey,
            SECURITY_INFORMATION securityInformation,
            /*ref IntPtr*/ IntPtr pSecurityDescriptor);

        public class PtrClass
        {
            //[DllImport(OffRegDllName, EntryPoint = "ORGetValue", CharSet = CharSet.Unicode)]
            [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
            internal delegate Win32Result ORGetValue(
                IntPtr hKey,
                string lpSubKey,
                string lpValue,
                out RegValueType pdwType,
                IntPtr pvData,
                IntPtr pcbData);
            //[DllImport(OffRegDllName, EntryPoint = "OREnumValue", CharSet = CharSet.Unicode)]
            [UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode)]
            internal delegate Win32Result OREnumValue(
                IntPtr hKey,
                uint dwIndex,
                StringBuilder lpValueName,
                ref uint lpcchValueName,
                IntPtr lpType,
                IntPtr lpData,
                IntPtr lpcbData);

        }
        /*-------------------------------------------------------------------*/
        public readonly IntPtr OffregLibraryAddress;
        public Native(string offregPath)
        {
            OffregLibraryAddress = LoadLibrary(offregPath);
        }
        ~Native()
        {
            FreeLibrary(OffregLibraryAddress);
        }
        public T Syscall<T>() where T : Delegate
        {
            return Marshal.GetDelegateForFunctionPointer<T>(GetProcAddress(OffregLibraryAddress, typeof(T).Name));
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern IntPtr LoadLibrary(string lpFileName);

        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool FreeLibrary(IntPtr hModule);
    }
}


如果有时间帮忙测试下 有没有其他bug, 这个库已经上传 nuget了, 现在有bug希望大家支持下

免费评分

参与人数 2吾爱币 +1 热心值 +2 收起 理由
TanXin + 1 用心讨论,共获提升!
opj1314 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-5 02:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表