我写了一个查看指定进程的小程序,程序执行的时候会每秒重复读出我指定的进程名,可是运行的时候发现程序读到大概25秒左右的时候就会出错了,但是我也不知道是怎么回事,所以特在此请教各位,望朋友们能给解答一下,先谢谢了。代码如下:dll部分的代码:
library jinchengdll;uses
  SysUtils,TLHelp32,
  Classes;{$R *.res}function zhaodaojincheng(shuru : string):string; stdcall;var
  jubing : longword;//进程快照句柄
  fprocessentry32 : TProcessEntry32;//结构类型的变量
  zhenjia : Boolean; //返回一个布尔值
  processid : longword;//储存进程ID
  mingcheng : string;//储存进程名称
  begin
      jubing := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //获得进程快照句柄
      fprocessentry32.dwSize := sizeof(fprocessentry32); //给TProcessEntry32结构的第一个参数赋值(也可以理解为把这个结构的第一个参数初始化)
      zhenjia := Process32First(jubing,fprocessentry32);//取得第一个进程信息
         while zhenjia = true do
               begin
                zhenjia := Process32Next(jubing,FprocessEntry32);//取得第下一个进程信息
                mingcheng := fprocessentry32.szExeFile;//取得一个进程的名称
                 if mingcheng = shuru then//如果进程名等于这个字符串
                 result := mingcheng; //该函数返回值                  end;  end;  exports
  zhaodaojincheng;begin
end.
*************************************************************************
exe部分代码:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Edit1: TEdit;
    Label2: TLabel;
    Timer1: TTimer;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementationfunction zhaodaojincheng(shuru : string):string;stdcall;external'jinchengdll.dll';{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.Enabled := true;end;procedure TForm1.FormCreate(Sender: TObject);
begin
label2.font.Color := clred;
label2.Caption := '请注意:该程序输入的进程名必须完'+#10'                   全符合进程列表里面的名称!';
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
shuru : string;//给自定义函数输入参数用的变量begin   shuru := edit1.text;
   self.Memo1.lines.Add(zhaodaojincheng(shuru));
end;end.
*****************************************************
程序截图如下:
程序执行25次循环之后就出现:

解决方案 »

  1.   

    1.主程序和dll之间传递string,请在dll项目use节的第一个,加上ShellMem
    2.CreateToolhelp32Snapshot创建的句柄,要在结束时CloseHandle
      

  2.   

    顶楼上..补一点点书面意思.帮助理解内核对象(包括线程Handle)都是系统资源,用了要还的,也就是说用完后一定要closehandle关闭,如果不这么做,你系统的句柄资源很快就用光了。
      

  3.   

    我在dll的uses里面加上shellmem以后会出现:[致命错误] jinchengdll.dpr(15): File not found: 'ShellMem.dcu'另外我在dll里面最后循环代码end了以后的后面加上closehandle(jubing);
    编译的时候没错但是程序运行后循环执行25次左右还是会出现同样的错误,所以我又把这句删除了的。
      

  4.   

    参看下DLL提示{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }
      

  5.   

    实验了好几回 发现好像是memo控件的问题,只要把memo控件取消了不用显示出来找到的进程就OK不会报错了