Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with validating a code block

Status
Not open for further replies.

AlastairP

Technical User
Feb 8, 2011
286
AU
I have some code that I use to terminate processes, and occasionally I get an error.

The error is "Alias LoProcess can't be found" or similar
I would like to get this to run without an error if possible.
Noted is that if I get the users to reboot their windows tablets, it will run fine.
Any help would be greatly appreciated


Code:
LPARAMETERS lcProcess
lcFail=.f.
lcComputer = "."
loWMIService = Getobject("winmgmts:" ;
    + "{impersonationLevel=impersonate}!\\" + lcComputer + "\root\cimv2")
colProcessList = loWMIService.ExecQuery ;
    ("Select * from Win32_Process")
    
FOR EACH loProcess IN colProcessList
   IF ALLTRIM(UPPER(loProcess.name)) == ALLTRIM(UPPER(lcProcess))
		TRY 	   
    		loProcess.terminate()
    	CATCH 
     		lcFail=.t.
		ENDTRY 
		IF lcFail=.t.
     		EXIT
		ENDIF 	
   ENDIF 
NEXT
 
I think your error is occurring on one of the lines

Code:
IF ALLTRIM(UPPER(loProcess.name)) == ALLTRIM(UPPER(lcProcess))
or
Code:
loProcess.terminate()

The second of which is tucked in a try...catch setting so shouldn't throw an error - thus it seems VFP is seeing
loProcess.name as an alias call to a table called loProcess, not an object.

So, I would test for the type of loProcess.name before comparing it with a string in the IF above

Code:
IF Type("loProcess.name") = "C"
        IF ALLTRIM(UPPER(loProcess.name)) == ALLTRIM(UPPER(lcProcess))



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
This is correctly analyzed, but indeed if loProcess isn't an object, you shouldn't even get to the line. as the FOR EACH loProcess IN colProcessList will make it an object. and if none exists in colProcessList your computer won't run any processes, that's never.

So that'd need a failure in the colProcessList collection.

Maybe this: If you loop multiple times and there are some termination request pending colProcessList might have processes that turn to NULL while you FOR EACH them. But even for loProcess=NULL loProcess still is a defined variable, that would just cause the error "LOPROCESS is not an object". It needs to be undefined before that code would look for an alias loProcess, a field loProcess and finally a variable loProcess.

In detail, behind the scenes VFP maintains a nametable for all things having names (no, not everything, eg not files, but any variable, object, alias field) and it prioritises looking for field names. "VARIABLE ... not found" should really be named to "NAME ... not found".

I see no way that's happening, other than a colProcessList item getting removed while this loop runs.

The consequence is the same recommendation: First check IF VARTYPE(loProcess)="O" as a defense against that error. It should never happen, but when it happens that's the thing to check. It could still happen if loProcess is released in the next moment. But it could help skip orphaned colProcessList items. The process list is in change all the time, not just by your own terminate calls. System processes are spawned and end.

So another fine way to address this could be to modify your query: Select * from Win32_Process. You already know which name you look for, that can be put as WHERE clause there: Select * from Win32_Process WHERE name = '&lcProcess'

You can't parameterize this as WHERE name = ?m.lcProcess, so also perhaps make sure lcProcess is passed in and the correct type 'C' and not empty and not containing unallowed characters. Process names will have no single quote inside them, for example.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top