I see a potencial problem. You changed the name of the process to find to
lower case. JScript is like dBASE in that case must be the same to find a
match.
so "PLUS.exe" may not equil to "plus.exe". I suggest changing the if
statement to use the same case all the time.
Rich...
Post by wagnerPost by unknownOn Thu, 16 Oct 2008 14:08:28 -0400 Wagner
Newsgroup: dbase.programming
Post by WagnerHow do you know the number of applications that are running.
For example, if I run the WordPad.exe twice, as in my application dBase
know that this WordPad.exe running two copies (stays)
Try the code below my signature. It is based upon code published by Rich,
Autotracker. I modified it
to filter out only the exe searched for.
getProcess("WordPad.exe")
The way I parse the result is not pretty, but I did not have time to find
a more elegant way, after
all, it is only an example :-)
Ivar B. Jessen
//-----
function getProcess(cProcess)
cProcess = lower(cProcess)
if file( "pTable.dbf")
drop table pTable
endif
if file("ans1.txt")
delete file "ans1.txt"
endif
create table pTable(fProcess char(15), fProcID integer)
#define vbCrLf chr(13)+chr(10)
extern CLONG GetCurrentProcessId( CVOID ) kernel32
plusHandle = str(GetCurrentProcessID())
sc = new OleAutoClient("MSScriptControl.ScriptControl")
sc.language = "JScript"
str = [function getProcesses(){] + vbCrLf
// Get Object and create enumerator
str += [ var e = new Enumerator( GetObject(
"winmgmts:" ).InstancesOf("Win32_process" ) );] +
vbCrLf
// Variable to hold the info we find
str += [ var msg = "" + "\n";] + vbCrLf
// Loop through processes
str += [ for (;!e.atEnd();e.moveNext())] + vbCrLf
str += [ {] + vbCrLf
// Get object from enumerator
str += [ var p = e.item ();] + vbCrLf
// Add process based on name match
str += [ if ( p.Name == ']+cProcess+[' ) {;] + vbCrLf
// Format message with process name and handle
str += [ msg += p.Name + "," + p.Handle + "\n";] + vbCrLf
str += [ }] + vbCrLf
str += [ }] + vbCrLf
// Return the process names and handles
str += [return msg;] + vbCrLf
str += [}] + vbCrLf
sc.addCode( str )
ans = sc.run("getProcesses")
release object sc
sc = NULL
set alternate to ans1.txt
set alternate on
set console off
? ans
set console on
set alternate off
set alternate to
use pTable
append from ans1.txt delim
use
select count(*) as nNr from pTable where fProcess is not null
titleString = "Searching for " + cProcess
msgString = "There " + iif(nNr > 1 or nNr = 0, "are ", "is ") + ;
nNr + " open " + iif(nNr > 1 or nNr = 0, "instances", "instance") + ;
" of " + cProcess
msgbox(msgString, titleString, 0)
use
drop table pTable
delete file "ans1.txt"
//-----
I took the liberty to do some changes
=======================
function getProcess(cProcess)
local plusHandle,sc,str,ans
cProcess = lower(cProcess)
if substr(cProcess,len(cProcess)-3) <> '.exe'
cProcess += '.exe'
endif
#define vbCrLf chr(13)+chr(10)
extern CLONG GetCurrentProcessId( CVOID ) kernel32
plusHandle = str(GetCurrentProcessID())
sc = new OleAutoClient("MSScriptControl.ScriptControl")
sc.language = "JScript"
str = [function getProcesses(){] + vbCrLf
// Get Object and create enumerator
str += [ var e = new Enumerator( GetObject(
"winmgmts:" ).InstancesOf("Win32_process" ) );] + vbCrLf
// Variable to hold the info we find
str += [ var msg = "" + "\n";] + vbCrLf
// Loop through processes
str += [ for (;!e.atEnd();e.moveNext())] + vbCrLf
str += [ {] + vbCrLf
// Get object from enumerator
str += [ var p = e.item ();] + vbCrLf
// Add process based on name match
// str += [ if ( p.Name == ']+cProcess+[' ) {;] + vbCrLf
// Format message with process name and handle
str += [ msg += p.Name + "," + p.Handle + "\n";] + vbCrLf
// str += [ }] + vbCrLf
str += [ }] + vbCrLf
// Return the process names and handles
str += [return msg;] + vbCrLf
str += [}] + vbCrLf
sc.addCode( str )
ans = sc.run("getProcesses")
ans = lower(ans)
release object sc
sc = NULL
? cProcess
RETURN nsub(cProcess,ans)
/*=============================================================================
FUNCAO : nsub( expC1,expC2 )
OBJETIVO : numero de vezes que uma sub-string se repete em uma string.
ENTRADA : expC1 = sub_string a ser procurada
expC2 = string a onde ocorrera a busca
RETORNO : expN = retorna quantidade de ocorrencias.
=============================================================================*/
function nsub(pSubString,pString)
local tamanho,String,SubString
String = pString
SubString = pSubString
tamanho = len( string )
string = strtran( string , SubString )
return int(( tamanho - len( String ) ) / len(SubString))