Jump to content

Recommended Posts

Posted (edited)

Hello AutoIters :)

I recently working on a script interfacing with a old Java program which does not provide API. The code as follows:

;a piece of code in main program
$result = get_token()
;get token return a int here
If $result == 0 Then
    ;code omitted here
Else
    logging("DEBUG: result is: " & $result)
    ;got token, start waiting
    wait_for_activation()
    logging("DEBUG: result after waiting is: " & $result)
    IniWrite("Data/" & $index[$i_index][1], "profile", "token", $result)
EndIf

;wait_for_activation
Func wait_for_activation()
    $activated = 0
    Local $temp[2]
    While $activated == 0
        $activated = _ImageSearch("lib/activate.bmp", 1, $temp[0], $temp[1], 40)
        Sleep(5000)
    WEnd
EndFunc   ;==>wait_for_activation

The Java program uses a GUI library that can't be handled by Autoit using window infos. (autoit can't see the content inside the window) So I resort to the _ImageSearch library and read screen directly.

The activation process takes really long, usually around 5~10 hours. But the weird thing is I can't find the token number in the ini output. and in the log file it's always

DEBUG: result is: 3
DEBUG: result after waiting is:

It seems the value of $result is lost after waiting. I can't tell the reason.

Can anyone kindly tell me where the problem is?

Thanks in advance.

Edited by xbtsw
Posted

It seems the value of $result is lost after waiting. I can't tell the reason.

It's because of "improper" design by the UDF writer and you. You are declaring $result as global, and the UDF stores it's result there.

The UDF $result should be local. Also, both of you are using unnecessarily "easy" variable names.

An example: If the UDF var was called $aiImageSearchResultDllCall this would probably not have happened, even without specifying local.

Posted (edited)

It's because of "improper" design by the UDF writer and you. You are declaring $result as global, and the UDF stores it's result there.

The UDF $result should be local. Also, both of you are using unnecessarily "easy" variable names.

An example: If the UDF var was called $aiImageSearchResultDllCall this would probably not have happened, even without specifying local.

Thanks for the reply, that's indeed where the problem is.

Can I take the chance to ask one more scope related question:

If I declared in global level of a au3 file using "Local", will the scope of the variable is the "File scope" or it's actually global even it's declared as local? Is there a "File scope" idea in autoit?

Suppose I have a UDF "testglobal.au3" as following:

#include-once

Local $amI_Global=1

and include.au3 as following:

#include "testglobal.au3"

ConsoleWrite($amI_Global & @CRLF)
;will I get 1 here?
Edited by xbtsw
Posted (edited)

There's no file scope. It's either local to the function or global.

Writing "Local $Something" ouside of a function will get you a Global

Local $abc = "local outside a function makes a global"

_A()

Func _A()
    MsgBox(0, "", $abc)
EndFunc

And while we are at it, never use Dim. Better to Specify Global or Local, less confusion and much easier to read the code later.

Edit:

;will I get 1 here?

Why don't you run it and see? :) Edited by AdmiralAlkex

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
  • Recently Browsing   0 members

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