title: Thread-Safe hooking function
body: <p>I am implementing little monitoring application, so i am hooking CreateWindowExA/W in process, so i can control the windows creation.
  The method i use to hook is to replace first 5 bytes from the call with assembler JMP instruction to my hook function. (Yes i know assembler, i used same method many times before).
  I use EnterCriticalSection at beginning of my hook code and i use InterlockedExchange to restore the stolen bytes aka replacing the JMP i wrote at beginning of CreateWindowExA/W with the real 5 bytes so i can call the function correctly. According to my experience everything has to be fine, but what happen is that at the moment when i just replaced the JMP with the real bytes, some other thread/s call the function, looks like the bytes are replaced for them too....</p>
  
  <p>I know i can use IAT/EAT tables hooking but i want to know whats wrong with my current method...
  Maybe the problem that InterlockedExchange is not working is that, CreateWindowExA/W is called from dll's(comctl32.dll,shell32.dll...) but not the main executable module.</p>
  
  <p>I hope someone help me, if you dont understand well my explanation please ask and i will re-explain.</p>