procedure FindAProcess(const AFilename: string; const PathMatch: Boolean; var ProcessID: DWORD);<br>var<br> lppe: TProcessEntry32;<br> SsHandle: Thandle;<br> FoundAProc, FoundOK: boolean;<br>begin<br> ProcessID :=0;<br> SsHandle := CreateToolHelp32SnapShot(TH32CS_SnapProcess, 0);<br> FoundAProc := Process32First(Sshandle, lppe);<br> while FoundAProc do<br> begin<br> if PathMatch then<br> FoundOK := AnsiStricomp(lppe.szExefile, PChar(AFilename)) = 0<br> else<br> FoundOK := AnsiStricomp(PChar(ExtractFilename(lppe.szExefile)), PChar(ExtractFilename(AFilename))) = 0;<br> if FoundOK then<br> begin<br> ProcessID := lppe.th32ProcessID;<br> break;<br> end;<br> FoundAProc := Process32Next(SsHandle, lppe);<br> end;<br> CloseHandle(SsHandle);<br>end;<br><br>function EnabledDebugPrivilege(const bEnabled: Boolean): Boolean;<br>var<br> hToken: THandle;<br> tp: TOKEN_PRIVILEGES;<br> a: DWORD;<br>const<br> SE_DEBUG_NAME = 'SeDebugPrivilege';<br>begin<br> Result := False;<br> if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then<br> begin<br> tp.PrivilegeCount := 1;<br> LookupPrivilegeValue(nil, SE_DEBUG_NAME, tp.Privileges[0].Luid);<br> if bEnabled then<br> tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED<br> else<br> tp.Privileges[0].Attributes := 0;<br> a := 0;<br> AdjustTokenPrivileges(hToken, False, tp, SizeOf(tp), nil, a);<br> Result := GetLastError = ERROR_SUCCESS;<br> CloseHandle(hToken);<br> end;<br>end;<br>function AttachToProcess(const HostFile, GuestFile: string; const PID: DWORD = 0): DWORD;<br>var<br> hRemoteProcess: THandle;<br> dwRemoteProcessId: DWORD;<br> cb: DWORD;<br> pszLibFileRemote: Pointer;<br> iReturnCode: Boolean;<br> TempVar: DWORD;<br> pfnStartAddr: TFNThreadStartRoutine;<br> pszLibAFilename: PwideChar;<br>begin<br> Result := 0;<br> EnabledDebugPrivilege(True);<br> Getmem(pszLibAFilename, Length(GuestFile) * 2 + 1);<br> StringToWideChar(GuestFile, pszLibAFilename, Length(GuestFile) * 2 + 1);<br> if PID > 0 then<br> dwRemoteProcessID := PID<br> else<br> FindAProcess(HostFile, False, dwRemoteProcessID);<br> hRemoteProcess := OpenProcess(PROCESS_CREATE_THREAD + {ÔÊÐíÔ¶³Ì´´½¨Ïß³Ì}<br> PROCESS_VM_OPERATION + {ÔÊÐíÔ¶³ÌVM²Ù×÷}<br> PROCESS_VM_WRITE, {ÔÊÐíÔ¶³ÌVMд}<br> FALSE, dwRemoteProcessId);<br> cb := (1 + lstrlenW(pszLibAFilename)) * sizeof(WCHAR);<br> pszLibFileRemote := PWIDESTRING(VirtualAllocEx(hRemoteProcess, nil, cb, MEM_COMMIT, PAGE_READWRITE));<br> TempVar := 0;<br> iReturnCode := WriteProcessMemory(hRemoteProcess, pszLibFileRemote, pszLibAFilename, cb, TempVar);<br> if iReturnCode then<br> begin<br> pfnStartAddr := GetProcAddress(GetModuleHandle('Kernel32'), 'LoadLibraryW');<br> TempVar := 0;<br> Result := CreateRemoteThread(hRemoteProcess, nil, 0, pfnStartAddr, pszLibFileRemote, 0, TempVar);<br> end;<br> Freemem(pszLibAFilename);<br>end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> AttachToProcess('QQ.exe', 'D:/iNDS-Top/Client/NDSDLL.dll');<br>end;