說我使用的RegQueryValueEx:使用PInvoke註冊表函數,如何從句柄指針獲取數據?
http://msdn.microsoft.com/en-us/library/ms724911(v=VS.85).aspx
我能得到的指針句柄lpType,但它只是一個整數,我如何真正得到的數據lpType指向?
說我使用的RegQueryValueEx:使用PInvoke註冊表函數,如何從句柄指針獲取數據?
http://msdn.microsoft.com/en-us/library/ms724911(v=VS.85).aspx
我能得到的指針句柄lpType,但它只是一個整數,我如何真正得到的數據lpType指向?
As it clearly tells says on MSDN它只是一個整數...它只是一個從0到7的枚舉,其中0是REG_NONE ... 7是REG_MULTI_SZ。檢查出here for more detail查看關鍵類型。
'lpType'不指向實際數據,實際數據返回'lpData'參數中。
logicnp是正確的。
m0s有正確的想法,但我會爭論該MSDN鏈接的清晰度。 lpType
實際上應該是無符號整數類型。我決定做一個枚舉爲我自己的理智:
/// <summary>Specify the data type to retrieve from the registry</summary>
/// <remarks> https://msdn.microsoft.com/en-us/library/windows/desktop/ms724884%28v=vs.85%29.aspx </remarks>
public enum RegDataType
{
/// <summary>[0] No defined value type.</summary>
REG_NONE,
/// <summary>[1] Null-terminated string.
/// It will be a Unicode or ANSI string, depending on whether you use the Unicode or ANSI functions.</summary>
REG_SZ,
/// <summary>[2] Null-terminated string that contains unexpanded references to environment variables (for example, "%PATH%").
/// It will be a Unicode or ANSI string, depending on whether you use the Unicode or ANSI functions.</summary>
REG_EXPAND_SZ,
/// <summary>[3] Binary data in any form.</summary>
REG_BINARY,
/// <summary>[4] 32-bit number.</summary>
REG_DWORD,
/* /// <summary> [4] 32-bit number in little-Endean format. This is equivalent to REG_DWORD.
/// In little-Endean format, a multi-byte value is stored in memory from the lowest byte (the "little end") to the highest byte.
//// For example, the value 0x12345678 is stored as (0x78 0x56 0x34 0x12) in little-Endean format.</summary>
*/ //REG_DWORD_LITTLE_ENDIAN 4
/// <summary>[5] 32-bit number in big-Endean format.
/// In big-Endean format, a multi-byte value is stored in memory from the highest byte (the "big end") to the lowest byte.
/// For example, the value 0x12345678 is stored as (0x12 0x34 0x56 0x78) in big-Endean format.</summary>
REG_DWORD_BIG_ENDIAN,
/// <summary>[6]Unicode symbolic link.</summary>
REG_LINK,
/// <summary>[7] Array of null-terminated strings that are terminated by two null characters.</summary>
REG_MULTI_SZ,
/// <summary>[8]Device-driver resource list.</summary>
REG_RESOURCE_LIST,
/// <summary>[9] </summary>
REG_FULL_RESOURCE_DESCRIPTOR,
/// <summary>[10] </summary>
REG_RESOURCE_REQUIREMENTS_LIST,
/// <summary>[11] 64-bit number.</summary>
REG_QWORD
/* /// <summary>[11] A 64-bit number in little-Endean format. This is equivalent to REG_QWORD.</summary>
*/ //REG_QWORD_LITTLE_ENDIAN 11
}
一旦你的,你可以通過數據類型轉換,封送指針結果,並根據類型轉換。由於您無法直接編組爲無符號類型,因此您也需要考慮這一點。 不要忘記錯誤處理。
段:
switch ((Enums.RegDataType)type)
{
case Enums.RegDataType.REG_SZ:
return Marshal.PtrToStringAnsi(pResult);
case Enums.RegDataType.REG_DWORD:
int DWORDCouldBeNegative = Marshal.ReadInt32(pResult);
uint DWORDCastBackToUInt = (uint)DWORDCouldBeNegative;
return DWORDCastBackToUInt.ToString();
case Enums.RegDataType.REG_QWORD:
Int64 QWORDCouldBeNegative = Marshal.ReadInt64(pResult);
UInt64 QWORDCastBackToUInt = (UInt64)QWORDCouldBeNegative;
return QWORDCastBackToUInt.ToString();
case Enums.RegDataType.REG_BINARY: ...
爲什麼向下票呢? – 2016-07-29 19:49:48