请教几个函数(100分)

  • 主题发起人 主题发起人 richmen
  • 开始时间 开始时间
R

richmen

Unregistered / Unconfirmed
GUEST, unregistred user!
我有两个独立的应用程序,分别为A、B,我想通过点击 A 的一个button,打开 B 程序,用那个函数?我想通过点击A的另一个button,关闭 B 程序,用那个函数?如果我已经运行了 B 程序,我点击 A 的button时,如何判断 B 程序已经在运行?(最好有具体用法或例子)。先谢了!
 
听不懂呀
 
运行:<br>&nbsp; Winexec();<br>关闭:<br>&nbsp; PostMessage(hwnd,WM_CLOSE,0,0);<br>查找:<br>&nbsp; FindWindow();
 
第一点不难!<br>第二点也不难,关键在于找程序B的条件,知道了这个程序B的主窗口的标题了吗?
 
用下面的方法,如果b不在运行则运行B,如果b在运行则关闭<br>uses<br>&nbsp; TlHelp32;<br><br>procedure TForm1.Button1Click(Sender: TObejct);<br>var<br>&nbsp; ProcessSnapShotHandle: THandle;<br>&nbsp; ProcessEntry: TProcessEntry32;<br>&nbsp; ProcessHandle: THandle;<br>&nbsp; Ret: BOOL;<br>&nbsp; Found: Boolean;<br>begin<br>&nbsp; Found:=False;<br>&nbsp; ProcessSnapShotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; if ProcessSnapShotHandle&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; ProcessEntry.dwSize:=SizeOf(TProcessEntry32);<br>&nbsp; &nbsp; Ret:=Process32First(ProcessSnapShotHandle, ProcessEntry);<br>&nbsp; &nbsp; while Ret do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if LowerCase(ProcessEntry.szExeFile)='b.exe' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Found:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; ProcessHandle:=OpenProcess(PROCESS_TERMINATE, False,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessEntry.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp; if ProcessHandle&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TerminateProcess(ProcessHandle, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(ProcessHandle)<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; Ret:=Process32Next(ProcessSnapShotHandle, ProcessEntry)<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseHandle(ProcessSnapShotHandle)<br>&nbsp; end;<br>&nbsp; if not Found then<br>&nbsp; &nbsp; WinExec('b.exe', SW_Show)<br>end;
 
同意<br>千年冰虫,
 
LeeChange你好,我用你的方法试了一下,如果没有打开B程序,就打开了,可是已经打开B程序后,他还是能在打开一个,而没有关闭,不知道是怎么回事?请指教!<br><br>我的程序:<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp;ProcessSnapShotHandle: THandle;<br>&nbsp;ProcessEntry: TProcessEntry32;<br>&nbsp;ProcessHandle: THandle;<br>&nbsp;Ret: BOOL;<br>&nbsp;Found: Boolean;<br>begin<br>&nbsp;Found:=False;<br>&nbsp;ProcessSnapShotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp;if ProcessSnapShotHandle&gt;0 then<br>&nbsp;begin<br>&nbsp; &nbsp;ProcessEntry.dwSize:=SizeOf(TProcessEntry32);<br>&nbsp; &nbsp;Ret:=Process32First(ProcessSnapShotHandle, ProcessEntry);<br>&nbsp; &nbsp;while Ret do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;if LowerCase(ProcessEntry.szExeFile)='C:/Program Files/WinZip/WINZIP32.EXE' then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Found:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp;ProcessHandle:=OpenProcess(PROCESS_TERMINATE, False, ProcessEntry.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp;if ProcessHandle&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TerminateProcess(ProcessHandle, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(ProcessHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;Ret:=Process32Next(ProcessSnapShotHandle, ProcessEntry);<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;CloseHandle(ProcessSnapShotHandle);<br>&nbsp;end;<br>&nbsp;if not Found then<br>&nbsp; &nbsp;WinExec('C:/Program Files/WinZip/WINZIP32.EXE', SW_Show);<br>end;
 
hehe,请先理解LowerCase的意义.<br>if LowerCase(ProcessEntry.szExeFile)='C:/Program Files/WinZip/WINZIP32.EXE' then<br>换成<br>if LowerCase(ProcessEntry.szExeFile)='c:/program files/winzip/winzip32.exe' then<br>ps: 如果用的不是Win98,你再找我吧.<br>
 
LeeChange,我用的是win2000
 
var<br>&nbsp; ProcessSnapShotHandle: THandle;<br>&nbsp; ProcessEntry: TProcessEntry32;<br>&nbsp; ProcessHandle: THandle;<br>&nbsp; Ret: BOOL;<br><br>&nbsp; ModuleSnapShotHandle: THandle;<br>&nbsp; ModuleEntry: TModuleEntry32;<br><br>&nbsp; Found: Boolean;<br>begin<br>&nbsp; Found:=False;<br>&nbsp; ProcessSnapShotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; if ProcessSnapShotHandle&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; ProcessEntry.dwSize:=SizeOf(TProcessEntry32);<br>&nbsp; &nbsp; Ret:=Process32First(ProcessSnapShotHandle, ProcessEntry);<br>&nbsp; &nbsp; while Ret do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ModuleSnapShotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessEntry.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; if ModuleSnapShotHandle&gt;0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ModuleEntry.dwSize:=SizeOf(TModuleEntry32);<br>&nbsp; &nbsp; &nbsp; &nbsp; Ret:=Module32First(ModuleSnapShotHandle, ModuleEntry);<br>&nbsp; &nbsp; &nbsp; &nbsp; if Ret then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if LowerCase(ModuleEntry.szExePath)='c:/program files/winzip/winzip32.exe' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Found:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessHandle:=OpenProcess(PROCESS_TERMINATE, False,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessEntry.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ProcessHandle&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TerminateProcess(ProcessHandle, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(ProcessHandle)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(ModuleSnapShotHandle)<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; Ret:=Process32Next(ProcessSnapShotHandle, ProcessEntry)<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseHandle(ProcessSnapShotHandle)<br>&nbsp; end;<br>&nbsp; if not Found then<br>&nbsp; &nbsp; WinExec('C:/Program Files/WinZip/WINZIP32.EXE', SW_Show)<br>end;
 
ShellExecute问题 &nbsp; <br>大家知道,在一个大型复杂的系统中,有时会调用一些外部程序来帮助我们完成某些特定功能。然而,如何打开并关闭这些外部程序呢?也许,这是一个老生常谈的话题,但笔者仍要继续讨论这个问题,原因有二:一是解决这个问题的方法很重要,并经常会遇到;二是大多数参考书提及的解决办法不全面,仍有很多朋友有疑问。下面,我将详细地阐述这个问题(提供的源代码均在WinNT/Delphi4.0环境下运行通过)。 <br><br>一、如何打开一个外部程序:<br>我们常用的函数有两个,WinExec,ShellExecute,因为ShellExecute函数主要用于Win32下,功能也强大一些,故而本文只对后者进行讨论。<br><br>首先,我们粗略地研究一下ShellExecute的几个参数:<br>hwnd:窗体的句柄; <br>lpOperation:打开程序执行的操作,共预留有"open","explore","print"三种方式,此参数可以省略,此时将依据打开的文件(lpFile)的类型执行相应的操作,比如:如果lpFile为一文本文件,那么将会在与该文件相关联的程序中打开它; <br>lpFile:文件名; <br>lpParamerters:打开文件时所需的参数; <br>lpDirectory:文件名所在的路径,当然,一般来说,在Windows中登 <br>"记记"过的程序(如WinWord)不必提供此参数; <br>nShowCmd:打开文件后程序窗体如何显示。 <br>该函数的详细文档请读者朋友参阅相应帮助。<br><br>其次,我们来举一个例子(以"记事本"为例): <br>procedure TForm1.OpenBtnClick(Sender:TObject); <br>begin <br>ShellExecute(handle,'open','notepad.exe',nil,nil,SW_ShowNormal); <br>end; <br>读者朋友可以在你的Delphi环境下试试这段代码,当然,由于使用Windows的API函数,请加上ShellAPI单元。<br><br>二、如何关闭打开的外部程序: <br>要关闭一个外部程序,只需向他发送一条消息就行了,比如,SendMessage(ExeHandle,WM_Close,0,0);其中ExeHandle是程序窗体的句柄,但如何得到窗体句柄呢?这是最为关键的地方,幸好Windows提供了FindWindow()函数,它能够解决这个问题,它有两个参数:lpClassName:程序的类名;lpWindowName:程序窗体的标题。<br><br>第一种情况也是最简单情况的就是提供确定的[*"确定的"加着重号*] <br>"lpWindowName"参数,如下例: <br>procedure TForm1.CloseAppClick(Sender: TObject);//确定标题 <br>var<br>Exehandle:Thandle; <br>begin<br>//获得句柄--&gt;标题确定 <br>ExeHandle:=findWindow(nil,'文件管理器');//返回句柄 <br>//关闭程序<br>if ExeHandle&lt;&gt;0 then <br>SendMessage(ExeHandle,WM_Close,0,0) <br>Else <br>Application.MessageBox('没有打开"文件管理器"!','提示', <br>MB_IconInformation+MB_OK); <br>end; <br><br>另一种情况是程序窗体没有确定的[*"没有确定的"加着重号*]标题,这就是很多朋友出现疑问的地方,也是几乎所有参考书均不涉及的地方。比如打开"写字板"程序,它的标题会随着文本文件名的不同而不同,此时,你要关闭它,就不得不提供程序的类名,但是如何获得程序的类名呢?笔者在这里向大家介绍一个可行的办法:首先打开程序(此处以"记事本"为例),然后,运行WinSight32(Delphi自带)或Spy++(VC自带),找到程序( "记事本")的运行状态,即可找到我们需要的类名(两者均为"Class Name"项)。参考一个例子: <br>procedure TForm1.CloseVAppClick(Sender: TObject);//标题不确定 <br>var <br>var <br>ExeHandle:Thandle; <br>Begin <br>//获得句柄--&gt;标题不确定 <br>ExeHandle:=FindWindow('notepad',nil);//'');//返回句柄 <br>//关闭程序 <br>if ExeHandle&lt;&gt;0 then <br>SendMessage(ExeHandle,WM_Close,0,0) <br>Else <br>Application.MessageBox('没有打开"记事本"程序!','提示', <br>MB_IconInformation+MB_OK); <br>end; <br><br>当然,既能提供程序的类名又能提供程序窗体的标题自然就没有讨论的必要了。这样,我们就可以在自己的程序中打开并关闭任意的外部程序了。另外,笔者在这里罗嗦一句:上文提到的ShellExeCute()还有有相当"豪华" 的用处!细心的读者可能注意到有的作者的程序里提供了一些广告他们的连接,这其实就是ShellExeCute()的运用之一,略举一例: <br>procedure TForm1.HttpClick(Sender: TObject); <br>begin <br>ShellExecute(handle,'open','http://liangming.163.net', <br>nil,nil,SW_ShowNormal); <br>end; &nbsp;
 
如果程序都是你自己编写的,那还不简单。<br>如何运行?就不多说了,无非就是EINEXEC,SHELLEXCUTE,<br>CreateProcess等函数。<br>如何关闭它?也好说,发送自定义消息即可,可以在<br>SendMessage函数中使用HWND_BROADCAST。<br><br>如果程序不是自己编写的,那比较麻烦,可以参照LeeChange和wujer<br>方法。
 
后退
顶部