title: Injecting C++ DLL
body: <p>I know there are various questions and books on this but I can't seem to get my C++ DLL injected into any processes.</p>
  
  <p>The code to inject the DLL:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "windows.h"
  
  bool Inject(DWORD pId, char *dllName);
  
  using namespace std;
  
  int main()
  {
      Inject(600, "C:\\d.dll");
      return 0;
  }
  
  bool Inject(DWORD pId, char *dllName)
  {
      HANDLE h = OpenProcess(PROCESS_ALL_ACCESS, false, pId);
      if(h)
      {
          LPVOID LoadLibAddr = (LPVOID)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
          LPVOID dereercomp = VirtualAllocEx(h, NULL, strlen(dllName), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
          WriteProcessMemory(h, dereercomp, dllName, strlen(dllName), NULL);
          HANDLE asdc = CreateRemoteThread(h, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddr, dereercomp, 0, NULL);
          WaitForSingleObject(asdc, INFINITE);
          VirtualFreeEx(h, dereercomp, strlen(dllName), MEM_RELEASE);
          CloseHandle(asdc);
          CloseHandle(h);
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>and the DLL I am trying to inject:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  
  BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                         DWORD reason        /* Reason this function is being called. */ ,
                         LPVOID reserved     /* Not used. */ )
  {
  switch (reason)
      {
        case DLL_PROCESS_ATTACH:
             MessageBox (0, "From DLL\n", "Process Attach", MB_ICONINFORMATION);
          break;
  
        case DLL_PROCESS_DETACH:
             MessageBox (0, "From DLL\n", "Process Detach", MB_ICONINFORMATION);
          break;
  
        case DLL_THREAD_ATTACH:
             MessageBox (0, "From DLL\n", "Thread Attach", MB_ICONINFORMATION);
          break;
  
        case DLL_THREAD_DETACH:
             MessageBox (0, "From DLL\n", "Thread Detach", MB_ICONINFORMATION);
          break;
      }
  
      return TRUE;
  }
  </code></pre>
  
  <p>I don't know enough C++ to know where this is going wrong. I have run Process Explorer on the process I am trying to inject to (process run as admin aswell) but it isn't being injected. When I run it, nothing happens, any ideas?</p>