簡體   English   中英

ERROR_MORE_DATA-從注冊表讀取

[英]ERROR_MORE_DATA -— Reading from Registry

我正在嘗試使用Windows ddk 7軟件包中提供的offreg.dll在內存中創建脫機注冊表。

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

當前,當嘗試從打開的注冊表配置單元/鍵讀取值時,出現以下錯誤: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作為字符串。

我不確定是否需要使用封送處理或使用已調整緩沖區的第二次調用。或者實際上是如何在C#中調整緩沖區。 任何幫助或指針將不勝感激。

謝謝。

pcbData參數上的屬性是錯誤的,它是ref,而不是out。 您需要將其初始化為傳遞給pvData參數的StringBuilder的Capacity。 現在,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();

對於輸出字符串參數,應使用StringBuilder而不是字符串。

一般規則是,如果參數為LPCTSTRLPCSTRLPCWSTR ),則使用字符串;如果參數為LPTSTRLPSTRLPWSTR ),則使用StringBuilder

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM