OpenProcess返回0

byyouxin 2010-01-19 06:05:10

procedure TForm1.Button2Click(Sender: TObject);
var
a:integer;
c, pid:longword;
begin
a:=findwindow('mingup',0);
pid:=GetWindowThreadProcessId(a);
c:=OpenProcess(PROCESS_ALL_ACCESS, False,pid);

Edit2.Text:=inttostr(c);
if (c=0) then begin
showmessage('呀么跌') ;
end;

end;

end.
...全文
282 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
byyouxin 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 keiy 的回复:]
程序有两处问题:
1.a:=findwindow('mingup',0);
前一个应该是类名,你的类名为mingup吗?如果是窗口名,应该是a:=findwindow(nil,'mingup');
2.pid:=GetWindowThreadProcessId(a);
GetWindowThreadProcessId返回的是thread ID,而非PID,要得到PID正确的是:
tid:=GetWindowThreadProcessId(a,pid);
我的程序可以正确运行:
var
    a:integer;
  c, tid,pid:longword;
begin
    a:=findwindow(nil,'计算器');

    tid:=GetWindowThreadProcessId(a,pid);
      Edit1.Text:= inttostr(pid) ;
    c:=OpenProcess(PROCESS_ALL_ACCESS, false,pid);

    Edit2.Text:=inttostr(c);
  if (c=0) then begin
  showmessage('呀么跌') ;
  end;

end;
[/Quote]
谢谢 原来如此!
柯本 2010-01-19
  • 打赏
  • 举报
回复
程序有两处问题:
1.a:=findwindow('mingup',0);
前一个应该是类名,你的类名为mingup吗?如果是窗口名,应该是a:=findwindow(nil,'mingup');
2.pid:=GetWindowThreadProcessId(a);
GetWindowThreadProcessId返回的是thread ID,而非PID,要得到PID正确的是:
tid:=GetWindowThreadProcessId(a,pid);
我的程序可以正确运行:
var
a:integer;
c, tid,pid:longword;
begin
a:=findwindow(nil,'计算器');

tid:=GetWindowThreadProcessId(a,pid);
Edit1.Text:= inttostr(pid) ;
c:=OpenProcess(PROCESS_ALL_ACCESS, false,pid);

Edit2.Text:=inttostr(c);
if (c=0) then begin
showmessage('呀么跌') ;
end;

end;
dd_zhouqian 2010-01-19
  • 打赏
  • 举报
回复
所有的函数返回值,都做判断
gyk120 2010-01-19
  • 打赏
  • 举报
回复
findwindow不怎么靠谱,你可以用进程快照查找进程,然后通过进程findwindow
kfcoffe 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 brightyang 的回复:]
showmessage('呀么跌') ;

[/Quote]
呵呵
brightyang 2010-01-19
  • 打赏
  • 举报
回复
showmessage('呀么跌') ;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧