gpt4 book ai didi

c# - ERROR_MORE_DATA ---- 从注册表读取

转载 作者:行者123 更新时间:2023-11-28 08:29:54 28 4
gpt4 key购买 nike

我正在尝试使用 windows ddk 7 包中提供的 offreg.dll 在内存中创建一个离线注册表。

您可以在此处找到有关 offreg.dll 的更多信息:MSDN

目前,在尝试从打开的注册表配置单元/ key 中读取值时,我收到以下错误:234 或 ERROR_MORE_DATA

这是包含 ORGetValue 的 .h 代码:

DWORD
ORAPI
ORGetValue (
__in ORHKEY Handle,
__in_opt PCWSTR lpSubKey,
__in_opt PCWSTR lpValue,
__out_opt PDWORD pdwType,
__out_bcount_opt(*pcbData) PVOID pvData,
__inout_opt PDWORD pcbData
);

这是我用来提取数据的代码

[DllImport("offreg.dll", CharSet = CharSet.Auto, EntryPoint = "ORGetValue", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public static extern uint ORGetValue(IntPtr Handle, string lpSubKey, string lpValue, out uint pdwType, out string pvData, out uint pcbData);

IntPtr myHive;
IntPtr myKey;
string myValue;
uint pdwtype;
uint pcbdata;

uint ret3 = ORGetValue(myKey, "", "DefaultUserName", out pdwtype, out myValue, out pcbdata);

目标是能够将 myValue 作为字符串读取。

我不确定我是否需要使用编码(marshal)处理...或使用调整后的缓冲区进行第二次调用...或者实际上如何在 C# 中调整缓冲区。任何帮助或指示将不胜感激。

谢谢。

最佳答案

pcbData 参数上的属性错误,是 ref,不是 out。您需要将其初始化为您为 pvData 参数传递的 StringBuilder 的容量。现在 API 函数可能会看到 0,因此将返回错误代码。

它应该看起来像这样:

[DllImport("offreg.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint ORGetValue(IntPtr Handle, string lpSubKey, string lpValue, out int pdwType, StringBuilder pvData, ref int pcbData);

int pdwtype;
var buffer = new StringBuilder(256);
int pcbdata = buffer.Capacity;
uint ret3 = ORGetValue(myKey, "", "DefaultUserName", out pdwtype, buffer, ref pcbdata);
string myValue = buffer.ToString();

关于c# - ERROR_MORE_DATA ---- 从注册表读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2623927/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com