ViciousXUSMC

Easy Question about Getting Values from a Function

5 posts in this topic

#1 ·  Posted

Ok so I am sure this is a easy one that I just can't figure out.

So ProcessExists() can be used to see if a process exist, and it also returns the value of the PID of that process.

Trying to keep a script short I wanted to do something like this.

If ProcessExists("Notepad.exe") Then
    $sBinary = "Notepad.exe"
ElseIf ProcessExists("WinWord.exe") Then
    $sBinary = "WinWord.exe"
EndIf

But I also need the PID in another variable.

I could right after my Then put $iPID = @Error or @Extended to capture those return values and save them to a variable, is there a way to do something similar for non error values?

Else I need to add more lines to the code that feels kinda sloppy and does the work twice.

If ProcessExists("Notepad.exe") Then
    $sBinary = "Notepad.exe"
    $iPID = ProcessExists("Notepad.exe")
ElseIf ProcessExists("WinWord.exe") Then
    $sBinary = "WinWord.exe"
    $iPID = ProcessExists("WinWord.exe")
EndIf

 

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think you might be able to do this:

If $iPID = ProcessExists("Notepad.exe") Then
    $sBinary = "Notepad.exe"
ElseIf $iPID = ProcessExists("WinWord.exe") Then
    $sBinary = "WinWord.exe"
EndIf
Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

anthonyjr2,
$iPID = undeclared variable  :) and confusion comparison/assignment ?

Why not

Local $iPID, $sBinary, $var
$var = ProcessExists("Notepad.exe")
If $var Then 
   $iPID = $var
   $sBinary = "Notepad.exe"
EndIf
$var = ProcessExists("WinWord.exe") 
If $var Then 
   $iPID = $var
   $sBinary = "WinWord.exe"
EndIf

 

Edited by mikell

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Just for fun.

#include <Array.au3>
Local $aArray[] = [ProcessExists("notepad.exe") ? ProcessExists("notepad.exe") : ProcessExists("WinWord.exe"),( ProcessExists("notepad.exe") ? "notepad.exe" : (ProcessExists("WinWord.exe") ? "WinWord.exe" : ""))]
_ArrayDisplay($aArray)

Saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites

#5 ·  Posted

Mikels way is the clean way I was trying to see if I could get around since I had to declare each as a variable, and originally in my code I had an Else as well so it was If, ElseIf, and Else.

The target question was, more or less is there a hidden @Return type thing I did not know about or some simple syntax I was missing.

For this particular scenario the best solution was simply to run the check twice like I showed in the 2nd example, I did not want complicated hard to follow code and I did not want a lot of lines.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now