怎么把Excel从系统进程中去掉?(50分)

  • 主题发起人 主题发起人 caoliu
  • 开始时间 开始时间
C

caoliu

Unregistered / Unconfirmed
GUEST, unregistred user!
我用D6+win2000+excel200;
在程序中ole excel:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
begin
MsExceL:=CreateOleObject('Excel.Application');
For i:=0 to (ListView1.Items.Count -1) do
begin
If ExtractFileExt(ListView1.Items.Caption)='.xls' then
begin
MsExceL.WorkBooks.open(ListView1.Items.Item.Caption);
MsExceL.Visible:=false;
MsExceL.WorkSheets[1].Activate;
end;
end;
.
.
.
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
if BitBtn2.Caption='关闭' then
begin
close;
end;
if BitBtn2.Caption='完成' then
begin
MSExceL.Quit;
MSExceL:=unassigned;
//MSExceL:=NULL;
BitBtn2.Caption:='关闭';
end;
end;

问题是excel以释放,但在系统进程中还有excel.exe,cpu=00;
怎么把Excel从系统进程中去掉?
 
unit EnumTask;

interface

uses Windows, Messages, Classes, SysUtils, TLHelp32;

type
TEnumTaskRecord = record
WindowTitle: PChar;
ProcessID, ThreadID: THandle;
WindowHandle: HWND;
end;
PEnumTaskRecord = ^TEnumTaskRecord;

TEnumTask = class
private
FList: TList;
procedure Clear;
function GetCount: Integer;
function GetItem(Index: Integer): PEnumTaskRecord;
public
constructor Create;
destructor Destroy; override;
procedure Open;
function IndexOf(ATitle: string): integer;
function Close(Index: integer):boolean;
property Count: Integer read GetCount;
property Items[Index: Integer]: PEnumTaskRecord read GetItem;
end;

type
TEnumProcessInfo = Record
ExeName : String;
ProcessID : DWORD;
end;
pEnumProcessInfo = ^TEnumProcessInfo;

TEnumProcess = class
private
FList: TList;

procedure Clear;
function GetCount: Integer;
function GetItem(Index: Integer): pEnumProcessInfo;
public
constructor Create;
destructor Destroy; override;
procedure Open;
function IndexOf(AExeName, AID: string): integer;
function Close(Index: integer):boolean;
property Count: Integer read GetCount;
property Items[Index: Integer]: pEnumProcessInfo read GetItem;
end;


implementation
//TEnumTask
constructor TEnumTask.Create;
begin
inherited;

FList := TList.Create;
end;

destructor TEnumTask.Destroy;
begin
Clear;
FList.Free;
inherited;
end;

procedure TEnumTask.Clear;
var
I: Integer;
begin
for I := 0 to FList.Count - 1 do
begin
StrDispose(PEnumTaskRecord(FList.Items)^.WindowTitle);
FreeMem(FList.Items);
end;
FList.Clear;
end;

function TEnumTask.GetCount: Integer;
begin
Result := FList.Count;
end;

function TEnumTask.GetItem(Index: Integer): PEnumTaskRecord;
begin
Result := PEnumTaskRecord(FList.Items[Index]);
end;


procedure TEnumTask.Open;
var
DesktopWindow, CurWindow: HWND;
TitleLength, I: Integer;
Node: PEnumTaskRecord;
Style: LongInt;
ProcessID, ThreadID: THandle;
begin
Clear;
DesktopWindow := GetDesktopWindow;
CurWindow := GetWindow(DesktopWindow, GW_CHILD);
while CurWindow <> 0 do
begin
if IsWindowVisible(CurWindow) then
begin
TitleLength := GetWindowTextLength(CurWindow);
if TitleLength > 0 then
begin
Style := GetWindowLong(CurWindow, GWL_EXSTYLE);
if (Style and WS_EX_TOOLWINDOW = 0) then
begin
ThreadID := GetWindowThreadProcessID(CurWindow, @ProcessID);
Node := nil;
for I := 0 to FList.Count - 1 do
if ThreadID = PEnumTaskRecord(FList.Items)^.ThreadID then
begin
Node := PEnumTaskRecord(FList.Items);
break;
end;
if Node = nil then
begin
New(Node);
Flist.Add(Node);
Node^.ThreadId := ThreadID;
Node^.ProcessID := ProcessID;
end;
Node^.WindowHandle := CurWindow;
Node^.WindowTitle := StrAlloc(TitleLength + 1);
GetWindowText(CurWindow, Node^.WindowTitle, TitleLength + 1);
end;
end;
end;
CurWindow := GetWindow(CurWindow, GW_HWNDNEXT);
end;
end;

function TEnumTask.IndexOf(ATitle: string): integer;
var
i:integer;
begin
result:=-1;
for i:=0 to FList.Count-1 do
if PEnumTaskRecord(FList.Items)^.WindowTitle = ATitle then
result:=i;
end;

function TEnumTask.Close(Index: integer):boolean;

begin
result:=false;
if TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, DWORD(GetItem(Index).ProcessID)),
$FFFFFFFF) then
result:=true;
end;

//TEnumProcess
constructor TEnumProcess.Create;
begin
inherited;

FList := TList.Create;
end;

destructor TEnumProcess.Destroy;
begin
Clear;
FList.Free;
inherited;
end;

procedure TEnumProcess.Clear;
var
I: Integer;
begin
for I := 0 to FList.Count - 1 do
begin
FreeMem(FList.Items);
end;
FList.Clear;
end;

function TEnumProcess.GetCount: Integer;
begin
Result := FList.Count;
end;

function TEnumProcess.GetItem(Index: Integer): pEnumProcessInfo;
begin
Result := pEnumProcessInfo(FList.Items[Index]);
end;


procedure TEnumProcess.Open;
var p : pEnumProcessInfo;
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeName := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
FList.Add(p);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;

function TEnumProcess.IndexOf(AExeName, AID: string): integer;
var
i:integer;
begin
result:=-1;
for i:=0 to FList.Count-1 do
if (pEnumProcessInfo(FList.Items)^.ExeName = AExeName) and
(pEnumProcessInfo(FList.Items)^.ProcessID = strtoint(AID)) then
result:=i;
end;

function TEnumProcess.Close(Index: integer):boolean;
begin
result:=false;
if TerminateProcess(OpenProcess(PROCESS_TERMINATE,
False,
GetItem(Index).ProcessID),
$FFFFFFFF) then
result:=true;
end;



end.
 
把MSExceL.Quit;
修改成MSExceL.application.Quit;
我刚学到的
 
把MSExceL.Quit;
修改成MSExceL.application.Quit;
不行啊!
 
列举系统进程,然后发消息关闭!!
 
我不知道怎样列举系统进程,然后发消息关闭!!
有没有其它办法?
 
the code I post above is just show you how to enumerate the process,
and kill them.
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
671
import
I
I
回复
0
查看
547
import
I
I
回复
0
查看
739
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部