借用一个以前的帖子<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, tlhelp32;<br>{type<br> TProcessEntry32 = record<br> dwSize: DWORD;<br> cntUsage: DWORD;<br> th32ProcessID: DWORD;<br> th32DefaultHeapID: DWORD;<br> th32ModuleID: DWORD;<br> cntThreads: DWORD;<br> th32ParentProcessID: DWORD;<br> pcPriClassBase: integer;<br> dwFlags: DWORD;<br> szExeFile: array[0..MAX_PATH - 1] of char;<br> end; }<br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>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><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 &gt; 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><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> AttachToProcess('Explorer.exe', extractfilepath(paramstr(0))+'Project2.dll');<br>end;<br>end.<br>///////////////下面是DLL文件<br>unit UnitDll;<br><br>interface<br><br>uses<br> SysUtils,<br> Classes,<br> Windows,<br> Dialogs;<br><br>var<br> hThreadHandle: Dword;<br> dwThreadID: Dword;<br> <br>implementation<br><br>{在左上角显示时间}<br>procedure ThreadProc;<br>var<br> hScreenDC: hdc;<br> SystemTime: _SYSTEMTIME;<br> Temp: string;<br> MyOutput: PChar;<br>begin<br> while true do<br> begin<br> Sleep(100);<br> hScreenDC := GetDC(0);<br> GetLocalTime(SystemTime);<br> Temp := format('Current Time is %d-%d-%d %d:%d:%d', [SystemTime.wYear,<br> SystemTime.wMonth,<br> SystemTime.wDay,<br> SystemTime.wHour,<br> SystemTime.wMinute,<br> SystemTime.wSecond]);<br> MyOutPut := Pchar(temp);<br> TextOut(hScreenDC, 0, 0, MyOutPut, lstrlen(MyOutPut));<br> ReleaseDC(0, hScreenDC);<br> end;<br>end;<br><br>initialization<br> hThreadHandle := CreateThread(nil, 0, @ThreadProc, nil, 0, dwThreadID);<br>finalization<br> if (hThreadHandle &lt;&gt; 0) then<br> TerminateThread(hThreadHandle, 0);<br> <br>end.<br>