remotereg.pp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. {
  2. This example program shows how to remotely connect to another machine.
  3. The machine name is given as UNC name as first argument (e.g.
  4. "\\SomeComputer") and the username as second argument. The password will be
  5. queried on the command line by Windows. If the connection is successful the
  6. keys of HKEY_USERS of the remote machine will be enumerated.
  7. Tested on a Windows 7 machine connecting to another Windows 7 machine.
  8. Note: The remote registry service must run on the destination machine.
  9. }
  10. program remotereg;
  11. {$mode objfpc}{$H+}
  12. {$apptype console}
  13. uses
  14. Classes, sysutils, registry, JwaWinNetWk, JwaWinType, JwaWinError;
  15. var
  16. reg: TRegistry;
  17. machine, username, s: String;
  18. res: NETRESOURCEA;
  19. err: DWORD;
  20. sl: TStringList;
  21. begin
  22. if ParamCount > 0 then
  23. machine := ParamStr(1)
  24. else
  25. machine := '';
  26. if ParamCount > 1 then
  27. username := ParamStr(2)
  28. else
  29. username := '';
  30. { if we have a username given then we need to establish a connection first;
  31. if no username is given then either the current user is the correct one
  32. or a connection to e.g. share was established already }
  33. if (machine <> '') and (username <> '') then begin
  34. Writeln('Connecting to ', machine, ' as ', username);
  35. FillChar(res, SizeOf(res), 0);
  36. res.dwType := RESOURCETYPE_ANY;
  37. res.lpRemoteName := PChar(Format('%s\IPC$', [machine]));
  38. err := WNetAddConnection2A(res, Nil, PChar(username),
  39. CONNECT_TEMPORARY or CONNECT_INTERACTIVE or CONNECT_COMMANDLINE);
  40. { ERROR_SESSION_CREDENTIAL_CONFLICT means that we already connected to the
  41. host and the connection is still available }
  42. if (err <> NO_ERROR) and (err <> ERROR_SESSION_CREDENTIAL_CONFLICT) then begin
  43. Writeln('Error connecting to remote machine ''', machine, ''': ',
  44. SysErrorMessage(err), ' (', err, ')');
  45. Exit;
  46. end;
  47. end;
  48. { for this test we want only the right to enumerate subkeys }
  49. reg := TRegistry.Create(KEY_ENUMERATE_SUB_KEYS);
  50. try
  51. { use HKEY_USERS, because the rights of Administrator users on Windows Vista
  52. and newer are sufficient for enumerating this }
  53. reg.RootKey := HKEY_USERS;
  54. if not reg.RegistryConnect(machine) then begin
  55. Writeln('Error connecting to remote registry');
  56. Exit;
  57. end;
  58. { we need to open the key nevertheless }
  59. reg.OpenKeyReadOnly('\');
  60. { now enumerate the subkeys }
  61. sl := TStringList.Create;
  62. try
  63. reg.GetKeyNames(sl);
  64. Writeln(sl.Count, ' keys found');
  65. for s in sl do
  66. Writeln(#9, s);
  67. finally
  68. sl.Free;
  69. end;
  70. reg.CloseKey;
  71. finally
  72. reg.Free;
  73. end;
  74. Writeln('Done');
  75. Readln;
  76. end.