在程序中用ShellExecute和ShellExecuteEx调用外部程序时,程序占用的内存会持续增大(100分)

F

fsljm

Unregistered / Unconfirmed
GUEST, unregistred user!
在程序中用ShellExecute和ShellExecuteEx调用外部程序时,程序占用的内存会增大,但
外部程序被关闭后,占用内存不会跟着减少,所以如果在程序作过多次的调用然后又关闭
外部程序的操作后,程序占用的内存会比初始时大很多。如果这是一个长时间不间断运行
的程序的话,势必会耗光内存。有哪位高手知道这是为什么?如何解决这个问题?
 
贴上源码,大家自己看吧,源码出自DFW的老贴子上面:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, WinProcs, ShellAPI;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
cmdStr:pchar;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
cmdstr:=pchar('RunDLL32.exe Shell32.DLL,Control_RunDLL Intl.cpl,,4');
{建立进程并等待其结束}
fillchar(StartupInfo,sizeof(StartupInfo),0);

CreateProcess(nil,cmdstr,nil,nil,false,0,nil,nil,StartupInfo,ProcessInfo);
With ProcessInfo do
begin
CloseHandle(hThread);
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
end;
end;
end.
--------------------------------------------------------------------------
---
 
张无忌:
你给出的源码,我有一行不太懂:
cmdstr:=pchar('RunDLL32.exe Shell32.DLL,Control_RunDLL Intl.cpl,,4');
RunDLL32.exe后面的那一串起何作用?能否解释一下?
 
你把这换成你要运行的EXE的路径就可以了
 
哦,这我就明白了。不过这种方式解决不了问题。用这种方式起动的程序不能手工关闭,
一旦执行手工关闭,窗口会消失,但进程未真的清除掉,而且会导致启动它的程序死锁
(窗口变灰),只有通过任务管理器将进程真的关闭,启动它的程序才会恢复活动状态。
我是想写一个象Explorer的程序,通过双击图标可以启动外部程序,用户可以自行关闭
外部程序。我用shellExecute来做,可以完成的很好。但后来发现程序占用的内存在逐
渐增大,仔细分析,才发现原来是启动外部程序时,会多消耗内存,但用户自行关闭外
部程序后,多消耗的内存也不释放,下次再启动外部程序时,又会多占一些内存。
 
刚才也试了用createprocess启动外部程序,也有占用的内存在外部程序关闭后不会回收
的问题,跟shellExecute是一样的!
 
With ProcessInfo do
begin
CloseHandle(hThread);
CloseHandle(hProcess);
end;
把那行代码换成这样就基本可以解决问题
 
还是一样!占用内存在增大,只是增大的慢一些而已!看来还有东西未释放!
 
顶部