Sytze:
This is what I use:
I am fairly sure this was found in the Universal Thread
Jack

lparameter lcProcess_to_kill
lnProcID = ProcID(lcProcess_to_kill)
if lnProcID <> 0 then
  lnResult = KillProcID(lnProcID)
  return lnResult
else
  return lnProcID
endif
******************************************************************************************************
function ProcList(taProcess)
  if pcount() = 0 then
 *** Nothing to do
 return 0
  endif
  local laProcess, hSnapShot, lnBufSize, lcProcInfo, lnResult, lnProcID, lnRow
  laProcess = taProcess
  if type("&laProcess") = "U" then
    public array &laProcess[1]
  else
    release &laProcess
    public array &laProcess[1]
  endif
  *** declare DLLs needed for the case
  declare integer CreateToolhelp32Snapshot in kernel32 integer dwFlags, integer 
th32ProcessID
  declare integer Process32First in kernel32 integer  hSnapshot, string @ lppe
  declare integer Process32Next in kernel32 integer hSnapshot, string @ lppe
  declare integer CloseHandle in kernel32 integer hObject
  *
  #define TH32CS_SNAPPROCESS 0x00000002
  *** Obtain a handle to the thread's snapshot
  hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  lnBufSize = 550
  lcProcInfo= num2dword(lnBufSize) + replicate(chr(0), lnBufSize -32)
  *** Get the first process
  lnResult = Process32First(hSnapShot, @lcProcInfo)
  lnProcID = 0
  lnRow = 0
  *** Now loop through the rest of processes
  do while lnResult <> 0
    lnRow = lnRow + 1
    dimension &laProcess(lnRow,5)
    *** Extract info
    &laProcess(lnRow,1) = buf2dword(substr(lcProcInfo,9,4))
    &laProcess(lnRow,2) = buf2dword(substr(lcProcInfo,21,4))
    &laProcess(lnRow,3) = buf2dword(substr(lcProcInfo,25,4))
    &laProcess(lnRow,4) = buf2dword(substr(lcProcInfo,29,4))
    &laProcess(lnRow,5) = strtran(substr(lcProcInfo,37),chr(0),"")
    *** Reinit lcProcInfo just "FileName" part so in XP file name does not get 
cluttered
    *** and in Win9x & ME could do the "Process32Next" corectly
    lcProcInfo= substr(lcProcInfo,1,36) + replicate(chr(0), lnBufSize - 36)
    lnResult = Process32Next(hSnapShot, @lcProcInfo)
  enddo
  CloseHandle(hSnapShot)
  return lnRow
endfunc &&* ProcList
******************************************************************************************************************************
function ProcID(tcProcessName)
  if pcount() = 0 then
    *** Nothing to do
    return 0
  endif
  if type("tcProcessName") <> "C" then
    return 0
  endif
  local lcProcessName, lnBufSize, lcProcInfo, lnResult, lnProcID, 
th32DefaultHeapID
  local cntUsage, th32ProcessID, th32ModuleID, cntThreads, th32ParentProcessID, 
pcPriClassBase, dwFlags, szExeFile
  lcProcessName = alltrim(tcProcessName)
  if empty(lcProcessName) then
    return 0
  endif
 *** declare DLLs needed for the case
  declare integer CreateToolhelp32Snapshot in Kernel32 integer dwFlags, integer 
th32ProcessID
  declare integer Process32First in kernel32 integer hSnapshot, string @lpPE
  declare integer Process32Next in kernel32 integer hSnapshot, string @ lpPE
  declare integer CloseHandle in kernel32 integer hObject
  #define TH32CS_SNAPPROCESS   0x00000002
  hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  lnBufSize = 550
  lcProcInfo = num2dword(lnBufSize) + replicate(chr(0), lnBufSize -32)
  *** Get the first process
  lnResult = Process32First(hSnapShot, @lcProcInfo)
  lnProcID = 0
  *** Cycle through the rest of the processes until the one in ? found (or 
not...)
  do while lnResult <> 0
    *** We will use only the th32ProcessID and szExeFile, but will extract all 
just in case...
    th32ProcessID = buf2dword(substr(lcProcInfo, 9, 4))
    szExeFile = strtran(substr(lcProcInfo,37), chr(0), "")
    *** Check if we have found the process in ?
    if atc(lcProcessName, szExeFile) <> 0 then
      lnProcID = th32ProcessID
      exit  && do while...enddo cycle
    endif
    *** Reinit lcProcInfo just "FileName" part so in XP file name does not get 
cluttered
    ***   and in Win9x & ME could do the "Process32Next" corectly
    lcProcInfo= substr(lcProcInfo,1,36) + replicate(chr(0), lnBufSize - 36)
    lnResult = Process32Next(hSnapShot, @lcProcInfo)
  enddo
  CloseHandle(hSnapShot)
  *clear dlls CreateToolhelp32Snapshot, Process32First, Process32Next, 
CloseHandle  &&this does not work...it clears all defined DLLs...
return lnProcID
endfunc  &&ProcID
******************************************************************************************************************************
function KillProcID(tnProcID)
  if pcount() = 0
    *** Nothing to do
    return 0
  endif
  if type('tnProcID') <> "N" then
    return 0
  endif
  local lnProcID, hProcess, lnResult
  if tnProcID = 0 then
    *** Nothing to do...
    return 0
  else
    lnProcID = tnProcID
  endif
  declare integer OpenProcess in Kernel32 integer dwDesiredAccess, integer 
bInheritHandle, integer dwProcId
  declare integer TerminateProcess in Kernel32 integer hProcess, integer 
uexitCode
  declare integer CloseHandle in Kernel32 integer hObject
  #define PROCESS_TERMINATE 1
  *** Obtain the handle to the process first
  hProcess = OpenProcess(PROCESS_TERMINATE, 0, lnProcID)
  *** if the process found running - "shoot it!"
  lnResult = 0
  if hProcess <> 0 then
    lnResult = TerminateProcess(hProcess, 0)
  endif
  CloseHandle(hProcess)
  *clear dlls OpenProcess, TerminateProcess, CloseHandle
  return lnResult
endfunc  &&KillProcID
******************************************************************************************************************************
** function Name : Buf[fer] 2 DWORD
** Purpose       :
** Description   :
** Parameter(s)  : Buffer as a Char*4 string
** return        : DWORD type imitation as LONG integer
** Side Effect(s):
** Notes:        :
******************************************************************************************************************************
function Buf2DWORD(tcBuffer)
  if type('tcBuffer') <> "C" then
    return 0
  endif
  tcBuffer = left(alltrim(tcBuffer), 4)
  local I, lnRet
  lnRet = asc(substr(tcBuffer, 1, 1))
  for i = 1 to 3
    lnRet = lnRet + asc(substr(tcBuffer, I + 1, 1)) * 256^I
  endfor
  return int(lnRet)
endfunc  &&Buf2DWORD
******************************************************************************************************************************
function Num2DWORD(tnValue)
  #define m0       256
  #define m1     65536
  #define m2  16777216
  local b0, b1, b2, b3
  b3 = int(tnValue / m2)
  b2 = int((tnValue - b3 * m2) / m1)
  b1 = int((tnValue - b3 * m2 - b2 * m1) / m0)
  b0 = mod(tnValue, m0)
  return chr(b0) + chr(b1) + chr(b2) + chr(b3)
endfunc  &&Num2DWORD
******************************************************************************************************************************



Jack Skelley
Senior Director, Programming/Computer Operations
New Jersey Devils
(973)757-6164
[email protected] ________________________________________
From: ProfoxTech [[email protected]] on behalf of Sytze de Boer 
[[email protected]]
Sent: Sunday, April 13, 2014 8:27 PM
To: [email protected]
Subject: Killing a program

I have this ancient program which uses Wordpad and the append general
process
This has worked fine for 20 years but lately is playing up where, during
the printing process, you can see it places Word.exe in the task bar

This causes a problem where the template INV.DOC is held open by Word
I thought the solution would be simple by killing word but this works
sometimes, but not always.
I do this

PARAMETERS whatprg
*lcProcess = "IEXPLORE.EXE"  && Process to be closed
lcProcess=whatprg+".EXE"
lcComputer = "."
loWMIService = GETOBJECT("winmgmts:" +
"{impersonationLevel=impersonate}!\\" + lcComputer + "\root\cimv2")
colProcessList = loWMIService.ExecQuery ("Select * from Win32_Process")

IF TYPE('colProcessList') = "O"
  FOR EACH loProcess IN colProcessList
     IF ALLTRIM(UPPER(loProcess.NAME)) == ALLTRIM(UPPER(lcProcess))
        loProcess.TERMINATE()
      ENDIF
  NEXT
ENDIF  && IF TYPE('colProcessList') = "O"
RELEASE colProcessList, loWMIService

Is there a better way ?

--
Kind regards
Sytze de Boer
Kiss Software


--- StripMime Report -- processed MIME parts ---
multipart/alternative
  text/plain (text body -- kept)
  text/html
---

[excessive quoting removed by server]

_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/profox
OT-free version of this list: http://mail.leafe.com/mailman/listinfo/profoxtech
Searchable Archive: http://leafe.com/archives/search/profox
This message: 
http://leafe.com/archives/byMID/profox/7d9e7f72b813014c8fd022cf04f820edf36b2...@ex06.drdad.thenewarkarena.com
** All postings, unless explicitly stated otherwise, are the opinions of the 
author, and do not constitute legal or medical advice. This statement is added 
to the messages for those lawyers who are too stupid to see the obvious.

Reply via email to