WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Global Variable Storage - a trick with Pointers


Using pointers and the following 3 UDFs, you can safely store and retrieve global variables from any function, no matter how deeply nested, and avoid overwriting them by accident. The first function creates an array to store the global variables and puts a pointer to the array in the registry under the name of the currently running winbatch script/exe. The other two udfs update and retrieve global variables. You can have more than one global variable array, and as near as I can tell, you can pass handles to ole objects to nested functions in this fashion. **Demo**
;--------------------------------------------------------------------------------
;puts a pointer in the registry to an array to be used to store global variables
;returns the specified array
;ArrayName is the name of the array
;VarList is a pipe delimited list of the global variable names to be stored in the array
;--------------------------------------------------------------------------------
;********************************************************************************
#DEFINESUBROUTINE REGISTERGLOBALARRAY(ArrayName,VarList)
;********************************************************************************
%ArrayName% = ARRDIMENSION(ITEMCOUNT(VarList,"|") + 1) ;array holding vars
%ArrayName%[0] = VarList
RETURN ENVIRONSET(ArrayName,&%ArrayName%)
#ENDSUBROUTINE
;--------------------------------------------------------------------------------
;returns the value of a global variable stored in a registered global array
;ArrayName is the name of the array
;Var is the name of the var
;--------------------------------------------------------------------------------
;***************************************************************************
#DEFINEFUNCTION GETGV(ArrayName,Var)
;***************************************************************************
Pointer = ENVIRONMENT(ArrayName)
RETURN *Pointer[ITEMLOCATE(Var,*Pointer[0],"|")]
#ENDFUNCTION
;--------------------------------------------------------------------------------
;sets the value of a global variable stored in a registered global array
;ArrayName is the name of the array
;Var is the name of the var
;value is the value to which the global variable should be set
;--------------------------------------------------------------------------------
;***************************************************************************
#DEFINEFUNCTION SETGV(ArrayName,Var,Value)
;***************************************************************************
Pointer = ENVIRONMENT(ArrayName)
*Pointer[ITEMLOCATE(Var,*Pointer[0],"|")] = Value
RETURN
#ENDFUNCTION

;demo
REGISTERGLOBALARRAY("GlobalArray","Title|Message") ;create a global variable array, call it Global Array
SETGV("GlobalArray","Title","This is the old title") ;set some values
SETGV("GlobalArray","Message","This is the old message")
MESSAGE(GETGV("GlobalArray","Title"),GETGV("GlobalArray","Message")) ;the result
SETGV("GlobalArray","Title","This is the new title") ;change some values
SETGV("GlobalArray","Message","This is the new message")
MESSAGE(GETGV("GlobalArray","Title"),GETGV("GlobalArray","Message")) ;the result 

Article ID:   W17223
File Created: 2007:07:03:14:28:54
Last Updated: 2007:07:03:14:28:54