Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to modify a string in memory?

Posted on 2005-05-09
4
Medium Priority
?
184 Views
Last Modified: 2010-04-05
There are two strings with the same value "mystring" in target.exe and I want to modify them to "urstring" while target.exe is running. So I wrote a dll and injected it into target.exe so that I can modify the memory more conveniently.

Can anyone tell me how to modify the string in memory  (in the same process, maybe different thread)?
Thanks
0
Comment
Question by:klemperer
  • 3
4 Comments
 
LVL 34

Accepted Solution

by:
Slick812 earned 375 total points
ID: 13961659
if you have access to the memory address you can use the API CopyMemory or system string functions like lstrcpy, however you might be limited to the current memory byte allocation for the current array of charaters  , , "mystring" in this case, which has 8 bytes, and I can not tell if this is a null terminated array of charaters or not which will make a difference in some types of array of charter access. . .
Here is a button click event, it has 2 pointers, p1 and p2 which would be the memory addresses in your target.exe for the charater arrays (not sure about calling them strings), I have to get and free memory in this code, but you would not need that for memory in the target.exe




procedure TForm1.sbut_CopyMemoryClick(Sender: TObject);
var
pStr: PChar;
p1, p2: Pointer; // p1 and p1 would be your memory addresses in the other program
Str1: String;
begin
Str1 := 'Str1 here';
pStr := 'pStr here';

// you will not need to get the Memory for your's, but I need to assign memory here
GetMem(p1, 9);
GetMem(p2, 10);// has room for null #0

// I would probally use the CopyMemory for any memory access in another program
CopyMemory(p1, pStr, 9); // p1 has only 9 bytes of memory, no room for null #0
// you will need to consider the null #0 end for charater arrays in other programs

// you should be able to use the null term string function like StrLCopy
StrLCopy(p2,p1,9);// adds #0 Null to end, must have 10 bytes of memory
ShowMessage(PChar(p2)); // be sure there is a Null for a PChar read

CopyMemory(p2, @Str1[1], 10);
ShowMessage(PChar(p2));

FreeMem(p1); // you will not need to free you memory
FreeMem(p2);
end;


 - - - - - - - - - - - - - - - - - - - --

the memory allocation in another application is different that the GetMem and FreeMem in the injected Delphi DLL
0
 

Author Comment

by:klemperer
ID: 13965352
I wrote a function according to your suggestion, which change a string (or char array) "mystring" into "urstring" in the memory.

function ModifyDirection:boolean;
var
pStr: PChar;
p1, p2: Pointer;
Str1: String;
begin
  str1:='mystring';
  pStr:='urstring';
  CopyMemory(p1, pStr, 8);
  StrLCopy(p2,p1,8);
  ShowMsg(PChar(p2));

  CopyMemory(p2, @Str1[1], 9);
  ShowMsg(PChar(p2));
  result:=True;
end;

I called this function in dll entry point (DLL_PROCESS_ATTACH) and the application raised an error "Memory can't write". Then I call it again when the application is fully loaded; it raises no error but doesn't work. How can I know that the string has been changed or not?
0
 
LVL 34

Expert Comment

by:Slick812
ID: 13965411
you do not seem to understand much of what I tried to tell you, the variables I used for my example

p1, p2: Pointer;

do not exist, and when you use them in your  ModifyDirection function, you do NOT assign any meory location to them, they are UNDEFINED, useless

 I tried to tell you that you need to use the memory locations in the target.exe for whatever you want to change. . .

do not use my  p1  or p2, use the pointer location for the memory block in the target.exe program that has the text in it

I hope you do not say that you do not know the memory locations of the text you want to change, and ask how to get those mem locations
0
 
LVL 34

Expert Comment

by:Slick812
ID: 13965446
also

in my example I showed 2 ways to change a memory location, but you should not use both like you did in your function

function ModifyDirection: Boolean;
var
pStr: PChar;
pt: Pointer
begin
result := False;
  pStr:='GOstring';
  pt := GetMemLocation; // your function to get the memory location of the text in the target.exe
  CopyMemory(pt, pStr, 8);
if (PChar(pt)[0] = pStr[0]) and (PChar(pt)[7] = pStr[7]) then
  result := True;

end;
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question