procedure TForm1.Button1Click(Sender: TObject);
var
  reg:tregistry;
  typ:tregdatatype;
  value:string;
  aBuffer:array[0..1024] of char;
begin
  reg:=tregistry.Create;
try  reg.RootKey:=HKEY_LOCAL_MACHINE;
  if reg.OpenKey('SOFTWARE\Microsoft\DRM',true) then
     begin
       typ:= reg.GetDataType('DataPath');
       case typ of
            rdString:  value:=reg.readstring('DataPath');
            rdexpandstring: value:=reg.readstring('DataPath');
            rdInteger: value:=inttostr(reg.ReadInteger('DataPath'));
            rdBinary:  begin
                         reg.ReadBinaryData('DataPath',aBuffer,Sizeof(aBuffer));
                         value:=(string(abuffer));//为什么输出的只有“C”?
                       end;       end;
       showmessage('键值为:'+value);
     end;
finally
  Reg.CloseKey;
  Reg.Free;
end;
end;为什么我读取一个类型为REG_Binary的键值不成功?应该怎样写代码?

解决方案 »

  1.   

    改为
     reg.ReadBinaryData('DataPath',aBuffer[0],Sizeof(aBuffer));

     reg.ReadBinaryData('DataPath',@aBuffer[0],Sizeof(aBuffer));
    试一试
      

  2.   

    因为读回的是unicode的字节码,需要进行unicode转换才能成ansistring
      

  3.   

    我用D2010,你的代码我试验了下,输出显示为:键值为:C:\Documents and Settings\All Users\DRM
      

  4.   

    不是吧,这和delphi 的版本有关系么?
      

  5.   

    有关系,string类型在D2009以后版本默认为Unicode双字节字符串,而之前是单字节字符串
      

  6.   

    //delphi7
    {我发现我也够无聊的
    下面是 delphi7 测试通过代码,
    结果显示为:C:\Documents and Settings\All Users\DRM
    2010-05-09
    }
    var
      Reg: TRegistry;
      P: PByte;
      DataInfo: TRegDataInfo;
    begin
      Reg := TRegistry.Create;
      Reg.RootKey := HKEY_LOCAL_MACHINE;
      Reg.OpenKey('SOFTWARE\Microsoft\DRM', False);
      try
        Reg.GetDataInfo('DataPath', DataInfo);
        GetMem(P, DataInfo.DataSize);
        Reg.ReadBinaryData('DataPath', P^, DataInfo.DataSize);
        ShowMessage(PWideChar(P));//得到结果咯
        Edit1.Text := PWideChar(P);
      finally
        FreeMem(P);
        Reg.CloseKey;
        Reg.Free;
      end;
    end;//delphi2007
    {我发现我也够无聊的
    下面是 delphi2007 测试通过代码,
    结果显示为:C:\Documents and Settings\All Users\DRM
    2010-05-09
    }
    var
      Reg: TRegistry;
      P: PByte;
      DataInfo: TRegDataInfo;
    begin
      Reg := TRegistry.Create;
      Reg.RootKey := HKEY_LOCAL_MACHINE;
      Reg.OpenKey('SOFTWARE\Microsoft\DRM', False);
      try
        Reg.GetDataInfo('DataPath', DataInfo);
        GetMem(P, DataInfo.DataSize);
        Reg.ReadBinaryData('DataPath', P^, DataInfo.DataSize);
        ShowMessage(PWideChar(P));//得到结果咯
      finally
        FreeMem(P);
        Reg.CloseKey;
        Reg.Free;
      end;
    end;
    //Delphi2010
    {我只给出如何读取,至于如何比较我认为网络上很多
    delphi2010测试通过
    2010-05-09
    }
    var
      Reg: TRegistry;
      P: PByte;
      DataInfo: TRegDataInfo;
    begin
      Reg := TRegistry.Create;
      Reg.RootKey := HKEY_LOCAL_MACHINE;
      Reg.OpenKey('SOFTWARE\Microsoft\DRM', False);
      try
        Reg.GetDataInfo('DataPath', DataInfo);
        GetMem(P, DataInfo.DataSize);
        Reg.ReadBinaryData('DataPath', P^, DataInfo.DataSize);
        ShowMessage(PChar(P));//得到结果咯
      finally
        FreeMem(P);
        Reg.CloseKey;
        Reg.Free;
      end;
    end;满意么???