Pergunta

I am using one file to send data to global variables in a globals file and having another file try and retrieve that data from the globals file after.

Example:

Main file sends data to global file and other file gets data from global file

For some reason the data isn't being preserved. When I pull the data from the global file it's not there. Why is this?

Here is some example code:

;main.au3
#include-once
#include "global.au3"


$g1 = "stuff"
$g2 = "stuff"

Run("copy.exe")

;global.au3
#include-once

Global $g1 = ""
Global $g2 = ""

;copy.au3
#include-once
#include "global.au3"

MsgBox(0, $g1, $g2)

main.au3 and copy.au3 have been built into .exe and I double click on main to run it.

The result: $g1 and $g2 are equal to "" and "" when it should be printing "stuff"

Foi útil?

Solução

This is because global.au3 doesn't get updated. It's providing initial values to your variables. Using #include "global.au3" is the same as writing:

;main.au3
#include-once
Global $g1 = ""
Global $g2 = ""

$g1 = "stuff"
$g2 = "stuff"

Run("copy.exe")

and

;copy.au3
#include-once
Global $g1 = ""
Global $g2 = ""

MsgBox(0, $g1, $g2)

As you can see, copy.au3 (copy.exe) has $g1 and $g2 set to "".

To pass your $g1 and $g2 variables to copy.exe, you can either use IniRead()/IniWrite() to read/write an .ini file or pass the variables as command line parameters.

Here's an example of the command line parameters option:

(no global.au3 needed; compile main.au3 and copy.au3; double click main.exe to run)

main.au3

;main.au3

$g1 = "stuff"
$g2 = "stuff"

ShellExecute('copy.exe', $g1 & ' ' & $g2)

copy.au3

;copy.au3

$g1 = $CmdLine[1]
$g2 = $CmdLine[2]

MsgBox(0, $g1, $g2)

I used ShellExecute() instead of Run() because Run() has been a pain in the a$$ in the past (for me). I don't usually have any issues with ShellExecute() and ShellExecuteWait().

Also, for more info on command line parameters you can search on "Command Line Parameters" in the AutoIt help.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top