Hi,
I'm trying to write a dll injector/ejector.
My inject code work fully but my eject code notify my dll is ejected but isn't ejected...
It is my eject code
Code:
private static bool EjectModule()
{
IntPtr hProcess = new IntPtr(0);
IntPtr hModule = new IntPtr(0);
int LenWrite = DllName.Length + 1;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, (uint)loadedPID);
try
{
IntPtr ModDll32 = GetModuleHandle("kernel32.dll");
IntPtr MyModDll = GetModuleHandle(nameofdll);
if (ModDll32 != null)
{
hModule = VirtualAllocEx(hProcess, IntPtr.Zero, (UIntPtr)LenWrite, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (MyModDll != null && hModule != null)
{
IntPtr hDllThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, GetProcAddress(GetModuleHandle("kernel32.dll"), "FreeLibrary"), hModule, 0, 0);
if (hDllThread != null)
{
if (WaitForSingleObject(hDllThread, INFINITE) != WAIT_FAILED)
{
CloseHandle(hDllThread);
CloseHandle(hProcess);
Console.WriteLine("Dll ejected succesfully!");
//loadedPID = 0;
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return true;
}
else
{
CloseHandle(hDllThread);
throw new Exception("unknow error =S");
}
}
else throw new Exception("CreateRemoteThread failed");
}
else throw new Exception("Can't found injected dll, or can't allocate memory");
}
else throw new Exception("Can't found kernel32.dll");
}
catch (Exception e)
{
Console.WriteLine("Failure on trying eject module");
Console.WriteLine("Exception :");
Console.WriteLine(e.ToString());
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return false;
}
CloseHandle(hProcess);
}
I thinks this code fail at
Code:
IntPtr hDllThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, GetProcAddress(GetModuleHandle("kernel32.dll"), "FreeLibrary"), hModule, 0, 0);
Thanks in advance.