title: dll injection - simple main
body: <p>I'm tring to inject a dll to an .exe, i code this simple main but my file isn't created.
  I inject it with a cpp code, but i don't think the injector is the problem.</p>
  
  <pre><code>DWORD WINAPI Main_thread( LPVOID lpParam)
  {
    std::ofstream myfile;
    myfile.open ("C:\\Users\\root\\Desktop\\example.txt");
    myfile &lt;&lt; "success" &lt;&lt; std::endl;
    myfile.close();
    return S_OK;
  }
  
  BOOL APIENTRY DllMain(HMODULE hModule, DWORD  _reason, LPVOID lpReserved)
  {
    if (_reason == DLL_PROCESS_ATTACH)
       CreateThread(0, 0x1000, &amp;Main_thread, 0, 0, NULL);
    return true;
  }
  </code></pre>
  
  <p>the injector code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Windows.h&gt;
  #include &lt;TlHelp32.h&gt;
  
  using namespace std;
  bool InjectDLL(DWORD ProcessID);
  
  char dllPath[250] = "C:\\Users\\root\\Desktop\\testdll\\bin\\Debug\\testdll.dll";
  char ProcessName[] = "chrome.exe";
  typedef HINSTANCE (*fpLoadLibrary)(char*);
  
  int main()
  {
  DWORD processId = NULL;
  PROCESSENTRY32 pe32 = {sizeof(PROCESSENTRY32)};
  HANDLE hProcSnap;
  hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if(Process32First(hProcSnap, &amp;pe32))
  {
      do{
           if(!strcmp(pe32.szExeFile, ProcessName))
          {
              processId = pe32.th32ProcessID;
              break;
          }
      }while(Process32Next(hProcSnap, &amp;pe32));
  }
  if (!InjectDLL(processId))
          cout &lt;&lt; "DLL failed to inject" &lt;&lt; endl;
  }
  
  bool InjectDLL(DWORD ProcessID)
  {
  HANDLE hProc;
  LPVOID paramAddr;
  HINSTANCE hDll = LoadLibrary("KERNEL32");
  fpLoadLibrary LoadLibraryAddr = (fpLoadLibrary)GetProcAddress(hDll, "LoadLibraryA");
  hProc = OpenProcess (PROCESS_ALL_ACCESS, false, ProcessID);
  paramAddr = VirtualAllocEx(hProc, 0, strlen(dllPath)+1, MEM_COMMIT, PAGE_READWRITE);
  bool memoryWritten = WriteProcessMemory(hProc, paramAddr, dllPath, strlen(dllPath)+1, NULL);
  CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, paramAddr, 0, 0);
  CloseHandle(hProc);
  return memoryWritten;
  }
  </code></pre>
  
  <p>the dll don't seem to be injected or he could'nt write,
  I didn't launch the injector as root</p>