title: C# Using byte[] directly in CreateRemoteThread and LoadLibrary injection
body: <p>I am using CreateRemoteThread to inject a dll into a target process and it works perfectly, currently it takes the parameters:</p>
  
  <pre><code>inject(string procName, string dllPath);
  </code></pre>
  
  <p>and reads dllPath to byte array:</p>
  
  <pre><code>byte[] bytes = Encoding.ASCII.GetBytes(dllPath);
  </code></pre>
  
  <p>I want to skip this process and give it the byte array to work with directly since I download encrypted bytes off my server. Note, my target platform is x64 which is why I'm not using jLibrary.</p>
  
  <p>If I go: byte[] bytes = File.ReadAllBytes(someFile) to test it out, the injection fails so I tried to convert the byte[] from my existing file to char[] then encode it to ascii but it also fails. How should I go about doing this?</p>
  
  <p>The class is:</p>
  
  <blockquote>
    <p>bool injectDLL(uint processToInject, string dllPath)
            {</p>
  </blockquote>
  
  <pre><code>        IntPtr processHandle = OpenProcess(desiredAccess, 1, processToInject);
  
          if (processHandle == INTPTR_ZERO) return false;
  
          IntPtr loadLibraryAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  
          if (loadLibraryAddress == INTPTR_ZERO) return false;
  
          IntPtr argAddress = VirtualAllocEx(processHandle, (IntPtr)null, (IntPtr)dllPath.Length, (0x1000 | 0x2000), 0X40);
  
          if (argAddress == INTPTR_ZERO) return false;
  
          byte[] bytes = Encoding.ASCII.GetBytes(dllPath);
  
          if (WriteProcessMemory(processHandle, argAddress, bytes, (uint)bytes.Length, 0) == 0)
              return false;
  
          if (CreateRemoteThread(processHandle, (IntPtr)null, INTPTR_ZERO, loadLibraryAddress, argAddress, 0, (IntPtr)null) == INTPTR_ZERO)
          {
              return false;
          }
  
          CloseHandle(processHandle);
          return true;
      }
  </code></pre>