title: Dll injection in x64 process don't work
body: <p>I have made a dll (32 Bits plattform) and now I want inject in any x64 process.</p>
  
  <p>Was found several examples on web of source codes in Delphi that promisses do this, but when was tested, none dll was injected in any x64 process, already when tested with x86 process these codes works very fine!.</p>
  
  <p>I found a unique example that promisses inject a dll file in x64 process, but in my tests, nothing have worked when was tried inject a dll in <em>notepad.exe</em> for example.</p>
  
  <p>So, someone here have a example of dll injector working for x64 processors or can help me to do this with this example following (if this is possible) ?</p>
  
  <p>Any suggestions will welcome!</p>
  
  <p>My last attempt was:</p>
  
  <p><em>Injector</em></p>
  
  <pre><code>  function InjectDLL(const dwPID: DWORD; {$IFDEF UNICODE} DLLPath: PWideChar
      {$ELSE} DLLPath: PAnsiChar {$ENDIF} ): Integer;
  
      const
        Kernel32 = 'kernel32.dll';
      var
        dwThreadID: Cardinal;
        hProc, hThread, hKernel: THandle;
        BytesToWrite, BytesWritten: SIZE_T;
        pRemoteBuffer, pLoadLibrary: Pointer;
      begin
        hProc := OpenProcess(PROCESS_CREATE_THREAD or PROCESS_QUERY_INFORMATION or
          PROCESS_VM_OPERATION or PROCESS_VM_WRITE or PROCESS_VM_READ, False, dwPID);
        if hProc = 0 then
          exit(0);
        try
          BytesToWrite := SizeOf(WideChar) * (Length(DLLPath) + 1);
          pRemoteBuffer := VirtualAllocEx(hProc, nil, BytesToWrite, MEM_COMMIT,
            PAGE_READWRITE);
          if pRemoteBuffer = nil then
            exit(0);
          try
            if not WriteProcessMemory(hProc, pRemoteBuffer, DLLPath, BytesToWrite,
              BytesWritten) then
              exit(0);
      {$REGION 'Check for UNICODE'}
      {$IFDEF UNICODE}
            hKernel := GetModuleHandleW(Kernel32);
            pLoadLibrary := GetProcAddress(hKernel, 'LoadLibraryW');
      {$ELSE}
            hKernel := GetModuleHandleA(Kernel32);
            pLoadLibrary := GetProcAddress(hKernel, 'LoadLibraryA');
      {$ENDIF}
      {$ENDREGION}
            hThread := CreateRemoteThread(hProc, nil, 0, pLoadLibrary, pRemoteBuffer,
              0, dwThreadID);
            try
              WaitForSingleObject(hThread, INFINITE);
            finally
              CloseHandle(hThread);
            end;
          finally
            VirtualFreeEx(hProc, pRemoteBuffer, 0, MEM_RELEASE);
          end;
        finally
          CloseHandle(hProc);
        end;
        exit(1);
      end;
  
  begin
  
  if InjectDLL(4864, 'C:\SampleDLL') &lt;&gt; 0 then begin
      ShowMessage('woO!');
  
  end;
  
  end.
  </code></pre>
  
  <p><em>Dll</em></p>
  
  <pre><code> uses
    System.SysUtils,
    System.Classes,
    Variants,
    Winapi.Windows;
  
  Function StartThread(pFunction : TFNThreadStartRoutine; iPriority : Integer = Thread_Priority_Normal; iStartFlag : Integer = 0) : THandle;
  var
  ThreadID : DWORD;
  begin
  Result := CreateThread(nil, 0, pFunction, nil, iStartFlag, ThreadID);
  if Result &lt;&gt; Null then
  SetThreadPriority(Result, iPriority);
  end;
  
  Function CloseThread( ThreadHandle : THandle) : Boolean;
  begin
  Result := TerminateThread(ThreadHandle, 1);
  CloseHandle(ThreadHandle);
  end;
  
  procedure ThisIsTheThread;
  begin
   MessageBoxW(0,'I am in your target : Dll file','woO!',0)
  end;
  
  procedure Run;
  Var
  hThread : THandle;
  begin
  hThread := StartThread(@ThisIsTheThread);
  hThread := StartThread(@ThisIsTheThread,THREAD_PRIORITY_ERROR_RETURN);
  CloseThread(hThread);
  end;
  
  
  procedure mydllproc(Reason: Integer);
  begin
    case Reason of
      DLL_PROCESS_ATTACH:
        begin
           Run;
        end;
    end;
  end;
  
  begin
    DllProc := mydllproc;
    mydllproc(DLL_PROCESS_ATTACH);
  
  end.
  </code></pre>
  
  <p>PS: works fine with 32 Bits process as said above.</p>
  
  <p><a href="http://www.ic0de.org/showthread.php?12067-SNIPPET-DLL-Injector-in-Delphi-XE3-(32bit-and-64bit-App-)" rel="nofollow">Source</a></p>