×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help with validating a code block

Help with validating a code block

Help with validating a code block

(OP)
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 

RE: Help with validating a code block

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 Smileing

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.

RE: Help with validating a code block

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
https://www.doschke.name

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close