Search code examples
delphidllparameter-passingcalling-convention

Passing parameters to DLL


I am implementing a global mouse hook and got stuck at passing parameters to the DLL. Could you help me out?

This is the host application function to start the hook:

function TMainForm.StartHook(LockDirection: byte): boolean;
type
  TStartHook = function(AppHandle: HWND; LockDir: byte): Byte;
var
  StartHookProc: TStartHook;
  SHresult: Byte;
  i:integer;
  LibPath: string;
begin
  LibPath:=ExtractFilePath(Application.ExeName)+'hooklib.dll';
  HookInfo.LibHandle := LoadLibrary(pwidechar(LibPath));

  @StartHookProc := GetProcAddress(HookInfo.LibHandle, 'StartHookProc');
  if @StartHookProc = nil then begin
    Showmessage('Exit #1');
    Exit;
  end;

  SHresult := StartHookProc(Handle, LockDirection);
end;

And this is the DLL-side function of StartHookProc

function StartHookProc(AppHandle: HWND; LockDir: byte): Byte; stdcall;
begin
  Result := 0;
  if Hooked then
  begin
    Result := 1;
    Exit;
  end;

  HookHandle:= SetWindowsHookEx(WH_MOUSE, @MouseHookProc, hInstance, 0);
...
end;

I think that what I am trying to do is clear - simply pass the LockDirection param to the dll so I can process the mouse messages accordingly. However, when I pass 0 as the param, on the dll-side it shows 'ě'(236). What am I doing wrong?

Thanks


Solution

  • TStartHook is declared incorrectly. It is missing the calling convention. It should be:

    type
      TStartHook = function(AppHandle: HWND; LockDir: byte): Byte; stdcall;