2010-04-12 83 views
1

如何从下面的DLL中获取值? offreg.dll。ERROR_MORE_DATA --- PVOID和C#---非托管类型

在我下面的代码,我已经成功地打开蜂箱,关键现在我想拿到钥匙的价值,我一直运行到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应该是绰绰有余。

任何帮助将不胜感激。

如上图所示,我已经试过字符串生成器... ...串的IntPtr ...等,这些都不是能够处理出PVData的...

谢谢。

回答

1

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

在您的PInvoke definiation pcbData

所以应该是一个裁判PARAM和具有非零值会:

[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); 
+1

这是在头版上有10个月大的问题,因为有人决定对其进行编辑14几分钟前。 – shf301 2011-02-06 21:15:10