读取存储在注册表中的GUID值 [英] Read GUID value stored in registry

查看:413
本文介绍了读取存储在注册表中的GUID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Delphi的注册表中读取存储为二进制值的GUID值值。
当我用BintoHex读取它,但结果是相反的顺序。
似乎我必须交换字节,但我认为BinToHex会这样做。



我指向这个线程,但我找不到正确的解决方案:



所有您需要做的是将相关字节直接复制到类型为 TGUID 的变量中,您的工作是完成了如果您想要一个字符串表示,请使用 GUIDToString

  var 
GUID:TGUID;
strGUID:string;
....
GUID:= PG​​UID(PAnsiChar(buffer)+14)^;
strGUID:= GUIDToString(GUID);

这将自动处理所有的endian问题。我们来看看 TGUID 的声明:

  TGUID =打包记录
D1:LongWord;
D2:Word;
D3:Word;
D4:字节数组[0..7]
结束

您的代码将其视为一个直接的字节数组。但是,在记录中, D1 D2 D3 是一个小端机的整体类型。所以你的代码反转前4个字节,那些属于 D1 的字节。然后它反转 D2 的两个字节,以及 D3 的两个字节。当然,在GUID结尾的8个字节的数组当然不会反转。



所以,尽管你可以很容易地做所有的字节交换自己,使用记录来执行此操作,并利用将二进制GUID记录转换为格式正确的GUID字符串的帮助函数。


I try to read a GUID value stored as a binary value in registry in Delphi. When I read it with BintoHex, but the result is in reverse order. It seems that I have to swap bytes but I thought BinToHex would do it.

I refered to this thread but I can't find the right solution : how to convert byte array to its hex representation in Delphi It seems that it is due to little Eendian.

Below, you can see the GUID stored in registry

Here is my code :

var
s : string;
buffer : pointer;
...

begin
getmem(buffer, 1024*1024);
....
reg.ReadBinaryData(iValueOfregistry, buffer^, 1024*1024);
....
bintohex(pointer(longint(buffer)+14), PChar(s), 32);

Output for s : 90E24D373F126545916439C4925E467B

GUID should be FOLDERID_Downloads GUID :
{374DE290-123F-4565-9164-39C4925E467B}

Please help

解决方案

A GUID in binary form is best thought of as a record. Indeed in Delphi the record already exists – it is TGUID.

All you need to do is to copy the relevant bytes directly into a variable of type TGUID and your job is done. If you want a string representation, use GUIDToString.

var
  GUID: TGUID;
  strGUID: string;
....
GUID := PGUID(PAnsiChar(buffer)+14)^;
strGUID := GUIDToString(GUID);

This will take care of all the endian issues automatically. Let's take a look at the declaration of TGUID:

TGUID = packed record
  D1: LongWord;
  D2: Word;
  D3: Word;
  D4: array[0..7] of Byte;
end;

Your code treats this as a straight array of bytes. However, in the record, D1, D2 and D3 are integral types on a little endian machine. So your code reverses the first 4 bytes, those belonging to D1. It then reverses the two bytes of D2, and also the two bytes of D3. The array of 8 bytes at the end of the GUID are not reversed, of course.

So, whilst you could readily do all the byte swapping yourself, it's far better to use a record to do so, and take advantage of the helper function that converts a binary GUID record to a properly formatted GUID string.

这篇关于读取存储在注册表中的GUID值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆