先看下面两端代码
procedure TForm1.BitBtn1Click(Sender: TObject);
var
   lppe: tprocessentry32;
   sshandle: thandle;
   found: boolean;
   i: integer;
begin
   i := 0;
   sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
   found := process32first(sshandle, lppe);
   while found do
      begin
         inc(i);
         ListBox1.Items.Add(lppe.szExefile);
         found := Process32Next(sshandle, lppe);
         if found = True then BitBtn1.Caption := IntToStr(i);
      end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
   lppe: tprocessentry32;
   sshandle: thandle;
   found: boolean;
   i: integer;
begin
   i := 0;
   sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
   found := process32first(sshandle, lppe);
   while found do
      begin
         inc(i);
         ListBox1.Items.Add(lppe.szExefile);
         found := Process32Next(sshandle, lppe);
         //if found = True then BitBtn1.Caption := IntToStr(i);
      end;
end;
大家看清楚了吗,区别就是那就注释,第一个ListBox1是有数据的,然而第二个ListBox1没有数据,那段注释难道有什么蹊跷吗? 

解决方案 »

  1.   

    一般这种情况说明程序的某个执行需要一点延时,你可以理解为给点时间让硬件思考一下. 特别是在while这种循环里面
    你把//if found = True then BitBtn1.Caption := IntToStr(i);改成application.ProcessMessages;应该也可以
      

  2.   

    使用之前,lppe: tprocessentry32;最好进行初始化。
      

  3.   

    这是读“正在运行的程序的文件名列表”,要引用tlhelp32问题说的很清楚,运行第一段代码,ListBox1是有数据项的。
    而运行第二段代码,ListBox1没有一个数据项,是空的。
    这是为什么啊,难道被我注释的那段代码,起很重要的作用吗,也不见得啊,你说是吧。
      

  4.   

    如果说要给点延迟,那我写添加sleep(1000),可结果还是不行。
      

  5.   


    var
      FProcessEntry32: TProcessEntry32;
      PID: integer;
      List: TStringList;
      ModuleListHandle: Thandle;
      ModuleStruct: TMODULEENTRY32;
      J: integer;
      Yn: boolean;
    begin
      if Combobox1.itemindex = -1 then exit;
      List := TStringList.Create;
      FProcessEntry32 := GetProcessID(List, Combobox1.text);
      PID := FProcessEntry32.th32ProcessID;
      ModuleListHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pID);
      ListBox1.Items.Clear;
      ModuleStruct.dwSize := sizeof(ModuleStruct);
      yn := Module32First(ModuleListHandle, ModuleStruct);
      j := 0;
      while (yn) do
      begin
        SetLength(ModuleArray, j + 1);
        ModuleArray[j] := ModuleStruct;
        ListBox1.Items.Add(ModuleStruct.szExePath);
        yn := Module32Next(ModuleListHandle, ModuleStruct);
        J := j + 1;
      end;
      CloseHandle(ModuleListHandle);
      

  6.   


    procedure TForm1.BitBtn4Click(Sender: TObject);
    var
       lppe: tprocessentry32;
       sshandle: thandle;
       found: boolean;
       i: integer;
    begin
       i := 0;
       sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
       found := process32first(sshandle, lppe);
       while found do
          begin
             inc(i);
             ListBox1.Items.Add(lppe.szExefile);
             found := Process32Next(sshandle, lppe);
             //if found = True then BitBtn1.Caption := IntToStr(i);
          end;
    end;
    楼主,我就用你自己的代码也没有发现问题啊.
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
       lppe: tprocessentry32;
       sshandle: thandle;
       found: boolean;
       i: integer;
    begin
       i := 0;
       sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
       found := process32first(sshandle, lppe);
       while found do
          begin
             inc(i);
             ListBox1.Items.Add(lppe.szExefile);
             found := Process32Next(sshandle, lppe);
             if found = false then Button1.Caption := IntToStr(i);
          end;
    end;这样也没有问题.看样子是TBitBtn的问题.
      

  8.   

    情况确实这样字,
      while found do
          begin
             inc(i);
             ListBox1.Items.Add(lppe.szExefile);
             found := Process32Next(sshandle, lppe);
             //if found = True then BitBtn1.Caption := IntToStr(i);
          end;
    ShowMessage(IntToStr(ListBox1.Items.Count));结果提示是0
      

  9.   

    换成TButton不管注释不注释那行代码,都没有问题.如果是TBitBtn就不一样.
      

  10.   

    刚刚改把代码放到Button1里运行,可以实现了,
    TBitBtn,为什么会导致这个情况啊?
      

  11.   

    lppe: tprocessentry32;lppe.dwSize 必须写入 即应该 lppe.dwSize := sizeof(lppe);这是MSDN是写明白的。
      

  12.   

    你不填值,lppe.dwSize是个随机值,那结果就无法预料了。
      

  13.   

    TBitBtn在你机器上实际应用中的一个小bug吧
      

  14.   

    这个问题应该可能是DELPHI编译器的问题你这样写:
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
       lppe: tprocessentry32;
       sshandle: thandle;
       found: boolean;
       i,k: integer;
    begin
       i := 0;
       sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);
       found := process32first(sshandle, lppe);
       while found do
          begin
             inc(i);
             ListBox1.Items.Add(lppe.szExefile);
             found := Process32Next(sshandle, lppe);
             //if found = True then BitBtn1.Caption := IntToStr(i);这里IF判断是多余的吧?
              k:= k;加上这个也是可以的。
          end;
    end;