CodeExec(
hProcess: THandle; //ターゲットプロセスのハンドル
Code: string //「Address-Value」形式のチートコード
);
procedure CodeExec(hProcess: THandle; Code: string);
const
SNotFoundHyphenCode = 'コードからハイフンが見つかりません';
SNotHexAddress = 'アドレス ''%s'' は16進数ではありません';
SNotHexValue = '値 ''%s'' は16進数ではありません';
SDigitIsOddValue = '値 ''%s'' は偶数桁ではありません';
STooLongDigitAddress = 'アドレス ''%s'' は8桁以内ではありません';
SEmptyAddress = 'アドレスが空です';
SEmptyValue = '値が空です';
SBadAccessAddress = 'アドレス ''%p'' には書き込めません';
SInvalidAddress = 'アドレス ''%p'' のアクセス保護の状態を変更できません';
var
HyphenPos, ValueLength, E: Integer;
_Addr, _Value: string;
Addr: Pointer;
Value: array of Byte;
Dummy: Cardinal;
begin
//ハイフン検索
HyphenPos := AnsiPos('-', Code);
if HyphenPos = 0 then
raise Exception.Create(SNotFoundHyphenCode);
//アドレス処理
//16進数8桁以内
if HyphenPos = 1 then
raise Exception.Create(SEmptyAddress);
_Addr := Copy(Code, 0, HyphenPos - 1);
if Length(_Addr) > 8 then
raise Exception.CreateFmt(STooLongDigitAddress, [_Addr]);
Val('$'+_Addr, DWORD(Addr), E);
if E <> 0 then
raise Exception.CreateFmt(SNotHexAddress, [_Addr]);
//値処理
if HyphenPos = Length(Code) then
raise Exception.Create(SEmptyValue);
_Value := Copy(Code, HyphenPos + 1, Length(Code));
if Odd(Length(_Value)) then
raise Exception.CreateFmt(SDigitIsOddValue, [_Value]);
SetLength(Value, Length(_Value) div 2);
ValueLength := Length(Value);
if HexToBin(PChar(_Value), PChar(Value), ValueLength) <> ValueLength then
raise Exception.CreateFmt(SNotHexValue, [_Value]);
//メモリ書き込み処理
if not VirtualProtectEx(hProcess, Addr, ValueLength, PAGE_EXECUTE_READWRITE, Dummy) then
raise Exception.CreateFmt(SInvalidAddress, [Addr]);
if not WriteProcessMemory(hProcess, Addr, @Value[0], ValueLength, Dummy) then
raise Exception.CreateFmt(SBadAccessAddress, [Addr]);
end;
名無しさん多謝
次は複数行文字列の処理とか書きたい。
でも改行コードって\r\n、\r、\n(Delphiでは#10#13、#10、#13)の3種類があるから、そこら辺も考えないとナ。


