Q&A

  • 불사조같은 Excel Process 완존히 죽이기..
//////////////////////////////////////////////////////////////////////////////////

// Excel을 create object를 하여 excel을 사용한후에 백그라운드에 살아있는

// 불사조같은 excel을

// 완존히 보내버리는 ...악~~ 소리없이..조용히..흐흐흐흐흐흐..나쁜넘..

//////////////////////////////////////////////////////////////////////////////////



0. 해당폼에 listbox1를 하나추가해줄것..(실행시는 안보이게설정한다.)



1. uses 절에 다음을 포함할것.

Tlhelp32;



2. 다음을 상수로 선언해줄것.

PROCESS_TERMINATE = $0001; // const for killing a proc

PROCESS_SET_INFORMATION = $0200; // const for changing priority



3. 선언부문

(********************************************************************************************)

procObject = class // class for use with Tlistbox.objects

private

fProc: TProcessEntry32; // declare all data private for any future changes

procedure putProcData(const value: TProcessEntry32);

function getProcData: TProcessEntry32;

procedure setPriority(const value: integer);

function getPriority: Integer;

public

constructor create(procinfo: TProcessEntry32);

published

property Theproc: TProcessEntry32 read getProcData write putProcData;

property ProcPriority: Integer read getPriority write setPriority;

end;

(********************************************************************************************)



3. 구현부문



constructor procObject.create(procinfo: TProcessEntry32);

begin

inherited create; // need to specify which inherited constructor to call

theproc := procinfo; // since parameter list differ

end;



function procObject.getPriority: Integer;

begin

result := fProc.pcPriClassBase;

end;



function procObject.getProcData: TProcessEntry32;

begin

result := fproc;

end;



procedure procObject.putProcData(const value: TProcessEntry32);

begin

fproc := value;

end;



procedure procObject.setPriority(const value: integer);

var ProcHandle: THandle;

begin // setter for property Theproc

ProcHandle := OpenProcess(PROCESS_SET_INFORMATION, false, fProc.th32ProcessID);

if not SetPriorityClass(ProcHandle, value) then

ShowMessage('An error occured while trying to change process priority')

else

fProc.pcPriClassBase := value;

if prochandle > 0 then

CloseHandle(ProcHandle);

end;



5. 실행부문( KillExcel을 Excel Process를 없애고자하는 시점에서 호출해준다.)

procedure TFrmDbm_BaseImport.KillExcel;

var

Proc: TProcessEntry32;

proc_Kill: procobject;

Snap: THandle;

procHandle_Kill : THandle;

prochandle: procObject;

tempname: string;

oldindex: integer;

ExitCD: integer;

begin

//Excel이 실행중인지 아닌지 확인하는 작업을 해줄것....

try

Snap := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0); // API in ToolHelp.pas (dcu)

Proc.dwSize := SizeOf(TProcessEntry32);

Process32First(Snap, Proc);

repeat

prochandle := procObject.create(proc);

if length(string(Proc.szExeFile)) > 0 then // check for processes without a description

begin

tempname := proc.szExeFile;

if tempname ='C:PROGRAM FILESMICROSOFT OFFICEOFFICEEXCEL.EXE' then

begin

listbox1.items.addobject(tempname, prochandle); // save the process object to the list



proc_Kill := procObject(listbox1.items.objects[0]); // get handle

procHandle_Kill := 0;

try // and kill process

procHandle_Kill := OpenProcess(PROCESS_TERMINATE, false, proc_Kill.Theproc.th32ProcessID);

TerminateProcess(procHandle_Kill, ExitCd);

except

showmessage('Excel process 를 종료하지 못했습니다. ctr + alt + del key를 눌러서 excel 작업을 종료하십시요 !' +

#13 + 'Error code :' + inttostr(ExitCD));

end;

if procHandle_Kill <> 0 then

CloseHandle(procHandle_Kill);

end;

end;

until (not Process32Next(Snap, Proc));

except

showmessage('An error occurred while reading task information');

end;

end;



2  COMMENTS
  • Profile
    아줌마 2001.09.11 20:28
    최지훈 wrote:

    > //////////////////////////////////////////////////////////////////////////////////

    > // Excel을 create object를 하여 excel을 사용한후에 백그라운드에 살아있는

    > // 불사조같은 excel을

    > // 완존히 보내버리는 ...악~~ 소리없이..조용히..흐흐흐흐흐흐..나쁜넘..

    > //////////////////////////////////////////////////////////////////////////////////

    >

    > 0. 해당폼에 listbox1를 하나추가해줄것..(실행시는 안보이게설정한다.)

    >

    > 1. uses 절에 다음을 포함할것.

    > Tlhelp32;

    >

    > 2. 다음을 상수로 선언해줄것.

    > PROCESS_TERMINATE = $0001; // const for killing a proc

    > PROCESS_SET_INFORMATION = $0200; // const for changing priority

    >

    > 3. 선언부문

    > (********************************************************************************************)

    > procObject = class // class for use with Tlistbox.objects

    > private

    > fProc: TProcessEntry32; // declare all data private for any future changes

    > procedure putProcData(const value: TProcessEntry32);

    > function getProcData: TProcessEntry32;

    > procedure setPriority(const value: integer);

    > function getPriority: Integer;

    > public

    > constructor create(procinfo: TProcessEntry32);

    > published

    > property Theproc: TProcessEntry32 read getProcData write putProcData;

    > property ProcPriority: Integer read getPriority write setPriority;

    > end;

    > (********************************************************************************************)

    >

    > 3. 구현부문

    >

    > constructor procObject.create(procinfo: TProcessEntry32);

    > begin

    > inherited create; // need to specify which inherited constructor to call

    > theproc := procinfo; // since parameter list differ

    > end;

    >

    > function procObject.getPriority: Integer;

    > begin

    > result := fProc.pcPriClassBase;

    > end;

    >

    > function procObject.getProcData: TProcessEntry32;

    > begin

    > result := fproc;

    > end;

    >

    > procedure procObject.putProcData(const value: TProcessEntry32);

    > begin

    > fproc := value;

    > end;

    >

    > procedure procObject.setPriority(const value: integer);

    > var ProcHandle: THandle;

    > begin // setter for property Theproc

    > ProcHandle := OpenProcess(PROCESS_SET_INFORMATION, false, fProc.th32ProcessID);

    > if not SetPriorityClass(ProcHandle, value) then

    > ShowMessage('An error occured while trying to change process priority')

    > else

    > fProc.pcPriClassBase := value;

    > if prochandle > 0 then

    > CloseHandle(ProcHandle);

    > end;

    >

    > 5. 실행부문( KillExcel을 Excel Process를 없애고자하는 시점에서 호출해준다.)

    > procedure TFrmDbm_BaseImport.KillExcel;

    > var

    > Proc: TProcessEntry32;

    > proc_Kill: procobject;

    > Snap: THandle;

    > procHandle_Kill : THandle;

    > prochandle: procObject;

    > tempname: string;

    > oldindex: integer;

    > ExitCD: integer;

    > begin

    > //Excel이 실행중인지 아닌지 확인하는 작업을 해줄것....

    > try

    > Snap := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0); // API in ToolHelp.pas (dcu)

    > Proc.dwSize := SizeOf(TProcessEntry32);

    > Process32First(Snap, Proc);

    > repeat

    > prochandle := procObject.create(proc);

    > if length(string(Proc.szExeFile)) > 0 then // check for processes without a description

    > begin

    > tempname := proc.szExeFile;

    > if tempname ='C:PROGRAM FILESMICROSOFT OFFICEOFFICEEXCEL.EXE' then

    > begin

    > listbox1.items.addobject(tempname, prochandle); // save the process object to the list

    >

    > proc_Kill := procObject(listbox1.items.objects[0]); // get handle

    > procHandle_Kill := 0;

    > try // and kill process

    > procHandle_Kill := OpenProcess(PROCESS_TERMINATE, false, proc_Kill.Theproc.th32ProcessID);

    > TerminateProcess(procHandle_Kill, ExitCd);

    > except

    > showmessage('Excel process 를 종료하지 못했습니다. ctr + alt + del key를 눌러서 excel 작업을 종료하십시요 !' +

    > #13 + 'Error code :' + inttostr(ExitCD));

    > end;

    > if procHandle_Kill <> 0 then

    > CloseHandle(procHandle_Kill);

    > end;

    > end;

    > until (not Process32Next(Snap, Proc));

    > except

    > showmessage('An error occurred while reading task information');

    > end;

    > end;

    >

  • Profile
    아줌마 2001.09.11 19:21
    어찌하여,,,에러가..

    정확히 class를 사용해 본적이 없어서요..

    헝헝..excel은 무조건 죽여야 하구여..

    소스를 공개해 주실수는 없는지요..

    오늘 중으로 마무리를 지어야 하는데,,

    이 excel이 죽지를 않아서리..

    죽어랑..죽어라..그래도, 역시나, 살아서 저를 골탕먹이는 군요..

    부탁합니다.

    전화를 주셔도 좋고,

    메일을 주셔도 좋습니다.



    그럼, 20000

    Tel 051-203-9001 신현숙

    angel71s@chol.net