1

I need to do it this way because I am in DllMain() therefore, loader lock is held. I've read that GetModuleHandle() also uses the loader lock [page #6] which would result in deadlock.

How could GetModuleHandle() implemented? Some code would be a plus.

Update: Since I am using SetWindowsHookEx on WinXP only. Just going to take advice in the comments, go the easy way, and use GetModuleHandle() the first time the callback gets called.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
unixman83
  • 9,421
  • 10
  • 68
  • 102
  • Can't you just export an `Init` function that will do all the "dangerous" stuff, and call it from the outside after `DllMain` was completed? You're looking for trouble if you're doing complicated things in `DllMain`. – Eran Sep 14 '11 at 13:52
  • @eran, no not with my mode of API injection. It is legacy 'API hooking' code, to support WinXP. – unixman83 Sep 14 '11 at 14:08
  • Try it the easy way before trying it the hard way. Just add a few lines of code to the target program to have it load your DLL if a special debugging switch is passed, say. – Raymond Chen Sep 14 '11 at 14:18
  • Windows XP SP2 adds ASLR, otherwise I could read 0x7ffdf000 which used to be the PEB, and walk that to get the loaded module's base address. – unixman83 Sep 14 '11 at 14:33

1 Answers1

0

You can call GetModuleHandle from DllMain. It doesn't load any libraries and doesn't increment module reference count. Other story is with LoadLibrary. Never call it from DllMain.

Sergey Podobry
  • 7,101
  • 1
  • 41
  • 51