var 
  ShellWindow : IShellWindows;
  nCount : Integer;
begin
  while not terminated do begin
    try
      ShellWindow := CoShellWindows.Create;
      nCount := ShellWindow.Count;
    except
    end;
    sleep(1000);
  end;
在Thread错误信息:标记没有引用存储如果写在不写在Thread中,程序运行正常?不知道为什么了?

解决方案 »

  1.   

    在线程中引用COM对象,要用CoInitialize
    uses ActiveX;var 
      ShellWindow : IShellWindows;
      nCount : Integer;
    begin
      CoInitialize(nil);
      try
        while not terminated do begin
        try
          ShellWindow := CoShellWindows.Create;
          nCount := ShellWindow.Count;
        except
        end;
        sleep(1000);
      finally
        CoUnInitialize;
      end;
    end;
      

  2.   

    在线程中使用COM接口要用CoInitialize进行初始化
    var 
      ShellWindow : IShellWindows;
      nCount : Integer;
    begin
      CoInitialize(nil);
      try
        while not terminated do begin
        try
          ShellWindow := CoShellWindows.Create;
          nCount := ShellWindow.Count;
        except
        end;
        sleep(1000);
      finally
        CoUninitialize
      end;
    end;