Code:
[DllImport("kernel32.dll")]
public static extern IntPtr CreateRemoteThread(int hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
[DllImport("kernel32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr GetModuleHandle(string Module);
[DllImport("kernel32.dll")]
public static extern int CloseHandle(IntPtr hHandle);
[DllImport("kernel32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string Procedure);
[DllImport("kernel32.dll")]
public static extern int OpenProcess(uint dwAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
// so on with others, WriteProcessMemory, VirtualAllocEx and VirtualFreeEx
int ProcId;
string DllName;
IntPtr LoadLibAddr = GetProcAddress(GetModuleHandle("Kernel32.dll"), "LoadLibraryA");
int HandleProc = OpenProcess(0x1fffff, false, ProcId);
int DllSize = DllName.Length + 1;
IntPtr Remotedll = VirtualAllocEx(HandleProc, IntPtr.Zero, DllSize, 0x1000, 4);
// WriteDllString to the allocated memory..
// Call CreateRemoteThread;
IntPtr Hndl = CreateRemoteThread(ProcId, IntPtr.Zero, 0, LoadLibAddr, Remotedll, 0, IntPtr.Zero);
VirtualFreeEx(ProcId, Hndl, DllSize, 0x8000);
CloseHandle(Hndl);
EDIT:
Wait, you can't use gabrola's code in C#, unless managed dll's can be injected nowadays..
You have to do it C# style.. Marshal.GetFunctionPointerForDelegate(Delegate d), WriteProcessMemory, VirtualAllocEx, VirtualFreeEx, CloseHandle, OpenProcess.. etc