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_AC CESS, False, processId)
AllocateMemory = VirtualAllocEx(processHand le, 0, lngSize, MEM_COMMIT, PAGE_READWRITE)
CloseHandle processHandle
End Function
'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_AC
AllocateMemory = VirtualAllocEx(processHand
CloseHandle processHandle
End Function
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.
ASKER
For example.
memoryOffset := AllocateMemory($FF)
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks you all for helping. Two great examples.
ASKER
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