作者theodolite (無)
看板C_Sharp
標題[問題] DllImport回傳LPBYTE的問題?
時間Thu May 7 19:33:52 2009
有關比較基本的DllImport已經可以成功了,目前的問題是要呼叫:
HRESULT __stdcall CeSvcGetBinary
(HCESVC hSvc, LPCSTR pszValName, LPBYTE pszVal, LPDWORD pcbVal);
這部份我在c#中有試過:
[DllImport("ceutil.dll")]
public static extern Uint32 CeSvcGetBinary
(IntPtr hSvc, string pszValName, //這部份沒什麼問題
[Out] IntPtr pszVal, //這個不確定怎麼回傳
ref int pcbVal);
使用上則是:
int dataLength = 0;
IntPtr dataByte = Marshal.AllocHGlobal (32);//32 bytes
hr = CeSvcGetBinary (phSvc, "BinaryData", dataBytes, ref dtaLength);
//使用Marshal將資料從dataByte中讀出來
byte[] dataDump = new byte [dataLength];
for(int index = 0; index < dataLength; index++)
dataDump[index] = Marshal.ReadByte(dataBytes, index);
這是我目前試出來會有資料寫進去,但是資料是錯誤的。因為是透過CEUtil去讀Registry
KEY是固定值,沒理由每次讀出來都不對,但是這是我目前達成的。
這樣的HRESULT會是0x800700ea,看起來像是ERROR_MORE_DATA,
但是不論給多少的空間都一樣,所以想說問題應該不是如此才對。
有關pszVal,我有試過像是ref byte[] pszVal或是其他直接byte array,
都不會有任何回應。
用String似乎比較簡單,可以用StringBuilder,是否應該嘗試用StringBuilder?
還是我的觀念上仍然有問題?請各位不吝指教,謝謝。
補充:StringBuilder也不太妙,但是參考PInvoke網站上的寫法,
是直接傳byte[]進去?不知有沒有成功的案例?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.63.100.2
1F:→ pico2k:dataDump[pos] -> dataDump[index] ? 05/08 01:02
2F:→ pico2k:Net Framework有讀取registry的相關API 05/08 01:04
3F:→ theodolite:要透過CEUtil是為了避免未來registry的位置變動 05/08 09:20
※ 編輯: theodolite 來自: 210.63.100.2 (05/08 14:23)