gpt4 book ai didi

delphi - OOP 方法中的 WriteProcessMemory

转载 作者:行者123 更新时间:2023-12-03 15:36:35 27 4
gpt4 key购买 nike

在内存中写入,以便在运行时用另一个类 (TEdit) 替换一个类 (TMyEdit)

const
vmtNewInstance = System.vmtNewInstance;

var
AClassInstance: TClass;
OldInstance: Pointer;

function GetNewInstance: TObject;
begin
Result := AClassInstance.NewInstance;
end;

function GetVirtualMethod(AClass: TClass; const VmtOffset: Integer): Pointer;
begin
Result := PPointer(Integer(AClass) + VmtOffset)^;
end;

procedure SetVirtualMethod(AClass: TClass; const VmtOffset: Integer; const Method: Pointer);
var
WrittenBytes: DWORD;
PatchAddress: PPointer;
begin
PatchAddress := Pointer(Integer(AClass) + VmtOffset);
WriteProcessMemory(GetCurrentProcess, PatchAddress, @Method, SizeOf(Method), WrittenBytes);
end;


initialization
OldInstance := GetVirtualMethod(TMyEdit, vmtNewInstance);
AClassInstance := TMyEdit;
SetVirtualMethod(StdCtrls.TEdit, vmtNewInstance, @GetNewInstance);

finalization
SetVirtualMethod(StdCtrls.TEdit, vmtNewInstance, OldInstance);

这很好用,但是如何编写 OOP 方法呢?这是我的尝试:

type
TVirtualMethod = class
FInstance: Pointer;
FTarget: TClass;
public
constructor Create(const SourceClass, DestClass: TClass);
destructor Free;
end;

{ TVirtualMethod }

var
ASource: TClass;

function GetNewInstance: TObject;
begin
Result := ASource.NewInstance;
end;

constructor TVirtualMethod.Create(const SourceClass, DestClass: TClass);
var
WrittenBytes: DWORD;
PatchAddress: PPointer;
begin
ASource := SourceClass;
FInstance := PPointer(Integer(ASource) + vmtNewInstance)^;
FTarget := DestClass;
PatchAddress := Pointer(Integer(DestClass) + vmtNewInstance);
WriteProcessMemory(GetCurrentProcess, PatchAddress, @GetNewInstance, SizeOf(GetNewInstance), WrittenBytes);
end;

destructor TVirtualMethod.Free;
var
WrittenBytes: DWORD;
PatchAddress: PPointer;
begin
PatchAddress := Pointer(Integer(FTarget) + vmtNewInstance);
WriteProcessMemory(GetCurrentProcess, PatchAddress, @FInstance, SizeOf(FInstance), WrittenBytes);
end;

var
v1: TVirtualMethod;

initialization
v1 := TVirtualMethod.Create(TMyEdit, TEdit);

finalization
v1.Free;

它编译得很好,但它会在内存中写入。

最佳答案

TVirtualMethodInterceptor功能似乎与您的某些意图重叠,并以 OOP 方式实现。

关于delphi - OOP 方法中的 WriteProcessMemory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8366838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com