Mam taki problem że troche pokleiłem troche napisałem sam kod ktury ma za zadanie zczytać zmienne globalne z innego procesu i tak robi ale nie do końca bo zczytuje tylko jedną a reszty niechce i niewiem dzie leży problem.PROSZE o jakąś pomoc

kod:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

PUNICODE_STRING = ^UNICODE_STRING;
UNICODE_STRING = record
Length: Word;
MaximumLength: Word;
Buffer: PWideChar;
end;

PANSI_STRING = ^ANSI_STRING;
ANSI_STRING = record
Length: Word;
MaximumLength: Word;
Buffer: PAnsiChar;
end;

CURDIR = record
(000)DosPath: UNICODE_STRING;
(008)Handle: Cardinal;
end;

_RTL_DRIVE_LETTER_CURDIR = record
(000)Flags: Word;
(002)Length: Word;
(004)TimeStamp: Cardinal;
(008)DosPath: ANSI_STRING;
end;

RTL_USER_PROCESS_PARAMETERS = record
(000)MaximumLength: Cardinal;
(004)Length: Cardinal;
(008)Flags: Cardinal;
(00c)DebugFlags: Cardinal;
(010)ConsoleHandle: Cardinal;
(014)ConsoleFlags: Cardinal;
(018)StandardInput: Cardinal;
(01c)StandardOutput: Cardinal;
(020)StandardError: Cardinal;
(024)CurrentDirectory: CURDIR;
(030)DllPath: UNICODE_STRING;
(038)ImagePathName: UNICODE_STRING;
(040)CommandLine: UNICODE_STRING;
(048)Environment: Pointer;
(04c)StartingX: Cardinal;
(050)StartingY: Cardinal;
(054)CountX: Cardinal;
(058)CountY: Cardinal;
(05c)CountCharsX: Cardinal;
(060)CountCharsY: Cardinal;
(064)FillAttribute: Cardinal;
(068)WindowFlags: Cardinal;
(06c)ShowWindowFlags: Cardinal;
(070)WindowTitle: UNICODE_STRING;
(078)DesktopInfo: UNICODE_STRING;
(080)ShellInfo: UNICODE_STRING;
(088)RuntimeData: UNICODE_STRING;
(090)CurrentDirectories: array[0..31] of _RTL_DRIVE_LETTER_CURDIR;
end;

PRTL_USER_PROCESS_PARAMETERS = ^RTL_USER_PROCESS_PARAMETERS;

PEB = packed record
Reserved1 : array[0..1] of Byte;
BeingDebugged: ByteBool;
Reserved2 : Byte;
Reserved3 : array[0..1] of Pointer;
Ldr : Pointer;
ProcessParameters: PRTL_USER_PROCESS_PARAMETERS;
Reserved4 : array[0..103]of Byte;
Reserved5 : array[0..51]of Pointer;

end;
PPEB = ^PEB;

PROCESS_BASIC_INFORMATION = packed record
ExitStatus : DWORD;
PebBaseAddress: PPEB;
AffinityMask : DWORD;
BasePriority : DWORD;
uUniqueProcessId: ULong;
uInheritedFromUniqueProcessId: ULong;
end;

TProcessBasicInformation = PROCESS_BASIC_INFORMATION;

var
Form1: TForm1;

implementation

{$R *.dfm}
function NtQueryInformationProcess(ProcessHandle:THandle;ProcessInformationClass:Byte;ProcessInformation:Pointer;ProcessInformationLength:ULONG;ReturnLength:PULONG):DWORD;stdcall;external 'ntdll.dll';

function HasReadAccess(hProcess:cardinal;pAddress:pointer;nSize:cardinal):bool;
var
memInfo:MEMORY_BASIC_INFORMATION;
begin
try
VirtualQueryEx(hProcess,pAddress,memInfo,sizeof(memInfo));
result:=(PAGE_EXECUTE <> memInfo.Protect) and (PAGE_NOACCESS <> memInfo.Protect);
if result then nSize:=memInfo.RegionSize else nSize:=0;
except
end;
end;

function GetProcessPth(PID:Cardinal):string;
const
ProcessBasicInformation = 0;
var
pbi:TProcessBasicInformation;
r,h:Cardinal;
ws:pwidechar;
cBuffRTLUserInfo:array [0..64]of char;
aPEB:PEB;
pAddrEnvStrBlock:pointer;
ProcPar:RTL_USER_PROCESS_PARAMETERS;
begin
Result:='';
try
h:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,PID);
if h = 0 then exit;
if NtQueryInformationProcess(h,ProcessBasicInformation,@PBI,SizeOf(PBI),@r) = 0 then
begin
if (ReadProcessMemory(h,pbi.PebBaseAddress,@aPEB,SizeOf(aPEB),r)) and (r = SizeOf(aPEB)) then
if (ReadProcessMemory(h,aPEB.ProcessParameters,@ProcPar,SizeOf(ProcPar),r)) and (r = SizeOf(ProcPar)) then
begin
if HasReadAccess(h,ProcPar.Environment,r) then showmessage(inttostr(r));
new(ws);
getmem(ws,r);
if ReadProcessMemory(h,ProcPar.Environment,ws,r,r) then
begin
result:=widestring(ws);
freemem(ws);
end else showmessage('n');
end;
end;
CloseHandle(h);
finally
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
s:=GetProcessPth(getcurrentprocessid);

showmessage(s);
end;

end.