vb to delphi allocatememory

I've never used virtualallocex before so I'm understanding how this should work.  Can somebody help translate this from vb?

'Allocates Memory, useful for code caves.
Public Function AllocateMemory(lngSize As Long) As Long
Dim hwnd, processHandle, processId As Long

    hwnd = FindWindow(vbNullString, CurrentProcess)
    If (hwnd = 0) Then Exit Function
   
    GetWindowThreadProcessId hwnd, processId
    processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, processId)
    AllocateMemory = VirtualAllocEx(processHandle, 0, lngSize, MEM_COMMIT, PAGE_READWRITE)
    CloseHandle processHandle
End Function
Grant FullenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MerijnBSr. Software EngineerCommented:

function AllocateMemory(lngSize: longint): longint;
var hwnd, processHandle, processID: longint;
begin
 hwnd := FindWindow(nil, CurrentProcess);
 if (hwnd = 0) then
  exit;
 
 GetWindowThreadProcessID(hwnd, processID);
 ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, false, processID);
 result := VirtualAllocEx(processhandle, 0, lngSize, MEM_COMMIT, PAGE_READWRITE);
 CloseHandle(processHandle);
end;

Open in new window

0
Grant FullenAuthor Commented:
Doesn't work.  I was looking and I'm sure the var types have to change.  All I want is to be able to allocate memory and return the address offset that it allocated.
0
Grant FullenAuthor Commented:
For example.

memoryOffset := AllocateMemory($FF)
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Dagan HooverDeveloperCommented:

function AllocateMemory(lngSize: Cardinal): Cardinal;
var
  Wnd,
  ProcessHandle,
  ProcessID : Integer;
begin
  Wnd := FindWindow(nil,CurrentProcess);
  GetWindowThreadProcessID(Wnd,ProcessID);
  //Just skip right to the ProcessID if it's for the 
  //calling process. Uncomment the below if needed.
  //ProcessID := GetCurrentProcessID;
  ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS,False,ProcessID);
  If ProcessHandle > 0 Then
    begin
    Result := VirtualAllocEX(ProcessHandle,0,lngSize,MEM_COMMIT,PAGE_READWRITE);
  end;
  CloseHandle(ProcessHandle);
end;

Open in new window

0
Dagan HooverDeveloperCommented:
Just tested to be sure, changed a type def and it works fine.
function AllocateMemory(lngSize: Cardinal): Cardinal;
var
  //Wnd,
  ProcessHandle,
  ProcessID : Integer;
begin
  //Wnd := FindWindow(nil,CurrentProcess);
  //GetWindowThreadProcessID(Wnd,ProcessID);
  //Just skip right to the ProcessID if it's for the 
  //calling process. Uncomment the below if needed.
  ProcessID := GetCurrentProcessID;
  ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS,False,ProcessID);
  If ProcessHandle > 0 Then
    begin
    Result := Cardinal(VirtualAllocEX(ProcessHandle,0,lngSize,MEM_COMMIT,PAGE_READWRITE));
  end;
  CloseHandle(ProcessHandle);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(AllocateMemory($FF)));
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Russell LibbySoftware Engineer, Advisory Commented:
You may want to download the ProcessMemory source from my site, as this is exactly what it was written to do.

http://users.adelphia.net/~rllibby/downloads/processmemory.zip

I only mention this because you are most likely going to need to read / write the memory after allocated (why else would you be allocating it?). The class I wrote greatly simplifies this for you. example below.

Regards,
Russell

---

var  objMemMgr:     TProcessMemory;
     dwIndex:       Integer;
     lpszTest:      Array [0..254] of Char;
begin

  // Create memory allocator for external process
  objMemMgr:=TProcessMemory.CreateFromHwnd(FindWindow(nil, CurrentProcess));

  // Resource protection
  try
     // Add a memory block in the process (255 bytes)
     dwIndex:=objMemMgr.Add($FF);

     // Set string to write
     StrPCopy(lpszTest, 'Hello world');

     // Write the string (plus null) to other process
     objMemMgr[dwIndex].Write(lpszTest, Succ(StrLen(lpszTest)));

     // Clear the memory block
     FillChar(lpszTest, SizeOf(lpszTest), 0);

     // Read back memory from other process
     objMemMgr[dwIndex].Read(lpszTest, SizeOf(lpszTest));

     // Copy on read is also supported through the access of the LocalMemory property
     MessageBox(0, PChar(objMemMgr[dwIndex].LocalMemory), nil, MB_OK);

  finally
     // Free memory allocator
     objMemMgr.Free;
  end;

end;

0
Grant FullenAuthor Commented:
Thanks you all for helping.  Two great examples.
0
Grant FullenAuthor Commented:
ThievingSix, what about if I wanted to pick another process?
0
Dagan HooverDeveloperCommented:

function AllocateMemory(lngSize: Cardinal): Cardinal;
var
  //Wnd,
  ProcessHandle,
  ProcessID : Integer;
begin
  Wnd := FindWindow(nil,<<INPUT WINDOW TITLE HERE>>);
  GetWindowThreadProcessID(Wnd,ProcessID);
  //Just skip right to the ProcessID if it's for the 
  //calling process. Uncomment the below if needed.
  //ProcessID := GetCurrentProcessID;
  ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS,False,ProcessID);
  If ProcessHandle > 0 Then
    begin
    Result := Cardinal(VirtualAllocEX(ProcessHandle,0,lngSize,MEM_COMMIT,PAGE_READWRITE));
  end;
  CloseHandle(ProcessHandle);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(AllocateMemory($FF)));
end;

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.