BOOL EjectDll (DWORD PID, LPCTSTR DllPath)
{
BOOL bMore=FALSE, bFound=FALSE;
HANDLE hSnapshot=INVALID_HANDLE_VALUE;
HANDLE hProcess=NULL;
MODULEENTRY32 me={sizeof (me)};
LPTHREAD_START_ROUTINE pThreadProc=NULL;
HMODULE hMod=NULL;
HSnapshot=CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, PID);
If (INVALID_HANDLE_VALUE=hSnapshot https://bbs.csdn.net/topics/=
{
_tprintf (L "snapshot creation process failed!" , GetLastError ());
return FALSE;
}
BMore=Module32First (hSnapshot, & amp; Me);
For (; BMore; BMore=Module32Next (hSnapshot, & amp; Me))//find module handles
{
If (_wcsicmp (me) szModule, DllPath) | | _wcsicmp (me) szExePath, DllPath))
{
BFound=TRUE;
break;
}
}
if (! BFound)
{
_tprintf (L "was not found in the process of being injected into the DLL");
If (hSnapshot!=INVALID_HANDLE_VALUE)
The CloseHandle (hSnapshot);
return FALSE;
}
HProcess=OpenProcess (PROCESS_ALL_ACCESS, FALSE, PID);
if (! HProcess)
{
_tprintf (L "open by injection process failed, did not get process handles!" , GetLastError ());
If (hSnapshot!=INVALID_HANDLE_VALUE)
The CloseHandle (hSnapshot);
return FALSE;
}
HMod=GetModuleHandle (L "kernel32. DLL");
If (hMod==NULL)
{
_tprintf (L "unable to obtain kernel32. The DLL module handles!" ,
GetLastError ());
If (hProcess)
The CloseHandle (hProcess);
If (hSnapshot!=INVALID_HANDLE_VALUE)
The CloseHandle (hSnapshot);
return FALSE;
}
PThreadProc=(LPTHREAD_START_ROUTINE) GetProcAddress call (hMod, "FreeLibrary");
If (pThreadProc==NULL)
{
Failed to get FreeLibrary function starting address _tprintf (L "!" , GetLastError ());
If (hProcess)
The CloseHandle (hProcess);
If (hSnapshot!=INVALID_HANDLE_VALUE)
The CloseHandle (hSnapshot);
return FALSE;
}
HANDLE hThread=CreateRemoteThread (hProcess, NULL, 0, pThreadProc, me. ModBaseAddr, 0, NULL);
If (! HThread)
{
_tprintf (L "failed to create the remote thread!" );
If (hProcess)
The CloseHandle (hProcess);
If (hSnapshot!=INVALID_HANDLE_VALUE)
The CloseHandle (hSnapshot);
return FALSE;
}
If (hSnapshot!=NULL)
The CloseHandle (hSnapshot);
If (hThread!=NULL)
The CloseHandle (hThread);
If (hProcess!=NULL)
The CloseHandle (hProcess);
Return TRUE;
}
CodePudding user response:
Uninstall function calls a DLL within itself,
CodePudding user response: