Aki Vai o code da unit inteira :
Para usar é o seguinte :
Créditos : Cm~
Ps : Nos Atachmments tem a unit inteira.
- Código:
unit UMemory;
interface
uses
sysutils,windows;
type
Tmemory = class(Tobject)
public
Function ReadByte(Address:integer):byte;Overload;
Function ReadByte(Address,OffSet:integer):byte;Overload;
Function ReadFloat(Address:integer):Single;Overload;
Function ReadFloat(Address,OffSet:integer):Single;Overload;
function ReadInteger(Address:integer):integer;Overload;
function ReadInteger(Address,OffSet:integer):integer;Overload;
Function ReadString(Address:integer):string;Overload;
Function ReadString(Address,OffSet:integer):string;Overload;
Function WriteByte(Address:integer;Value:byte):boolean;Overload;
Function WriteByte(Address,OffSet:integer;Value:byte):boolean;Overload;
Function WriteBytes(Address:Integer;Value,Bytes:byte):boolean;Overload;
Function WriteBytes(Address,OffSet:Integer;Value,Bytes:byte):boolean;Overload;
function WriteFloat(Address:integer;Value:Single):boolean;Overload;
function WriteFloat(Address,OffSet:integer;Value:Single):boolean;Overload;
Function WriteInteger(Address,Value:integer):boolean;Overload;
Function WriteInteger(Address,OffSet,Value:integer):boolean;Overload;
Function WriteString(Address:integer;Value:string):boolean;Overload;
Function WriteString(Address,OffSet:integer;Value:string):boolean;Overload;
end;
var
Memory:Tmemory;
implementation
{ Tmemory }
{Trate o error do geito que quiser nessa função,geralmente o error que acontece mais nessas
funções são Access Violation.}
procedure CreateException(Error:Exception);
begin
end;
//Função ReadByte,Lê um Address e retorna um valor do tipo Byte.
function Tmemory.ReadByte(Address: integer): byte;
begin
result := 0;
try
Result := Pbyte(Address)^;
except
on E : Exception do
begin
CreateException(e);
end;
end;
end;
//Função ReadByte,Agora com pointers.
function Tmemory.ReadByte(Address, OffSet: integer): byte;
begin
result := 0;
try
Result := Pbyte(PInteger(Address)^ + OffSet)^;
except
on E : Exception do
begin
CreateException(e);
end;
end;
end;
//Função ReadFloat,Lê um Address e retorna um valor do tipo Single.
function Tmemory.ReadFloat(Address: integer): Single;
begin
result := 0.0;
try
result := PSingle(Address)^;
except
on E : Exception do
begin
CreateException(e);
end;
end;
end;
//Função ReadFloat,Agora com Pointer.
function Tmemory.ReadFloat(Address, OffSet: integer): Single;
begin
result := 0.0;
try
result := PSingle(PInteger(Address)^ + OffSet)^;
except
on E : Exception do
begin
CreateException(e);
end;
end;
end;
//Função ReadInteger,Lê um address e retorna um valor do tipo integer.
function Tmemory.ReadInteger(Address: integer): integer;
begin
result := 0;
try
result := Pinteger(Address)^;
except
on E : Exception do
begin
CreateException(e);
end;
end;
end;
//Função ReadInteger,Agora com Pointers.
function Tmemory.ReadInteger(Address, OffSet: integer): integer;
begin
result := 0;
try
result := Pinteger(PInteger(Address)^ + OffSet)^;
except
on E : Exception do
begin
CreateException(e);
end;
end;
end;
//Função ReadString,Lê um address e retorna um valor do tipo String.
function Tmemory.ReadString(Address: integer): string;
var
i, Value : Byte;
begin
Result := '';
try
i:= 0;
repeat
Value := pbyte(Address + i)^;
Result:= Result + char(Value);
inc(i);
until Value = 0;
Result:=Copy(Result,1,Length(Result)-1);
except
on e : Exception do
begin
CreateException(e);
end;
end;
end;
//Função ReadString,Agora com Pointers.
function Tmemory.ReadString(Address, OffSet: integer): string;
var
i,PAddress, Value : Byte;
begin
Result := '';
try
PAddress := Pinteger(Address)^ + OffSet;
i:= 0;
repeat
Value := pbyte(PAddress + i)^;
Result:= Result + char(Value);
inc(i);
until Value = 0;
Result:=Copy(Result,1,Length(Result)-1);
except
on e : Exception do
begin
CreateException(e);
end;
end;
end;
//Procedure WriteByte,Escreve um valor do tipo Byte.
Function Tmemory.WriteByte(Address: integer; Value: byte):boolean;
begin
Result := true;
try
pbyte(Address)^ := value;
except
on E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteByte,Agora com Pointers.
Function Tmemory.WriteByte(Address, OffSet: integer; Value: byte):boolean;
begin
Result := true;
try
pbyte(Pinteger(Address)^ + OffSet)^ := value;
except
on E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteBytes,Escreve varios bytes de uma vez só.
Function Tmemory.WriteBytes(Address: Integer; Value, Bytes: byte):boolean;
var
I:integer;
begin
Result := true;
try
for i := 0 to bytes - 1 do
begin
Pbyte(Address + i)^ := value;
end;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteBytes,Agora com Pointers.
Function Tmemory.WriteBytes(Address, OffSet: Integer; Value, Bytes: byte):boolean;
var
I,Paddress:integer;
begin
Result := true;
try
Paddress := PInteger(Address)^ + OffSet;
for i := 0 to bytes - 1 do
begin
Pbyte(PAddress + i)^ := value;
end;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteFloat,Escreve um valor do tipo Single.
function Tmemory.WriteFloat(Address: integer; Value: Single): boolean;
begin
Result := true;
try
PSingle(Address)^ := Value;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteFloat,Agora com Pointers.
function Tmemory.WriteFloat(Address, OffSet: integer;
Value: Single): boolean;
begin
Result := true;
try
PSingle(Pinteger(Address)^ + OffSet)^ := Value;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteInteger,Escreve um valor do tipo Integer.
Function Tmemory.WriteInteger(Address,Value: integer):boolean;
begin
Result := true;
try
Pinteger(Address)^ := Value;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteInteger,Agora com Pointers.
Function Tmemory.WriteInteger(Address, OffSet, Value: integer):boolean;
begin
Result := true;
try
Pinteger(Pinteger(Address)^ + OffSet)^ := Value;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteString,Escreve um valor do tipo String.
Function Tmemory.WriteString(Address: integer; Value: string):boolean;
var
TVal : Byte;
i: integer;
begin
Result := true;
try
for i:=1 to Length(Value) do
begin
TVal:=ord(Value[i]);
pbyte(Address + i - 1)^ := Tval;
if i = Length(Value) then
begin
TVal:=0;
pbyte(Address + i)^ := Tval;
end;
end;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
//Procedure WriteString,Agora com Pointers.
Function Tmemory.WriteString(Address, OffSet: integer; Value: string):boolean;
var
TVal : Byte;
i,Paddress: integer;
begin
Result := true;
try
Paddress := Pinteger(Address)^ + OffSet;
for i:=1 to Length(Value) do
begin
TVal:=ord(Value[i]);
pbyte(PAddress + i - 1)^ := Tval;
if i = Length(Value) then
begin
TVal:=0;
pbyte(PAddress + i)^ := Tval;
end;
end;
except
On E : Exception do
begin
CreateException(e);
Result := False;
end;
end;
end;
initialization
{initialization code goes here}
Memory := Tmemory.Create;
finalization
{finalization code goes here}
FreeAndNil(Memory);
end.
Para usar é o seguinte :
- Código:
//Por exemplo,você quer escrever um valor integer(4 Bytes) na memoria,usando Pointers :
Memory.WriteInteger(AddressPointer,OffSet,Value);
//Ou,sem Pointer
Memory.WriteInteger(Address,Value);
//Todas as funções Write(De escrever) retornam um valor Boolean(True,False) que se for verdadeiro a operação deu certou e se for Falso deu errado.
Créditos : Cm~
Ps : Nos Atachmments tem a unit inteira.