[Lazarus] Remote Registry...

Sven Barth pascaldragon at googlemail.com
Fri Aug 23 10:27:40 CEST 2013


Am 23.08.2013 10:05, schrieb Sven Barth:
> Am 23.08.2013 09:50, schrieb Michael Van Canneyt:
>>
>>
>> On Fri, 23 Aug 2013, Flávio Etrusco wrote:
>>
>>> On Thu, Aug 22, 2013 at 9:55 PM, waldo kitty 
>>> <wkitty42 at windstream.net> wrote:
>>>> On 8/22/2013 16:46, Liyuan Garcia Caballero wrote:
>>>>>
>>>>> Hi guys,
>>>>>
>>>>> I need access to remote registry of Microsoft Window and get a data,
>>>>> it's posible with Pascal?
>>>>
>>>>
>>>> of course it is possible... the question is how are you expecting 
>>>> to be able
>>>> to perform this task...
>>>>
>>>>> Somebody can share one code?
>>>>
>>>>
>>>> no but possibly an idea or two... write a remote "server" that 
>>>> accesses the
>>>> registry for whatever you want... then talk to it via a local 
>>>> "client"...
>>>>
>>>> in reality, it should be no different than using ssh or rdp into 
>>>> the remote
>>>> host and starting the remote machine's regedit or similar... 
>>>> capturing that
>>>> information and feeding it back across the link...
>>>>
>>>> NOTE: i use the term "server" above because you are wanting to 
>>>> connect to a
>>>> remote system and pull data from it... in the most general terms, 
>>>> that makes
>>>> it a server... the connecting package on the local side is then 
>>>> considered a
>>>> client...
>>>>
>>>
>>>
>>> I guess he means this:
>>> http://msdn.microsoft.com/en-us/library/windows/desktop/ms724840%28v=vs.85%29.aspx 
>>>
>>
>> Currently this is not supported by FPC.
>>
>> What needs to be done is implement
>>
>> function TRegistry.RegistryConnect(const UNCName: string): Boolean;
>> begin
>>   Result := False;
>> end;
>>
>> in packages/fcl-registry/src.
>>
>> The function indicated by Flavio must be used for this.
>>
>> So I'd guess that would be something like:
>>
>> function TRegistry.RegistryConnect(const UNCName: string): Boolean;
>>
>> Var
>>   Res : HKey;
>>
>> begin
>>   Result := RegConnectRegistryA(PAnsiChar(UNCName),FRootKey, at HKey);
>>   if Result then
>>     FCurentKey:=Res;
>> end;
>>
> Not entirely. According to the Delphi documentation RootKey needs to 
> be set instead of CurrentKey and also the key must be closed in the 
> end. I've already done the implementation and I'm currently testing it.
> As a workaround for older FPC versions simply set RootKey to the value 
> returned by RegConnectRegistry.
> Note: The login stuff mentioned on the MSDN page needs to be done 
> nevertheless.
Implemented in FPC revision 25332.

Attached is an example program that shows how to remotely connect to 
another machine (this can also be used for using RegConnectRegistry 
directly in case of FPC 2.6.2 and older). The machine name is given as 
UNC name as first argument (e.g. "\\SomeComputer") and the username as 
second argument. The password will be queried on the command line by 
Windows. If the connection is successful the keys of HKEY_USERS of the 
remote machine will be enumerated. Tested on a Windows 7 machine 
connecting to another Windows 7 machine.

Note: The remote registry service must run on the destination machine.

Regards,
Sven
-------------- next part --------------
program remotereg;

{$mode objfpc}{$H+}
{$apptype console}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this }, sysutils, windows, registry, JwaWinNetWk,
  JwaWinType;

var
  reg: TRegistry;
  machine, username, s: String;
  res: NETRESOURCEA;
  err: DWORD;
  sl: TStringList;
begin
  if ParamCount > 0 then
    machine := ParamStr(1)
  else
    machine := '';
  if ParamCount > 1 then
    username := ParamStr(2)
  else
    username := '';

  if (machine <> '') and (username <> '') then begin
    Writeln('Connecting to ', machine, ' as ', username);
    FillChar(res, SizeOf(res), 0);
    res.dwType := RESOURCETYPE_ANY;
    res.lpRemoteName := PChar(Format('%s\IPC$', [machine]));
    err := WNetAddConnection2A(res, Nil, PChar(username),
             CONNECT_TEMPORARY or CONNECT_INTERACTIVE or CONNECT_COMMANDLINE);
    if (err <> NO_ERROR) and (err <> ERROR_SESSION_CREDENTIAL_CONFLICT) then begin
      Writeln('Error connecting to remote machine ''', machine, ''': ',
        SysErrorMessage(err), ' (', err, ')');
      Readln;
      Exit;
    end;
  end;

  reg := TRegistry.Create(KEY_ENUMERATE_SUB_KEYS);
  try
    reg.RootKey := HKEY_USERS;
    if not reg.RegistryConnect(machine) then begin
      Writeln('Error connecting to remote registry');
      Exit;
    end;
    reg.OpenKeyReadOnly('\');
    sl := TStringList.Create;
    try
      reg.GetKeyNames(sl);
      Writeln(sl.Count, ' keys found');
      for s in sl do
        Writeln(#9, s);
    finally
      sl.Free;
    end;
    reg.CloseKey;
  finally
    reg.Free;
  end;
  Writeln('Done');
  Readln;
end.



More information about the Lazarus mailing list