2010-04-12 68 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