IonicWind Software

IWBasic => General Questions => Topic started by: Andy on August 16, 2017, 11:25:48 PM

Title: The Union command
Post by: Andy on August 16, 2017, 11:25:48 PM
The Union command (one I have never used before, and never really noticed it in the help file) was the way to get a Uuint64 value into a hex string - this is the code:

You will not be able to run this yet, not until I post the library and include files.

$include "C:\\Include Files\\Registry\\registrylib.inc"

union dat
uint64 value
uint dword[2]
endunion

def num as dat

num.dword[0] = 0xFfffffff
num.dword[1] = 0xFfffffff

num.value = RegGetQWValue("HKEY_CURRENT_USER\\Software\\Test\\One","q2")

openconsole
print
print
print hex$(num.value)
print
print num.dword[0]
print num.dword[1]
print
print
print
do:until inkey$ <> ""
end


Now, I understand what Clint, Jalih and Fasecero are telling me about splitting up the Uint64 into two dwords (low and high bits), and from what I've read on the Internet a Union is a variable held in memory (in the same location) that can be of any valid variable type.

So in the above code, what is the purpose of these two lines - I presume we are setting the max values for both low and high bit values????


num.dword[0] = 0xFfffffff
num.dword[1] = 0xFfffffff


I'm simply asking for a little clarification on the command as the help file doesn't seem to tell me much about it.

It certainly works, attached is a sample of my RegExport function - note the qwords are correct (but I had to reverse each pair for it to be in the correct format for a .reg file).





Title: Re: The Union command
Post by: jalih on August 17, 2017, 01:56:49 AM
Hi Andy,

It was just to show you the full uint64 range works. It just assigns hi -and lo-dwords.
Title: Re: The Union command
Post by: Andy on August 17, 2017, 07:51:05 AM
Thanks Jalih, I does demonstrate it works, but how?
Title: Re: The Union command
Post by: LarryMc on August 17, 2017, 10:29:36 PM
with a union your saying that I want to create variables that sometimes contain one type of information and sometimes contain another type of information.
In the example the following was used:
union dat
uint64 value
uint dword[2]
endunion


The difference between the above and this
type dat
uint64 value
uint dword[2]
endtype

is that when I declare
dat myvar
using the TYPE declaration myvar consumes to pieces of memory at two different memory addresses; the first address points to myvar.value and the second points to myvar.dword[0] and a total of two unit64 or 4 dwords of memory are consume(which ever way you want to look at it)

when I declare
dat myvar
using the UNION declaration myvar consumes one piece of memory at only one memory address and the total amount of memory used is 1 unit64 0r 2 dwords(depending upon how you want to look at it)
when you use myvar.value you are telling the compiler to treat that address as a uint64 value and when you use myvar.dword[0] you're telling it to treat it as an int.

With unions there is nothing to keep you from doing this:
union dat
uint64 value
uint dword[2]
       string wow
endunion


I know I'm not very good at explaining but I hope that helps a little.