使用PInvoke注册表函数,如何从句柄指针获取数据?

时间:2010-05-29 00:27:51

标签: c# .net pinvoke

假设我正在使用RegQueryValueEx:

http://msdn.microsoft.com/en-us/library/ms724911(v=VS.85).aspx

我可以获得lpType的指针句柄,但它只是一个整数,我如何实际获取lpType指向的数据?

3 个答案:

答案 0 :(得分:1)

As it clearly tells says on MSDN它只是一个整数...它只是一个从0到7的枚举,其中0是REG_NONE ... 7是REG_MULTI_SZ。查看here for more detail查看关键字类型。

答案 1 :(得分:1)

'lpType'不指向实际数据,实际数据在'lpData'参数中返回。

答案 2 :(得分:-1)

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:   ...