ERROR_MORE_DATA --- PVOID 和 C# --- 非托管类型

发布于 2024-08-28 18:18:31 字数 1284 浏览 7 评论 0原文

如何从以下 DLL 中获取值? offreg.dll。

在下面的代码中,我已成功打开配置单元、密钥,现在我尝试获取密钥的值,但我不断遇到 ERROR_MORE_DATA (234) 错误。

这是 C++ .dll:

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
    );

这是我的 C# 代码:

        [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 StringBuilder pvData, out uint pcbData);

            IntPtr myHive;            
            IntPtr myKey;
            StringBuilder myValue = new StringBuilder("", 256);
            uint pdwtype;
            uint pcbdata;

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

所以问题似乎是围绕 PVOID pvData 我似乎无法理解正确的类型,或正确的缓冲区大小。总是出现234错误。

注意:运行此命令时 pcbdata = 28... 因此 256 应该足够了。

任何帮助将不胜感激。

如上所示,我尝试过字符串生成器... string... IntPtr... 等。其中没有一个能够处理 PVData 的输出...

谢谢。

How can I get the value from the following DLL? offreg.dll.

In my below code, I have successfully opened the hive, the key and now I am trying to get the value of the key and I keep running into the ERROR_MORE_DATA (234) error.

Here is the C++ .dll:

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
    );

Here is my C# code:

        [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 StringBuilder pvData, out uint pcbData);

            IntPtr myHive;            
            IntPtr myKey;
            StringBuilder myValue = new StringBuilder("", 256);
            uint pdwtype;
            uint pcbdata;

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

So the issue seems to be around PVOID pvData I can't seem to get the right type, or buffer size right. Always with the 234 error.

NOTE: When running this command pcbdata = 28... so 256 should be more than enough.

Any help would be greatly appreciated.

As shown above, I've tried string builder... string... IntPtr... etc. None of which were able to handle the out of PVData...

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2024-09-04 18:18:32

在传入之前,您需要将 pcbData 初始化为缓冲区的大小。请记住,C 不知道您传递的缓冲区有多大,传入的 pcbData 值告诉函数 pvData 有多大。在您的情况下,您传递零,告诉 OrGetValue 您 pvData 是一个 0 字节缓冲区,因此它会响应告诉您它需要更大的缓冲区。

因此,在您的 PInvoke 定义中 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 StringBuilder pvData, ref uint pcbData);

IntPtr myHive;            
IntPtr myKey;
StringBuilder myValue = new StringBuilder("", 256);
uint pdwtype;
uint pcbdata = myValue.Capacity();

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

You need to initialize pcbData to the size of your buffer before passing it in. Remember C doesn't know how large of a buffer you are passing it, the pcbData value coming in tells the function how large pvData is. In your case you are passing in zero, telling OrGetValue that you that pvData is a 0 byte buffer, so it responds telling you it needs a larger buffer.

So in your PInvoke definiation pcbData should be a ref param and have a non-zero value going in:

[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 StringBuilder pvData, ref uint pcbData);

IntPtr myHive;            
IntPtr myKey;
StringBuilder myValue = new StringBuilder("", 256);
uint pdwtype;
uint pcbdata = myValue.Capacity();

uint ret3 = ORGetValue(myKey, "", "DefaultUserName", out pdwtype, out myValue, ref pcbdata);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文