Hi guys,
I need access to remote registry of Microsoft Window and get a data, it's posible with Pascal?
Somebody can share one code?
Thank a lot

Mailing lists / lazarus / 2013-08
12 messages · 5 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi guys,
I need access to remote registry of Microsoft Window and get a data, it's posible with Pascal?
Somebody can share one code?
Thank a lot
Liyuan Garcia Caballero wroteHi 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
-Fl?vio
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;Michael.
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.
Regards,
Sven
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.Maybe commit the example in fcl-registry/examples ?
Michael.
Done in 25334. Including a few comments on what's happening.
Regards,
Sven
I guess he means this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724840%28…
Fl?vio Etrusco wroteI guess he means this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724840%28v=vs.85%29.aspx
interesting... you'd think that people would mention these things and give links as above if it is what they are actually after... thanks for the pointer and to michael for the additional information... and definitely thanks to sven for the implementation...
now we wait to see if the OP can update to the latest FPC SVN, as indicated by sven, to gain access to it...
He can also use the functionality in 2.6.x, 2.7.1 "just" wraps the necessary house keeping. For this he can open the remote registry with RegConnectRegistry and pass resulting HKEY handle to the Rootkey property of a TRegistry instance. Afterwards he can work normally with TRegistry. At the end he needs to close the Rootkey using RegCloseKey. This is basically what TRegistry now does in 2.7.1 as well.
Regards,
Sven
right but it has to be specifically added, correct? it has not (yet) been added other than in FPC trunk? i did see it come in here when i svn upped my FPC and lazarus a few hours ago ;)
can i assume that RegCloseKey sends the possibly modified key back to the remote? i'm just asking to learn O:)
Code in trunk:
=== code begin ===
sl := Nil;
reg := TRegistry.Create(KEY_READ);
try
reg.RootKey := HKEY_USERS;
if not reg.ConnectRegistry('\\SomeMachine') then
raise Exception.Create('Could not connect');
reg.OpenKeyReadOnly('\');
sl := TStringList.Create;
reg.GetKeyNames(sl);
for s in sl do
Writeln(s);
finally
reg.Free;
sl.Free;
end;=== code end ===
Equivalent code in 2.6.2 and earlier:
=== code begin ===
sl := Nil;
reg := TRegistry.Create(KEY_READ);
try
if RegConnectRegistry('\\SomeMachine', HKEY_USERS, key)
ERROR_SUCCESS then
raise Exception.Create('Could not connect');
reg.RootKey := key;
reg.OpenKeyReadOnly('\Software');
sl := TStringList.Create;
reg.GetKeyNames(sl);
for s in sl do
Writeln(s);
finally
reg.Free;
sl.Free;
end;=== code end ===
You are only opening either HKEY_USERS, HKEY_LOCAL_MACHINE or HKEY_PERFORMANCE_DATA using
RegConnectRegistry/TRegistry.ConnectRegistry. All other subkeys are opened using the normal registry API (be it directly or through TRegistry) and Windows handles the rest. When exactly it sends the modifications to the remote machine is up to Windows.
Regards,
Sven