Link to home
Start Free TrialLog in
Avatar of Grant Fullen
Grant Fullen

asked on

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
Avatar of MerijnB
MerijnB
Flag of Netherlands image


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

Avatar of Grant Fullen
Grant Fullen

ASKER

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.
For example.

memoryOffset := AllocateMemory($FF)

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

ASKER CERTIFIED SOLUTION
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks you all for helping.  Two great examples.
ThievingSix, what about if I wanted to pick another process?

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