为什么列举窗口然后获得窗口的进程id与系统显示的不一样阿? (200分)

  • 主题发起人 主题发起人 hyne
  • 开始时间 开始时间
H

hyne

Unregistered / Unconfirmed
GUEST, unregistred user!
列举了所有窗口,获得hwnd,通过openprocess得到其进程id,为什么和系统显示的Id不一样阿?
有什么好的解决办法么?
 
edit 可是进程的主窗口的子空口.
获得主窗口后还要进一步获得edit 的句柄.
 
这个明白,关键是主窗口的句柄,谢谢
 
注意uses TLHelp32)
然后
var lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
begin
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
ListBox.Items.Add(StrPas(lppe.szExeFile));//列出所有进程。
found := Process32Next(Hand,lppe);
end;
end;

/////////////////////////////////////////////////////
uses ... TLHelp32, ...

type
TForm1 = class(TForm)
...
end;

var
Form1: TForm1;
l : Tlist; ////返回的东东在"L"这个TList中。

type
TProcessInfo = Record
ExeFile : String;
ProcessID : DWORD;
end;
pProcessInfo = ^TProcessInfo;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var p : pProcessInfo;
i : integer;
ContinueLoop:BOOL;
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
l := TList.Create;
l.Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeFile := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
l.Add(p);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var p : pProcessInfo;
i : integer;
begin
With l do
for i := Count - 1 DownTo 0 do
begin p := items; Dispose(p); Delete(i); end;
end;

...
end.
///////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
{Places the modulenames of the running/minimized tasks into a listbox }
var
pTask : pTaskEntry; {requires Uses ToolHelp}
Task : bool;
Pstr : array [0..79] of Char;
Str : string[80];
byt_j : byte;
begin
ListBox1.Clear;
GetMem(pTask, SizeOf(TTaskEntry)); {Reserve memory for TaskEntry}
pTask^.dwSize:=SizeOf(TTaskEntry);

byt_j:=0; {Set up a counter for number of tasks}
Task:=TaskFirst(pTask); {Find first task}
While task do
begin
inc(byt_j); {count number of different tasks}
Str:=StrPas(pTask^.szModule); {Convert PStr into Pascal string}
Listbox1.Items.Add(str); {Store Pascal string into listbox}
task:=taskNext(pTask); {Check for next possible task}
end;
Label1.Caption:=IntToStr(byt_j)+ ' tasks found'; {Show counter}
end;

 
这个问题已经解决了,没有参考你们的代码,所以大家就平分吧
我的代码整理后再贴上来
主要要采用同一个数字类型即可
 
本人的软件已经开发完成,源代码公开
软件功能:自动找到cterm的输入窗口句柄,随机输入字符防止bbs断线
关键技术:
sendkey
列举系统进程
列举顶级窗口
列举某个窗口所有子句柄
注册系统热键
源代码:(我都想给自己加分了)

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ImgList, ExtCtrls, CoolTrayIcon, Menus, ComCtrls,Tlhelp32;

type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
ImageList1: TImageList;
ListBox1: TListBox;
Timer1: TTimer;
CoolTrayIcon1: TCoolTrayIcon;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
StaticText7: TStaticText;
Timer2: TTimer;
StatusBar1: TStatusBar;
GroupBox2: TGroupBox;
StaticText4: TStaticText;
Label2: TLabel;
Edit1: TEdit;
StaticText5: TStaticText;
StaticText6: TStaticText;
Label4: TLabel;
Image1: TImage;
StaticText1: TStaticText;
StaticText2: TStaticText;
CtrlAltF81: TMenuItem;
CtrlAltF91: TMenuItem;
CtrlF121: TMenuItem;
CtrlF111: TMenuItem;
procedure StaticText1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure StaticText1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer2Timer(Sender: TObject);
procedure StaticText1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure CtrlAltF91Click(Sender: TObject);
procedure CtrlAltF81Click(Sender: TObject);
procedure CtrlF121Click(Sender: TObject);
procedure StatusBar1DblClick(Sender: TObject);
procedure CtrlF111Click(Sender: TObject);

private
{ Private declarations }
public
procedure WMHotKey(var Message: TWMHOTKEY); message WM_HOTKEY;
procedure ctermhwnd(Sender: TObject);
{ Public declarations }
end;
const
PROCESS_TERMINATE=$0001;
var
Form1: TForm1;
k,icc:integer;
list,listpid,listwin:tstringlist;
FSnapshotHandle: THandle;
ContinueLoop: BOOL;
FProcessEntry32: TProcessEntry32;
pidlen,pp:integer;
i,s,q:integer;
ss,ver:string;
qq:char;
implementation

{$R *.dfm}
uses shellapi;
function EnumWndProc(AWnd: HWND; AlParam: LPARAM):Boolean;stdcall;
var
WndCaption: array[0..254] of Char;
ProID:cardinal;
myHandle:THandle;
mm:integer;
begin
GetWindowText(AWnd, @WndCaption, 254);
if WndCaption[0]<>chr(0) then
begin
GetWindowThreadProcessId(awnd,proid);
for mm:=1 to listpid.Count do
begin
if (listpid.Strings[mm-1]=format('%x',[ProID])) and (IsWindowVisible(awnd)) then
begin
listwin.Add(format('%d',[awnd]));
end;
end;
end;
//killprocess.ListBox1.Items.Add(Format('%d = %s',[AWnd,StrPas(WndCaption)])+'pid:'+format('%x',[ProID^])+'pidhwnd:'+format('%x',[myhandle]));
Result := True;
end;

function GetEditHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;
var
buffer: array[0..255] of Char;
buffer1: array[0..255] of Char;
begin
Result := True;
//得到目标窗口的控件
GetClassName(hwnd,buffer,256);
//找到目标窗口的目标控件
if lowercase(strpas(buffer))='afxframeorview42s' then
begin
form1.ListBox1.Items.Add(inttostr(hwnd));
result:=false;
end;
end;//end of function


procedure SendShift(H: HWnd; Down: Boolean);
var vKey, ScanCode, wParam: Word;
lParam: longint;
begin
vKey:= $10;
ScanCode:= MapVirtualKey(vKey, 0);
wParam:= vKey or ScanCode shl 8;
lParam:= longint(ScanCode) shl 16 or 1;
if not(Down) then lParam:= lParam or $C0000000;
SendMessage(H, WM_KEYDOWN, vKey, lParam);
end;

procedure SendCtrl(H: HWnd; Down: Boolean);
var vKey, ScanCode, wParam: Word;
lParam: longint;
begin
vKey:= $11;
ScanCode:= MapVirtualKey(vKey, 0);
wParam:= vKey or ScanCode shl 8;
lParam:= longint(ScanCode) shl 16 or 1;
if not(Down) then lParam:= lParam or $C0000000;
SendMessage(H, WM_KEYDOWN, vKey, lParam);
end;

procedure SendKey(H: Hwnd; Key: char);
var vKey, ScanCode, wParam: Word;
lParam, ConvKey: longint;
Shift, Ctrl: boolean;
begin
ConvKey:= OemKeyScan(ord(Key));
Shift:= (ConvKey and $00020000) <> 0;
Ctrl:= (ConvKey and $00040000) <> 0;
ScanCode:= ConvKey and $000000FF or $FF00;
vKey:= ord(Key);
wParam:= vKey;
lParam:= longint(ScanCode) shl 16 or 1;
if Shift then SendShift(H, true);
if Ctrl then SendCtrl(H, true);
SendMessage(H, WM_KEYDOWN, vKey, lParam);
SendMessage(H, WM_CHAR, vKey, lParam);
lParam:= lParam or $C0000000;
SendMessage(H, WM_KEYUP, vKey, lParam);
if Shift then SendShift(H, false);
if Ctrl then SendCtrl(H, false);
end;
procedure TForm1.StaticText1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
k:=1;
screen.Cursor:=crcross;
end;

procedure TForm1.StaticText1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
k:=0;
screen.Cursor:=crdefault;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
HWndCalculator:HWnd;
test:integer;
proid:cardinal;
begin
list:=tstringlist.create;
listpid:=tstringlist.create;
listwin:=tstringlist.Create;
winexec('cterm.exe',sw_normal);
icc:=0;
ver:= 'Cterm外挂 V3.2';
HWndCalculator := FindWindow(nil,pchar(ver)); // close the exist Calculator
if HWndCalculator <> 0 then
begin
GetWindowThreadProcessId(HWndCalculator,proid);
TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),proid), 0);
//SendMessage(HWndCalculator, WM_CLOSE, 0, 0);
//application.Terminate;
end;
form1.Caption:=ver;
try
RegisterHotKey(Handle, 102, MOD_CONTROL + MOD_ALT, VK_F12);
RegisterHotKey(Handle, 101, MOD_CONTROL + MOD_ALT, VK_F11);
RegisterHotKey(Handle, 100, MOD_CONTROL + MOD_ALT, VK_F10);
RegisterHotKey(Handle, 99, MOD_CONTROL + MOD_ALT, VK_F9);
RegisterHotKey(Handle, 98, MOD_CONTROL + MOD_ALT, VK_F8);
RegisterHotKey(Handle, 97, MOD_CONTROL, VK_F12);
RegisterHotKey(Handle, 96, MOD_CONTROL, VK_F11);
except
end;
form1.Top:=((screen.Height - form1.Height) div 2);
form1.Left:=((screen.Width - form1.Width) div 2);
if not fileexists('aaaa.ini') then
begin
list.Clear;
list.Add(statictext4.caption);
list.Add(edit1.text);
list.Add('0');
list.SaveToFile('aaaa.ini');
end else begin
list.Clear;
list.LoadFromFile('aaaa.ini');
statictext4.Caption:=list.Strings[0];
edit1.Text:=list.Strings[1];
try
if list.Strings[2]='1' then cooltrayicon1.IconVisible:=true;
except end;
end;
timer1.Interval:=strtoint(edit1.Text+'000');
statictext7.Caption:=inttostr(timer1.Interval div 1000);
ctermhwnd(sender);
Timer1Timer(sender);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
ss:='';
if listbox1.Items.count>0 then
begin
for i:=0 to listbox1.Items.Count-1 do
begin
s:=length(statictext4.Caption);
q:=random(2)+1;
for k:=1 to q do
begin
qq:=statictext4.Caption[random(s)+1];
sendkey(strtoint(listbox1.Items.Strings),qq);
sleep(100);
ss:=ss+qq;
end;
sleep(100);
end;
timer1.Interval:=strtoint(inttostr(random(strtoint(edit1.text)))+'000')+5000;
statictext6.Caption:=ss;
end else begin
end;
statictext7.Caption:=inttostr(timer1.Interval div 1000);
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
listbox1.Items.Delete(listbox1.ItemIndex);
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
try
if strtoint(edit1.Text)=0 then edit1.Text:='60';
timer1.Interval:=strtoint(edit1.Text+'000');
statictext7.Caption:=inttostr(timer1.Interval div 1000);
except
edit1.Text:='60';
timer1.Interval:=strtoint(edit1.Text+'000');
end;
list.Clear;
list.LoadFromFile('aaaa.ini');
list.Strings[1]:=edit1.Text;
list.SaveToFile('aaaa.ini');
end;

procedure TForm1.N1Click(Sender: TObject);
begin
form1.Visible:=not form1.Visible;
if not form1.Visible then popupmenu1.Items[0].caption:='显示 Ctrl+Alt+F12'
else popupmenu1.Items[0].caption:='隐藏 Ctrl+Alt+F12';
end;

procedure TForm1.N2Click(Sender: TObject);
begin
statictext7.Caption:=inttostr(timer1.Interval div 1000);
timer2.Enabled:=not timer2.enabled;
timer1.Enabled:=not timer1.enabled;
if timer1.Enabled then popupmenu1.Items[1].Caption:='停止 Ctrl+Alt+F11'
else popupmenu1.Items[1].Caption:='开始 Ctrl+Alt+F11';
end;

procedure TForm1.N3Click(Sender: TObject);
begin
if (application.MessageBox(pchar('真的退出'+ver+'?'),'请确认',mb_okcancel)=idcancel) then exit;
list.Free;
application.Terminate ;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
popupmenu1.Items[0].Click;
action:=canone;
end;
procedure TForm1.WMHotKey(var Message: TWMHOTKEY);
begin
if message.HotKey=102 then popupmenu1.Items[0].click;
if message.HotKey=101 then popupmenu1.Items[1].click;
if message.HotKey=100 then popupmenu1.Items[2].click;
if message.HotKey=99 then popupmenu1.Items[3].Click;
if message.HotKey=98 then popupmenu1.Items[4].Click;
if message.HotKey=97 then popupmenu1.Items[5].Click;
if message.HotKey=96 then popupmenu1.Items[6].Click;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
cooltrayicon1.IconIndex:=random(imagelist1.count);
statictext7.Caption:=inttostr(strtoint(statictext7.caption)-1);
end;

procedure TForm1.StaticText1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
var p:TPoint;
hwnd:integer;
begin
GetCursorPos(p);
hwnd:=WindowFromPoint(p);
if k=1 then
statictext1.Caption:=inttostr(hwnd);
end;

procedure TForm1.CtrlAltF91Click(Sender: TObject);
begin
if fileexists('cterm.exe') then
begin
winexec('cterm',sw_normal);
ctermhwnd(sender);
end;
end;

procedure tform1.ctermhwnd(Sender: TObject);
begin
listbox1.Clear;
listpid.Clear;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
pidlen:=length(format('%s',[FProcessEntry32.szEXEfile]));
if lowercase(copy(format('%s',[FProcessEntry32.szEXEfile]),pidlen-8,9))='cterm.exe' then
begin
listpid.Add(format('%x',[FProcessEntry32.th32ProcessID]));
end;
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
//抓取快照完成
listwin.Clear;
EnumWindows(@EnumWndProc,0);
for pp:=1 to listwin.Count do
begin
EnumChildWindows(strtoint(listwin.Strings[pp-1]),@GetEditHandle,0);
end;
//自动获取cterm完成
end;

procedure TForm1.CtrlAltF81Click(Sender: TObject);
begin
showmessage('⌒ǒ Hyne love Maylu ǒ⌒'+#13+#13
+'关于: '+ver+#13+#13
+'本软件由Lucian编写'+#13
+'有任何意见请投条'+#13
+'简要使用方法:'+#13
+'1.将本软件置于Cterm安装目录'+#13
+'2.以后运行本软件即可启动Cterm'+#13
+'3.本程序将自动保持BBS不断'+#13
+'4.高手可以修改aaaa.ini调整配置'+#13
+' .1.第一行为随机发送字符序列'+#13
+' .2.第二行为发送随机间隔'+#13
+' .3.第三行为1时不显示tray'+#13);
end;

procedure TForm1.CtrlF121Click(Sender: TObject);
begin
cooltrayicon1.IconVisible:=not cooltrayicon1.IconVisible;
if cooltrayicon1.IconVisible then popupmenu1.items[5].Caption:='隐藏图标 Ctrl+F12'
else popupmenu1.items[5].Caption:='显示图标 Ctrl+F12';
end;

procedure TForm1.StatusBar1DblClick(Sender: TObject);
var
cuti,cutii:integer;
begin
cutii:=0;
for cuti:=0 to listbox1.Count-1 do
begin
if (listbox1.Items.Strings[cuti]=statictext1.caption) and (statictext1.caption<>'') then cutii:=1;
end;
if cutii=0 then listbox1.Items.Add(statictext1.caption);
end;

procedure TForm1.CtrlF111Click(Sender: TObject);
begin
ctermhwnd(sender);
end;

end.


 
多人接受答案了。
 
后退
顶部