Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function handles diff arguments
#1
I'm making a replacement function for wait that asks to retry. It works fine except I would like to use different arguments (such as pass a window name or a handle) depending on the situation. How do I define the function so that it will accept either argument based on how it is called?

For example, here is my code... it fails if i pass it a string (ie: WaitFor2 "Window Name") it fails with "Error in test_waitfor2: expected numeric expression.";
Code:
Copy      Help
/
function [^waitmax] [hwnd] [str's] ;;waitmax: 0 is infinite.

Function WaitFor is template to create functions that wait for some condition.

waitmax is max number of seconds to wait. 0 is infinite.

int w=0
int n
str s2    

if(waitmax<0 or waitmax>2000000) end ES_BADARG
opt waitmsg -1

int wt(waitmax*1000) t1(GetTickCount)
rep
    rep
        0.1
        
         here insert code that checks for some condition
         and returns (ret or break) if it is true
        ;try to activate the window. if successful, exit function
        if hwnd>0
            w=act(hwnd)
        else
            w=act(win(s))
            err
        if (w>0)
            break
            
        if(wt>0 and GetTickCount-t1>=wt)
            break
            
    if (w>0)
        break
    
    ;if timeout, then bring up dialog to continue waiting or quit (indicate what we are waiting for)
    if s=""
        s2.from("Timed out waiting for: " s)
    else
        s2="Timed out"
    n=mes(s2 "" "ARI2!")
    if (n='A')                 ;;abort
        end "WaitFor2 timeout"
    if (n='R')                ;;retry (wait only 5 seconds)
        wt=5000
        t1=GetTickCount    
    if (n='I')                 ;;ignore
        break                
ret

By the way, this would be a nice default feature for the wait function to have!

Thank you
#2
Use VARIANT type. It can store value of different types.

Use this function to convert VARIANT to window handle.
Function WindowHandleFromVARIANT
Code:
Copy      Help
;/
function# VARIANT'window_

;Converts any window expression (handle, name, etc) to window handle.
;Returns handle.
;Error if failed.

;REMARKS
;If window_ contains integer (VT_I4), it is interpreted as window handle.
;If window_ contains string (VT_BSTR), it is interpreted as window name or +class. If empty string - as currently active window.
;Error if window_ contains some other type.


int hwnd

sel window_.vt
,case VT_I4 ;;hwnd
,hwnd=window_.lVal
,if(!IsWindow(hwnd)) end "invalid window handle"
,
,case VT_BSTR ;;window name etc
,str s=window_.bstrVal
,if(!s.len) hwnd=win
,else if(s[0]='+') hwnd=win("" s+1)
,else hwnd=win(s)
,if(!hwnd) end "window not found"
,
,case else end "incorrect argument"

ret hwnd

err+ end _error

example
Function Function_window_name_or_handle
Code:
Copy      Help
;/
function VARIANT'window_

int hwnd=WindowHandleFromVARIANT(window_); err end _error
outw hwnd
#3
Thank you so much for the information!


Forum Jump:


Users browsing this thread: 1 Guest(s)