Forum > Operating Systems

WriteProcessMemory not working for me...

(1/2) > >>

CodeSculptor:
My WRITEPROCESSMEMORY call is consistently not writing anything, and the Bytes Written returned is consistently ZERO..

Here's the code I am using to prepare for the calls :

Get the Process ID as :
      Main_Process_ID : Cardinal;
      Main_Process_ID := Get_ProcessID( Trim( UpperCase( lbProcesses.GetSelectedText )));

Set up the read/write area as :

VAR Heap_Byte_Address : Pointer;
       tmpByte_Array     : ARRAY [ 00 .. 128 ] OF BYTE;

Heap byte address was fetched from corresponding ModuleInfo.lpBaseOfDll...
So the READS work great.

This works ok. (even when I read the entire heap... but only trying two bytes here )

PROCEDURE intReadBuffer_From_Memory;
    VAR HowManyBytesRead : QWord;
        Process_Handle   : HANDLE;
       
    BEGIN
         Process_Handle := OpenProcess( PROCESS_ALL_ACCESS, False, Main_Process_ID );

         ReadProcessMemory( Process_Handle,
                            Pointer( Heap_Byte_Address ),
                            @tmpByte_Array,
                            2,
                            HowManyBytesRead );
         
         CloseHandle( Process_Handle );
    END;
   

Here is how I prepare the assigning for Write (two bytes only)...
       tmpByte_Array[ 0 ] := 65;
       tmpByte_Array[ 1 ] := 65;
       WB_Size                 := 2;

Then I call with  WriteBufferToMemory( Heap_Byte_Address );     (below)...


Here's the call that fails :

    PROCEDURE WriteBufferToMemory( inAddress : Pointer );
    VAR HowManyBytesWritten : QWord;
        Process_Handle      : HANDLE;
       
    BEGIN                                 
         HowManyBytesWritten := 0;
         Process_Handle := OpenProcess( PROCESS_ALL_ACCESS, False, Main_Process_ID );

         WriteProcessMemory( Process_Handle, 
                             Pointer( Heap_Byte_Address ),
                             @tmpByte_Array,
                             2, 
                             HowManyBytesWritten );
         
         CloseHandle( Process_Handle );
    END;

It's the WriteProcessMemory that fails.  Keeps showing 0 bytes written, and returns the original bytes again when I call the read again.

Any ideas?

Thaddy:
So you have an array of 129 bytes? Are you sure that is what you mean? Shouldn't it be [0..127]?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- tmpByte_Array     : packed array [ 0 .. 127 ] of byte;// packed because of alignment, 128 bytes longI suspect, but did not test, that your code is over-indexing. In {$R+} state the compiler will warn you about that.

Martin_fr:
You didn't even tell what LastError reports....

440bx:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Process_Handle := OpenProcess( PROCESS_ALL_ACCESS, False, Main_Process_ID ); The first thing to do is to check that OpenProcess succeeded, _particularly_ when you're asking for PROCESS_ALL_ACCESS.

Start there and, as Martin_fr suggested, when something fails, get the last error from the O/S, otherwise you're programming blind.

ETA:

formatting suggestion, put your code between tags [ code = pascal ] <your code> [ /code ]  (without the spaces after/before the brackets)

CodeSculptor:
I'd totally forgotten about Last Error..
it returns 998 .

Seems I MIGHT need : VirtualProtectEx'ing

Navigation

[0] Message Index

[#] Next page

Go to full version