scila1996

How to create UNION type like C Programer ?

6 posts in this topic

i Have a C Code

union
{
    int a;
    byte b;
} test_union;

int main
{
    test_union _var;
    _var.a = 256;
    _var.b = 2;
}

=> Result of _var.a => 258

I'm try with DllStructCreate with this way

Local $_union = DllStructCreate("byte[4]")
Local $_test_union = DllStructCreate("int a;int b", DllStructGetPtr($_union))
DllStructSetData($_test_union, 1, 256)
DllStructSetData($_test_union, 2, 2)
MsgBox(64, "", DllStructGetData($_test_union, "a"))

=> result is : 256 ?

 

How do i create a union with data organize like C ? Please help me ! Thank you for reply :)

Share this post


Link to post
Share on other sites



You're playing with firearms here: $_test_union is twice as large as $_union.

Anyway, this is the counterpart of your C example:

Local $_union = DllStructCreate("byte[4]")
Local $_test_union = DllStructCreate("int a", DllStructGetPtr($_union))
DllStructSetData($_test_union, 1, 256)
DllStructSetData($_union, 1, 2, 1)
MsgBox(64, "", DllStructGetData($_test_union, "a"))

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

You're playing with firearms here: $_test_union is twice as large as $_union.

Anyway, this is the counterpart of your C example:

Local $_union = DllStructCreate("byte[4]")
Local $_test_union = DllStructCreate("int a", DllStructGetPtr($_union))
DllStructSetData($_test_union, 1, 256)
DllStructSetData($_union, 1, 2, 1)
MsgBox(64, "", DllStructGetData($_test_union, "a"))

 

How to get data of int b ?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

How to i create union with element is struct ? Example

typedef union _ULARGE_INTEGER {
  struct {
    DWORD LowPart;
    DWORD HighPart;
  };
  struct {
    DWORD LowPart;
    DWORD HighPart;
  } u;
  ULONGLONG QuadPart;
} ULARGE_INTEGER, *PULARGE_INTEGER;

 

Edited by scila1996

Share this post


Link to post
Share on other sites

The struct u is pointless.

Simply like this:

Local $_union = DllStructCreate("long[2]")
Local $_test_union = DllStructCreate("int64 a", DllStructGetPtr($_union))
DllStructSetData($_union, 1, 0x12345678, 1)
DllStructSetData($_union, 1, 0x99999999, 2)
MsgBox(64, "", Hex(DllStructGetData($_union, 1, 1), 8))
MsgBox(64, "", Hex(DllStructGetData($_union, 1, 2), 8))
MsgBox(64, "", Hex(DllStructGetData($_test_union, "a"), 16))
MsgBox(64, "", DllStructGetData($_test_union, "a"))

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

How to get data of int b ?

I'm not 100% on this, but quite sure that you are missing the point of union.

I believe it's that it will be one variable stored in it, either the byte or the int, not both.

So because it can be an int, and an int is larger than byte, it has to at least the size of int.

Don't take that as definite, I've been known to be wrong.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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