Jump to content

Easy Question about Getting Values from a Function


Recommended Posts

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

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...