将参数传递给DLL

时间:2012-01-13 10:40:19

标签: delphi dll parameter-passing calling-convention

我正在实现一个全局鼠标钩子,并且在将参数传递给DLL时遇到困难。你能救我一下吗?

这是启动钩子的主机应用程序功能:

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;

这是StartHookProc

的DLL端功能
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;

我认为我想要做的是清楚 - 只需将LockDirection参数传递给dll,这样我就可以相应地处理鼠标消息。但是,当我传递0作为参数时,在dll端它显示'ě'(236)。我做错了什么?

由于

1 个答案:

答案 0 :(得分:2)

TStartHook声明不正确。它缺少调用约定。它应该是:

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