title: Why do the hooking libraries work only occasionally for API Calls in Windows 10?
body: <p>I used multiple Hooking libraries e.g. Microsoft Detours Express, Mhook, etc. to hook NtWriteVirtualMemory API calls. I wrote following code to hook the API:</p>
  
  <pre><code>    #include &lt;string&gt;
      #include &lt;fstream&gt;
      #include &lt;process.h&gt;
      #include &lt;Windows.h&gt;
      #include &lt;detours.h&gt;
  
      #pragma comment(lib, "detours.lib")
  
      using namespace std;
  
      #define MAX_SIZE 10000
      char tempPath[MAX_SIZE];
  
      typedef LONG(NTAPI * oNtWriteVirtualMemory)(
              IN HANDLE               ProcessHandle,
              IN PVOID                BaseAddress,
              IN PVOID                Buffer,
              IN ULONG                NumberOfBytesToWrite,
              OUT PULONG              NumberOfBytesWritten OPTIONAL);
  
      oNtWriteVirtualMemory pNtWriteVirtualMemory = (oNtWriteVirtualMemory)GetProcAddress(GetModuleHandle(L"Ntdll.dll"), "NtWriteVirtualMemory");
  
      BOOL WINAPI MyNtWriteVirtualMemory(
              IN HANDLE               ProcessHandle,
              IN PVOID                BaseAddress,
              IN PVOID                Buffer,
              IN ULONG                NumberOfBytesToWrite,
              OUT PULONG              NumberOfBytesWritten OPTIONAL
          )
          {
             DWORD pidSource = GetCurrentProcessId();
             DWORD pidDestination = GetProcessId(ProcessHandle);
               if (pidSource != pidDestination)
               {
                   FILE * f1 = fopen(tempPath, "a+");
                      fprintf(f1, "inter-process write from %d to %d!\n", pidSource, pidDestination);
                      fclose(f1);
               }
  
            return pNtWriteVirtualMemory(ProcessHandle, BaseAddress, Buffer,umberOfBytesToWrite, NumberOfBytesWritten OPTIONAL);
          }
  
    BOOL APIENTRY DllMain(HMODULE hModule,
          DWORD  ul_reason_for_call,
          LPVOID lpReserved
          )
      {
          strcpy(tempPath, "C:\\Windows\\Temp\\log.txt");
          switch (ul_reason_for_call)
          {
          case DLL_PROCESS_ATTACH:
  
              try
              {
                  DisableThreadLibraryCalls(hModule);
  
                  DetourTransactionBegin();
                  DetourUpdateThread(GetCurrentThread());
                  DetourAttach(&amp;(PVOID&amp;)pNtWriteVirtualMemory, MyNtWriteVirtualMemory);
                  DetourTransactionCommit();
              }
              catch (int e)
              { }
              break;
          case DLL_THREAD_ATTACH:
              try
              {
                  DisableThreadLibraryCalls(hModule);
  
                  DetourTransactionBegin();
                  DetourUpdateThread(GetCurrentThread());
                  DetourAttach(&amp;(PVOID&amp;)pNtWriteVirtualMemory, MyNtWriteVirtualMemory);
                  DetourTransactionCommit();
              }
              catch (int e)
              {
              }
              break;
          case DLL_PROCESS_DETACH:
  
              try
              {
                  DetourTransactionBegin();
                  DetourUpdateThread(GetCurrentThread());
                  DetourDetach(&amp;(PVOID&amp;)pNtWriteVirtualMemory, MyNtWriteVirtualMemory);
                  DetourTransactionCommit();
              }
              catch (int e)
              {
              }
              break;
          case DLL_THREAD_DETACH:
              try
              {
                  DetourTransactionBegin();
                  DetourUpdateThread(GetCurrentThread());
                  DetourDetach(&amp;(PVOID&amp;)pNtWriteVirtualMemory, MyNtWriteVirtualMemory);
                  DetourTransactionCommit();
              }
              catch (int e)
              {
              }
              break;
          }
          return TRUE;
      }
  </code></pre>
  
  <p>However, it only works in some specific cases. For example, when the API Monitor program writes its monitoring DLL into the target process to monitor the process's behavior, my hooking code works well and hooks the NtWriteVritualMemory call of API Monitor program as usual. Also, the code hooks the API in Visual Studio very well. When the Visual Studio starts to build a project, it calls NtWriteVirtualMemory for some inter-process writes, and I can see the logs of these calls on my temporary log file!</p>
  
  <p>However, when I write my own code or build another one's code (e.g. <a href="https://github.com/stephenfewer/ReflectiveDLLInjection" rel="nofollow noreferrer">Reflective DLL Injection</a>) to call the NtWriteVirtualMemory API, the hook does not detect it at all. I use the Visual Studio 2015 to build these codes to either call the API or hook it. Moreover, I use <a href="https://www.apriorit.com/dev-blog/160-apihooks" rel="nofollow noreferrer">AppInit_DLLs Infrastructure</a> to make my hooking DLL a global user level hook.</p>
  
  <p>Any clue will be appreciated.</p>