title: Ejecting after injecting DLL from running process
body: <p>I wrote this function to inject DLL into running process:</p>
  
  <pre><code>DLL_Results CDLL_Loader::InjectDll()
  {
      DWORD ThreadTeminationStatus;
      LPVOID VirtualMem;
      HANDLE hProcess, hRemoteThread;
      HMODULE hModule;
  
      if (!isInit())
          return NOT_INIT;
  
      if (isInjected())
          return DLL_ALREADY_HOOKED;
  
      hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
      if (hProcess == NULL)
          return PROCESS_ERROR_OPEN;
  
      VirtualMem = VirtualAllocEx (hProcess, NULL, strlen(DllFilePath), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
      if (VirtualMem == NULL)
          return PROCESS_ERRORR_VALLOC;
  
      if (WriteProcessMemory(hProcess, (LPVOID)VirtualMem, DllFilePath, strlen(DllFilePath), NULL) == 0)
      {
          VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE|MEM_COMMIT);
          CloseHandle(hProcess); 
          return PROCESS_ERROR_WRITE;
      }
  
      hModule = GetModuleHandle(L"kernel32.dll");
      hRemoteThread = CreateRemoteThread(hProcess, NULL, 0, 
                          (LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "LoadLibraryA"),
                            (LPVOID)VirtualMem, 0, NULL);
  
      if (hRemoteThread == NULL)
      {
          FreeLibrary(hModule);
          VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE | MEM_COMMIT);
          CloseHandle(hProcess); 
          return PROCESS_ERROR_CREATE_RTHREAD;
      }
  
      WaitForSingleObject(hRemoteThread, INFINITE);
      GetExitCodeThread(hRemoteThread, &amp;ThreadTeminationStatus);
      FreeLibrary(hModule);
  
      VirtualFreeEx(hProcess, NULL, (size_t)strlen(DllFilePath), MEM_RESERVE | MEM_COMMIT);
      CloseHandle(hRemoteThread);
      CloseHandle(hProcess); 
      injected = true;
      return DLLHOOK_OK;
  }
  </code></pre>
  
  <p>And It works great, but when i was trying to eject the dll i was unable to find information about unhooking.. i was trying to build some function to do it and i think i'm close
  this is what i've got so far:</p>
  
  <p>is that the right way? if so what parameter should i pass in createRemoteThread instade of VirtualMem (That was used in the injecting function)...</p>
  
  <pre><code>DLL_Results CDLL_Loader::EjectDll()
  {
      DWORD ThreadTeminationStatus;
      HANDLE hProcess, hRemoteThread;
      HMODULE hModule;
  
      if (isInjected())
          return DLLEJECT_OK;
  
      hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
      if (hProcess == NULL)
          return PROCESS_ERROR_OPEN;
  
      hModule = GetModuleHandle(L"kernel32.dll");
      hRemoteThread = CreateRemoteThread(hProcess, NULL, 0, 
                          (LPTHREAD_START_ROUTINE)GetProcAddress(hModule, "FreeLibrary"),
                            /*(LPVOID)VirtualMem &lt;- What do i need to send here?*/, 0, NULL);
  
      if (hRemoteThread != NULL)
      {
          WaitForSingleObject(hRemoteThread, INFINITE);
          GetExitCodeThread(hRemoteThread, &amp;ThreadTeminationStatus);
      }
  
      CloseHandle(hRemoteThread);
      CloseHandle(hProcess); 
      injected = false;
      return DLLEJECT_OK;
  }
  </code></pre>