i tried to inject a dll into another process,but CreateRemoteThread() fails :(
Code:
const char szDLL[] = "somedll.dll";
void RemoteLoadDll(HANDLE,const char *);
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,INT){
RemoteLoadDll(GetCurrentProcess(),szDLL); // i also tried with handles to different processes
return 0;
}
void RemoteLoadDll(HANDLE hProcess,const char *szDll){
char szLibPath[_MAX_PATH];
void* pLibRemote; // The address (in the remote process) where
// szLibPath will be copied to;
DWORD hLibModule; // Base address of loaded module (==HMODULE);
HMODULE hKernel32 = ::GetModuleHandle("Kernel32");
// initialize szLibPath
strcpy(szLibPath,szDll);
// 1. Allocate memory in the remote process for szLibPath
// 2. Write szLibPath to the allocated memory
pLibRemote = ::VirtualAllocEx( hProcess, NULL, sizeof(szLibPath),
MEM_COMMIT, PAGE_READWRITE );
::WriteProcessMemory( hProcess, pLibRemote, (void*)szLibPath,
sizeof(szLibPath), NULL );
MessageBox(NULL,"before createremotethread()","remoteloaddll()",MB_OK);
// Load DLL into the remote process
// (via CreateRemoteThread & LoadLibrary)
// THIS WILL RAISE A MEMORY ACCESS EXCEPTION...WHY??
hThread = ::CreateRemoteThread( hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE) ::GetProcAddress( hKernel32,
"LoadLibraryA" ),
pLibRemote, 0, NULL );
MessageBox(NULL,"after createremotethread()","remoteloaddll()",MB_OK);
::WaitForSingleObject( hThread, INFINITE );
// Get handle of the loaded module
::GetExitCodeThread( hThread, &hLibModule );
// Clean up
::CloseHandle( hThread );
::VirtualFreeEx( hProcess, pLibRemote, sizeof(szLibPath), MEM_RELEASE );
}
help