Sign in to follow this  
Followers 0
VinitMeta

AutoIt Error- "Variable used without being declared"

9 posts in this topic

#1 ·  Posted (edited)

Here is another similar problem> for following code i am getting error as "Variable used without being declared"

The variable for which i am getting this error is $LogPath in function Func WriteLog($strText) which i have already defined as Global.

Edited by VinitMeta

Share this post


Link to post
Share on other sites



@VinitMeta...

First of all always wrap any code using the code tag like this:

#Region Include files
#include "Common.au3"
#EndRegion Include files

#NoTrayIcon

#Region Parameter Explaination
#comments-start
;Input Parameters
;1. psexecPath = Path of psexec Utility.
;2. BaseMachine = Machine Name or Ip address Without Backslash
;3. UserName = UserName under which this command need to be execute
;4. Password = Corrosponding password for base Machine.
;5. VMstartUtilityPath = File to be Execute remotely. This File must be presnt on local system.
; The file contsionas VMRun command to start the virtual Machine.
;6. VMImageToLoad = Image path for virtual Machine.
#comments-End
#EndRegion

Global $Result = 1
Global $psexecCommnd_FileName
Global $BaseMachine
Global $UserName
Global $Password
Global $ExecuteFile
Global $ImagePath
#Region OnAutoItStart
Func OnAutoItStart()
#comments-start
Global $psexecCommnd_FileName = ""
Global $BaseMachine= ""
Global $UserName= ""
Global $Password= ""
Global $ExecuteFile= ""
Global $ImagePath= ""
#comments-end

SetEnv("AUTODEPLOYMENT_STATUS","NOTSTARTED")


$exitCode = 0
BeginLog()

if $CmdLine[0] < 6 then
WriteLog("Invalid Arguments")
Else
WriteLog("Calling ParseInput()")
Call("ParseInput")

WriteLog("Calling StartVirtualMachine()")
call ("StartVirtualMachine")
EndIf
EndFunc
#EndRegion OnAutoItStart




;Stores Command line parameters And create comman to start the Vitrual machine on given host.
Func StartVirtualMachine()
LogFuncStart("StartVM->StartVirtualMachine")

WriteLog("Calling ValidateParameters()")
$Valid = call("ValidateParameters")

If $Valid == False Then
Exit
EndIf

WriteLog(StringLeft($BaseMachine,2))
if StringLeft($BaseMachine,2) == "\\" then
$BaseMachine = StringTrimLeft($BaseMachine,2)
EndIf


;Execute Command to start Virtual Machine.
WriteLog("Invoking ShellExecuteWait() with Parameter" & $psexecCommnd_FileName &' \\' &$BaseMachine & ' -i -u ' & $UserName & ' -p ' &$Password & ' -c "' &$ExecuteFile &'" "' &$ImagePath)
$StartVM = ShellExecuteWait($psexecCommnd_FileName,' \\' &$BaseMachine & ' -i -u ' & $UserName & ' -p ' &$Password & ' -c "' &$ExecuteFile &'" "' &$ImagePath& '"','','',@SW_MAXIMIZE)

WriteLog("Return code is "&$StartVM &" Error Code is - "& @error)

;if Both the codes are zero means command executed successfully.
if $StartVM == 0 AND @error == 0 Then

SetEnv("AUTODEPLOYMENT_STATUS","STARTVM")
WriteLog("Virtual Machine startup SUCCSSS")

else

SetEnv("AUTODEPLOYMENT_STATUS","FAILED")
WriteLog("Virtual Machine startup FAILED")

EndIf
EndFunc

;Function to validate the parameters.
Func ValidateParameters()
$Valid = True
LogFuncStart("StartVM->ValidateParameters")

;Check if psexec.exe file path is valid.
if (FileExists($psexecCommnd_FileName) ==0 ) Then
WriteLog("psexec is not available at specified path")
$Valid = False
EndIf

;Check if base machine is reachable.
if ($Result = Ping($BaseMachine)) == 0 Then
WriteLog("Host not Reachable Error Code Returned is - " & @error)
$Valid = False
EndIf

;Check if Usrname is empty string
if not StringLen($UserName) > 0 Then
WriteLog("User Name can not be blank")
$Valid = False
EndIf

;check if password is empty
if not StringLen($Password) > 0 Then
WriteLog("Password can not be blank")
$Valid = False
EndIf

;Check if file to be execte on remote system is presnt.
if Not FileExists($ExecuteFile) Then
WriteLog("File to execute is not available. Please check the Path specified. ("&$ExecuteFile&")")
$Valid = False
EndIf

;check if image path is defined.
if StringLen($ImagePath) > 0 Then
WriteLog("Please specify the path of Image to load")
$Valid = False
EndIf

return $Valid
EndFunc


;Function to Parse the Input parametrs.
Func ParseInput()
Dim $VarTemp
FOR $element IN $CmdLine
$VarTemp = StringSplit($element,"=",1)

Switch($VarTemp[1])
case "UserName"
$UserName = $VarTemp[2]; UserName Under which one want to execute command remotely
WriteLog("UserName - "&$UserName)

Case "password"
$Password = $VarTemp[2]; Corrosponding Password.
WriteLog("password - ")

Case "BaseMachine"
$BaseMachine = $VarTemp[2]; Remote Machine on which command is to executed.
WriteLog("BaseMachine - "&$BaseMachine)

Case "psexecPath"
$psexecCommnd_FileName = $VarTemp[2]; Complete Path of psexec tool.
WriteLog("psexecPath - "&$psexecCommnd_FileName)

case "VMstartUtilityPath"
$ExecuteFile = $VarTemp[2]; File to be execute on remote syste. This file contains command to be exec.
WriteLog("VMstartUtilityPath - "&$ExecuteFile)

Case "VMImageToLoad"
$ImagePath = $VarTemp[2]; Path of the image to be load to start Virtual Machine.
WriteLog("VMImageToLoad - "&$ImagePath)
EndSwitch
NEXT
EndFunc

Second copy and paste the exact error which u see in Scite.

Share this post


Link to post
Share on other sites

@VinitMeta...

Where is Common.au3 file? no one can check this code because there is missing UDFs and probably some more variables.


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

The variable for which i am getting this error is $LogPath in function Func WriteLog($strText) which i have already defined as Global.

You declare it at the end of script, you should declare global variables at the begining, because when you call functions at the begining, the variable does not declared yet as Global.

#include-once
#include <File.au3>

.....

Global $LogLevel
Global $LogPath

.....

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here is another similar problem> for following code i am getting error as "Variable used without being declared"

The variable for which i am getting this error is $LogPath in function Func WriteLog($strText) which i have already defined as Global.

That is virtually impossible.

Btw, you have declared it twice.

And you haven't declared $text and $value (that's not problem but still...)

[codеbox][/codеbox] is maybe more appropriate

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Here is the code snippet but still their is error

I repeat, no one can check those kind of script, they not full, how we supose to check part of some big script, where used variables and functions called...

Provide full working example of your problem (or the full script you are using), otherwise i don't think anyone can help you with this info.


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I repeat, no one can check those kind of script, they not full, how we supose to check part of some big script, where used variables and functions called...

Provide full working example of your problem (or the full script you are using), otherwise i don't think anyone can help you with this info.

Ok As per your saying i am posting full code here. What i am trying to do is: -

in Logger.au3 I have written function to write the log file. I want to provide the different log levels. So with BeginLog() i am trying to set the log level. Now this logger.au3 i have included in temp.au3.

but when i run the code with it gives me error saying "Variable used without being declared"

Common.au3

Edited by VinitMeta

Share this post


Link to post
Share on other sites

Func OnAutoItStart()

....

This one tells me all :) - OnAutoItStart() called before even script is started, so no variables declared on this step.

You can check it like this:

Func OnAutoItStart()
    If Not IsDeclared("LogPath") Then Exit MsgBox(16, "", "Variable [$LogPath] is not declared yet")
    BeginLog(1,"C:\Test.log")
    Call("LogTester")
EndFuncoÝ÷ Ø Ý£*.綬zØb±û§rبrcºËn±ëazX§zÊ.¶ÈzØ^~éܶ*'±´Þ²Ö®ßöÚºÚ"µÍYÚ[ÙÊK  ][ÝÐÎÌLÕÝÙÉ][ÝÊBØ[
    ][ÝÓÙÕÝ][ÝÊB[ÈÙÕÝ
BÜ]SÙ×ÑXYÊ ][ÝÑXYÉ][ÝÊBÜ]SÙ×ÑÜ   ][ÝÑÜ][ÝÊBÜ]SÙ×ÕØ ][ÝÕØ[É][ÝÊB[[

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

This one tells me all >_< - OnAutoItStart() called before even script is started, so no variables declared on this step.

You can check it like this:

Func OnAutoItStart()
    If Not IsDeclared("LogPath") Then Exit MsgBox(16, "", "Variable [$LogPath] is not declared yet")
    BeginLog(1,"C:\Test.log")
    Call("LogTester")
EndFuncoÝ÷ Ø Ý£*.綬zØb±û§rبrcºËn±ëazX§zÊ.¶ÈzØ^~éܶ*'±´Þ²Ö®ßöÚºÚ"µÍYÚ[ÙÊK  ][ÝÐÎÌLÕÝÙÉ][ÝÊBØ[
    ][ÝÓÙÕÝ][ÝÊB[ÈÙÕÝ
BÜ]SÙ×ÑXYÊ ][ÝÑXYÉ][ÝÊBÜ]SÙ×ÑÜ   ][ÝÑÜ][ÝÊBÜ]SÙ×ÕØ ][ÝÕØ[É][ÝÊB[[

Thanks a lot 'MrCreatoR'. :)

Share this post


Link to post
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
Sign in to follow this  
Followers 0