简体   繁体   中英

What's Delphi byte ASM representation of a empty string?

Considering the following byte arrays:

const
  //Values : Array[0..4] of byte = ($C2,$00,$00,$90,$90);
  Values: Array[0..0] of Byte = (???); // '' ?

How can initialize the second byte array (similar to first byte array) with a asm representation, but this time, of a empty string?


Edition:

As it is unclear for what purpose you ask and what you are going to do with that array. – Tom Brunberg

The goal is write a empty string to a address of target process:

procedure WriteBytes(hProcess: THandle; Address: pointer; const Buffer: array of byte);
var
  Read: THandle;
  oldprot, tmp: dword;
begin
  if (VirtualProtectEx(hProcess, Address, Length(Buffer), PAGE_EXECUTE_READWRITE, @oldprot)) then
    Writeln('1 - VirtualProtectEx() successfully!');
  
  if (WriteProcessMemory(hProcess, Address, @Buffer, Length(Buffer), Read)) then
    Writeln('2 - WriteProcessMemory() successfully!');

  if (VirtualProtectEx(hProcess, Address, Length(Buffer), oldprot, @tmp)) then
    Writeln('3 - VirtualProtectEx() successfully!');
end;

In C++ the following example works fine:

PVOID hmod = debug_event.u.LoadDll.lpBaseOfDll; 
ULONG op; 
SIZE_T NumberOfBytesWritten; 

if (VirtualProtectEx(pi.hProcess, hmod, 1, PAGE_READWRITE, &op)) 
{ 
 WriteProcessMemory(pi.hProcess, hmod, "", 1, &NumberOfBytesWritten); 
}

How this could be in Delphi?

The declaration of WriteProcessMemory is:

function WriteProcessMemory(hProcess: THandle; const lpBaseAddress: Pointer; 
  lpBuffer: Pointer; nSize: SIZE_T; var lpNumberOfBytesWritten: SIZE_T): BOOL; stdcall;

The C code you show:

WriteProcessMemory(pi.hProcess, hmod, "", 1, &NumberOfBytesWritten);

Use an empty string which in C is passed to a function as a pointer to the memory containing the array of string characters ended by a nul character. So in the code you show (empty string), the pointer point to a nul byte.

You said:

The goal is write a empty string to a address of target process

You have almost the correct declaration with the difference that your array of byte, to be equivalent of the C code has to be nul terminated and if you are interested in the equivalent of C nul string, you can use:

const
  Values: Array[0..0] of Byte = (0);

If you want to pass a non nul string, pay attention to Unicode (16 bit characters) the Delphi uses unless you use an AnsiString (8 bits characters). If you take the address of a string using a cast, you get a pointer to a nul terminated string. The nul is either 8 or 16 bits depending on string type. Bu in case of a nul string, the pointer is nil.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM