Jump to content

Global variables aren't working in #include


Recommended Posts

I'm getting an error in this script:

Opt("WinWaitDelay",100)
Opt("WinTitleMatchMode",4)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)
TrayTip("clears any tray tip","",0)
AutoItSetOption("TrayIconDebug", 1) ;0-off

#include "OSVersion.au3"

Run('C:\Dnload\9xAddons\TestThis.exe')
MsgBox("","Test","$ProgramFiles - " & $ProgramFiles & @CRLF & "_OSVersion() - " & _OSVersion())

Exit

This is the error I am getting:

TestThis.thumb.jpg.88dffa00a7e14c94c33d8765d9b98a70.jpg

The variables should be defined in the #include "OSVersion.au3"

Func _OSVersion()
    Global $OSVersion
    Local $sWinVer = FileGetVersion('winver.exe')
    $OS_Version = @OSVersion

    $servicepack_version = StringReplace(@OSServicePack, "Service Pack ", "SP")
    If @error = -1 Then $servicepack_version = ""
    ConsoleWrite($OS_Version & $servicepack_version & StringRegExpReplace(@OSArch, '(X86)', '') & @CRLF)
    ConsoleWrite(@OSArch & @CRLF)
    If $OSVersion = 'Win7' Or $OSVersion = 'Win7SP1' Or $OSVersion = 'Win7X64' Or $OSVersion = 'Win7SP1x64' Then
        $ProgramFiles = "C:\Programs\"
    Else
        $ProgramFiles = "C:\Program Files\"

    EndIf

    Return $OS_Version & $servicepack_version & StringRegExpReplace(@OSArch, '(X86)', '')
EndFunc   ;==>_OSVersion

What do I need to do to resolve the error?

Thanks,

Docfxit

Link to comment
Share on other sites

your setting the global variable inside a function, so it won't exist until after you call that function.

Try moving that Global $OSVersion to that top of your "OSVersion.au3" file.  it shouldn't throw the error anymore, but it will be blank until after you call the function so I'm not sure if it would show in your MsgBox.

Link to comment
Share on other sites

Thanks for the reply...

I tried this in "OSVersion.au3":

Global $OSVersion, $ProgramFiles

Func _OSVersion()
    Local $sWinVer = FileGetVersion('winver.exe')
    $OS_Version = @OSVersion

    $servicepack_version = StringReplace(@OSServicePack, "Service Pack ", "SP")
    If @error = -1 Then $servicepack_version = ""
    ConsoleWrite($OS_Version & $servicepack_version & StringRegExpReplace(@OSArch, '(X86)', '') & @CRLF)
    ConsoleWrite(@OSArch & @CRLF)
    If $OSVersion = 'Win7' Or $OSVersion = 'Win7SP1' Or $OSVersion = 'Win7X64' Or $OSVersion = 'Win7SP1x64' Then
        $ProgramFiles = "C:\Programs\"
    Else
        $ProgramFiles = "C:\Program Files\"

    EndIf

    Return $OS_Version & $servicepack_version & StringRegExpReplace(@OSArch, '(X86)', '')
EndFunc   ;==>_OSVersion

Moving Global $OSVersion outside the function and adding $ProgramFiles fixed the problem.

 

Edited by Docfxit
Link to comment
Share on other sites

  • Moderators

You moved the Global to the top, but are still declaring it Globally first line in your function....

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

2 minutes ago, JLogan3o13 said:

You moved the Global to the top, but are still declaring it Globally first line in your function....

Thanks for noticing that.  I removed it from the function.

I'm still getting the same error.

 

Link to comment
Share on other sites

Moving Global $OSVersion outside the function and adding $ProgramFiles fixed the problem.

 

Thanks a bunch guys.  You saved my day.

I have the include file inside a ton of scripts.  Now I need to figure out a way to remove that code from a ton of scripts and add #include to them.

Thanks,

Docfxit

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

×
×
  • Create New...