user1023395
user1023395

Reputation: 263

how to get HInstance using BTMemoryModule

I am trying to load a dll from resource using BTMemoryModule.pas unit but I'm getting this error The specified module could not be loaded. These are the procedures in my dll which I'm calling from exe using BTMemoryModule:

procedure StartHook; stdcall;
begin
  if MessageHook=0 then
  begin
    MessageHook := SetWindowsHookEx(WH_GetMessage, 
                                    @GetMsgProc, 
                                    HInstance, 
                                    0);
    if MessageHook = 0 then 
      ShowMessage(SysErrorMessage(GetLastError));
  end;
end;

function GetMsgProc(Code: Integer; 
  wParam, lParam: Longint): Longint; stdcall ;
begin
  Result := CallNextHookEx(MessageHook, Code, wParam, lParam);
end;

Upvotes: 1

Views: 1296

Answers (2)

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43043

From the MSDN documentation, there are two ways of creating the hook:

  • Either by using the DLL module handle;
  • Either by using a Thread ID.

hMod [in] Type: HINSTANCE A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.

dwThreadId [in] Type: DWORD The identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.

Since BTMemoryModule.pas does not have a regular DLL module, you may try to use the thread ID parameter:

MessageHook := SetWindowsHookEx(WH_GetMessage, 
                                @GetMsgProc, 
                                0, 
                                GetThreadId);

Or even trying to let both last parameters be equal to 0.

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 597196

System-wide hooks MUST use actual DLL files from disk, as they have to be loaded and mapped into the address space of each running process that is hooked. In other words, each process will do an implicit LoadLibrary(), so it needs a filename of a real DLL to load from. You CANNOT use a resource-based DLL for such hooks.

Upvotes: 5

Related Questions