1

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
user1023395
  • 263
  • 3
  • 4
  • 13
  • Did you try our code without BTMemoryModule? That is, does it work with an external dll with calling LoadLibrary? – Arnaud Bouchez Nov 12 '11 at 09:29
  • It's hard to understand why you would be grappling with this. Why not save the DLL to disk. I'm finding it hard to imagine a non-malicious use motivation for this Q. – David Heffernan Nov 12 '11 at 13:57

2 Answers2

5

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • then how it works i mean why I'm getting error then how to get it work properly ? – user1023395 Nov 12 '11 at 09:33
  • But AFAIK, the DLL *is* loaded and mapped into the address space of the running process, so you may be able to create the hook. Even if the HInstance parameter is not a true DLL handle in this case. You may try using the dwThreadId parameter. – Arnaud Bouchez Nov 12 '11 at 09:54
  • Using the `dwThreadId` parameter creates a thread-specific hook. To hook multiple processes that way requires calling `SetWindowsHookEx()` multiple times, which is a waste of resources, and the `hInstance` parameter must point to a real DLL if the thread is in another process. Using a resource-based DLL only makes sense for hooks that are local to the calling process only. However, resource blocks are usually not marked for executing code. You might have to use `VirtualAlloc()` to get around that. – Remy Lebeau Nov 12 '11 at 16:35
  • so it hard to get dll work properly from resource. what if i use dll as exe what about hInstance as my exe file ? hINSTANCE ONLY get dll ? – user1023395 Nov 12 '11 at 16:38
  • Stick with using a DLL, that is what the API is expecting. Don't try to use your EXE directly. – Remy Lebeau Nov 13 '11 at 07:47
0

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.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • nope not working i try ed to set both parameter 0 and GetThreadID need thread to b created and its not XP compatible :) without BTMemoryModule.pas calling external dll works fine – user1023395 Nov 12 '11 at 11:51