C++ (Qt)// Microsoft linker helper for getting the DLL's HINSTANCE.// See http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx for more details.//// If you need to link with a non-MS linker, you would have to add code to look up the DLL's// path in HKCR. regsvr32 stores the path under the CLSID key for the 'Snapsie.CoSnapsie' interface.EXTERN_C IMAGE_DOS_HEADER __ImageBase;...// Get the path to this DLL so we can load it up with LoadLibrary.TCHAR dllPath[_MAX_PATH];GetModuleFileName((HINSTANCE) &__ImageBase, dllPath, _MAX_PATH);
Pascalprocedure TForm1.Button2Click(Sender: TObject);var Dll: String; hMem: Pointer; dwTmp: DWORD; hProcess, hThread: THandle; Injected: Boolean;begin Dll := edtDLL.Text; if not FileExists(Dll) then Exit; Injected := False; hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, ProcessId); if hProcess <> 0 then begin hMem := VirtualAllocEx(hProcess, nil, Length(Dll) + 1, MEM_COMMIT or MEM_RESERVE, PAGE_READWRITE); if hMem <> nil then begin WriteMem(hProcess, hMem, PChar(Dll), Length(Dll)); hThread := CreateRemoteThread(hProcess, nil, 0, GetProcAddress(GetModuleHandle(kernel32), 'LoadLibraryA'), hMem, 0, dwTmp); if hThread <> 0 then begin WaitForSingleObject(hThread, INFINITE); if GetExitCodeThread(hThread, dwTmp) then Injected := dwTmp <> 0; CloseHandle(hThread); end; VirtualFreeEx(hProcess, hMem, 0, MEM_RELEASE); end; CloseHandle(hProcess); end; if Injected then ShowMessage('Injected');end;