• Welcome to Jose's Read Only Forum 2023.
 

global variables

Started by Kent Sarikaya, September 23, 2007, 07:03:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

#30
 
The "trick" is to use Automation compatible types, e.g. BSTR instead of ASCIIZ, safe arrays instead of other kind of arrays, object variables instead of pointers to custom allocated memory, etc. VariantClear knows how to manage them.

For a VT_BSTR variant, you allocate an string with SysAllocString and store the returned pointer in the structure. VariantClear will call SysFreeString to free the memory used by the string.

For a safe array, you allocate the memory and manage the elements using the API safe arrays functions. VariantClear will call SafeAerrayDestroy, that knows how to free the memory used by the array since a safe array includes information about the kind of data stored and the number of elements.

For an object variable, VariantClear will call the Release method of the object.

Now, if you want to use other kind of strings or arrays, then you will have to free the memory yourself, because VariantClear has no means to know how to do it.

For a perfect solution, just include another of my suggestions: native support for safe arrays.

Charles Pegge


Oops! I missed your COM post Jose, this topic is moving too fast!

COM and variants do not prevail in the Linux world, but the Variant structure is generic enough to be an open standard, and a very comprehensive one. Do we know if Microsoft considers the Variant and COM system as being Microsoft property. I find myself using very similar structures in $ and R$ to handle different data types and could very well adopt at least some of the standard. It would certainly improve interoperability, for low-level coding.

José Roca

 
You even can have structures containing variants, objects, etc., inside a variant. For that, you will need to learn some low-level COM programming and use the IRecordInfo interface. The possibilities are enormous...

Charles Pegge


Coming back to this global thing, most of my coding uses simple arrays of global strings. You can put dynamic structures inside strings and pass them anywhere by index. Then  memory allocation /  deallocation is never an issue. The string array can also be used as a stack, so that individual string elements are available for reuse.

The DIM .. AT .. when used in conjunction with strings is an especially useful feature of PowerBasic. - something I miss in FB.