关闭进程和模拟键盘输入

作者在 2010-03-07 00:58:57 发布以下内容
program Project1;

uses
  Windows,SysUtils,Tlhelp32;

{$R *.res}

function FindProcessId(ExeFileName: string):THandle;
var
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
result:=0;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
    if UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName) then
    begin
      result:=FProcessEntry32.th32ProcessID;
      break;
    end;
    ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle (FSnapshotHandle);
end;

function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: boolean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

//但是对于服务程序,它会提示"拒绝访问".其实只要程序拥有Debug权限即可:
function EnableDebugPrivilege: Boolean;  //这个可以吗?
function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
var
TP: TOKEN_PRIVILEGES;
Dummy: Cardinal;
begin
TP.PrivilegeCount := 1;
LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
if bEnable then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else TP.Privileges[0].Attributes := 0;
AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
Result := GetLastError = ERROR_SUCCESS;
end;
var
hToken: Cardinal;
begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
result:=EnablePrivilege(hToken, 'SeDebugPrivilege', True);
CloseHandle(hToken);
end;

begin

//提高权限
   EnableDebugPrivilege;
   //结束进程
   KillTask('QQ.exe');
end.


----------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
hWindow: HWND;           { 窗体句柄 }
dwProcessID: DWORD; { 进程 ID }
hProcess: THandle;       { 进程句柄 }
begin
{ 根据标题获取窗体的句柄 }
hWindow := FindWindow(nil, '追影虚拟多桌面');
{ 通过窗体句柄获取进程 ID }
GetWindowThreadProcessId(hWindow, dwProcessID);

SetForegroundWindow(hwindow);   //设置消息的接受窗口也就是激活窗口
sleep(1000);  //休息一下
{ 通过进程 ID 获取进程句柄 }
hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, dwProcessID);

{ 结束该进程 }
TerminateProcess(hProcess, 0);
end;
这个是得到了句柄然后结束进程。用的了spy++。来得到具体的窗口标题
--------------------------------------------------------------------
begin

    winexec('notepad',1);
    //把记事本窗口激活
    setforegroundwindow(findwindow(nil,'无标题-记事本'));

    //模拟输入     'abc'
    keybd_event(65,0,KEYEVENTF_EXTENDEDKEY     or     0,0);
    keybd_event(65,0,KEYEVENTF_EXTENDEDKEY     or     KEYEVENTF_KEYUP,0);
    keybd_event(66,0,KEYEVENTF_EXTENDEDKEY     or     0,0);
    keybd_event(66,0,KEYEVENTF_EXTENDEDKEY     or     KEYEVENTF_KEYUP,0);

    keybd_event(67,0,KEYEVENTF_EXTENDEDKEY     or     0,0);
    keybd_event(67,0,KEYEVENTF_EXTENDEDKEY     or     KEYEVENTF_KEYUP,0);

    //模拟     alt     +     F     打开文件菜单
    keybd_event(VK_MENU,0,KEYEVENTF_EXTENDEDKEY     or     0,0);
    keybd_event(70,0,KEYEVENTF_EXTENDEDKEY     or     0,0);
    keybd_event(70,0,KEYEVENTF_EXTENDEDKEY     or     KEYEVENTF_KEYUP,0);
    keybd_event(VK_MENU,0,KEYEVENTF_EXTENDEDKEY     or     KEYEVENTF_KEYUP,0);

    //模拟输入     按     s       保存
    keybd_event(83,0,KEYEVENTF_EXTENDEDKEY     or     0,0);
    keybd_event(83,0,KEYEVENTF_EXTENDEDKEY     or     KEYEVENTF_KEYUP,0);
end;
网上找的好的源码。看一下。消息和得到进程的句柄
编程点滴 | 阅读 126086 次
文章评论,共0条
游客请输入验证码
浏览2354376次