Using ReadProcess function of kernel32.dll at pasc

2019-06-03 20:27发布

问题:

I override the function library kernel32.dll in Pascal and OpenProcess function returns 0. Function GetLastError() returns 87 error, that means

ERROR_INVALID_PARAMETER 87 (0x57) The parameter is incorrect.

What could be wrong?

Program UODll;
const search_window = 'Ultima Online - xxx (The Abyss)';
function FindWindow(C1, C2: PChar): Longint; external 'FindWindowA@user32.dll stdcall';
function GetWindowThreadProcessId(hWnd: Longint; opt: Word): Word; external 'GetWindowThreadProcessId@user32.dll stdcall';
function OpenProcess(dwDesiredAccess: Word; inherit: Byte; pid: Word): Longint; external 'OpenProcess@kernel32.dll stdcall';
function GetProcessId(proc: Longint): Word; external 'GetProcessId@kernel32.dll stdcall';
function GetLastError(): Word; external 'GetLastError@kernel32.dll stdcall';
var
res, err: Word;
wnd, proc: Longint;
Begin
wnd := Findwindow('', search_window);
if (wnd > 0) then
begin
res := GetWindowThreadProcessId(wnd, res);
proc := OpenProcess($0400,0,res);
err := GetLastError();
writeln(IntToStr(proc));
writeln(IntToStr(err));
end;
End.

Im trying to use LongWord and Cardinal, but i have the same error.. Who can help me?) P.S. its not delphi.. i dont know what is this :) Programm calls UOScript

回答1:

OpenProcess has declaration

HANDLE WINAPI OpenProcess(
  _In_  DWORD dwDesiredAccess,
  _In_  BOOL bInheritHandle,
  _In_  DWORD dwProcessId
);

dwDesiredAccess and pid are double words that are

typedef unsigned long       DWORD;

i.e. 32bit on x86, according to this answer.

But Delphi/Pascal Word type is 16bit.

Also, BOOL is defined as

typedef int BOOL;

So, you should use Integer for inherit instead of Byte

So, your function declaration is incorrect.

Seems you should use Cardinal or LongWord instead of Word in your declarations.

If you use Delphi, you can import Windows module that has all Win API functions defined.



标签: dll pascal