Jump to content

How to translate a C struct containing bit fields using DllStructCreate()?


Recommended Posts

Consider the following C struct as an example:

typedef struct _KEY_VIRTUALIZATION_INFORMATION {
  ULONG VirtualizationCandidate  :1;
  ULONG VirtualizationEnabled  :1;
  ULONG VirtualTarget  :1;
  ULONG VirtualStore  :1;
  ULONG VirtualSource  :1;
  ULONG Reserved  :27;
} KEY_VIRTUALIZATION_INFORMATION, *PKEY_VIRTUALIZATION_INFORMATION;

What is the AutoIt equivalent to the above C struct? I create the following template to things easier for answerers.

$tagKEY_VIRTUALIZATION_INFORMATION = "ULONG VirtualizationCandidate;" & _
    "ULONG VirtualizationEnabled;" & _
    "ULONG VirtualTarget;" & _
    "ULONG VirtualStore;" & _
    "ULONG VirtualSource;" & _
    "ULONG Reserved"
$tKEY_VIRTUALIZATION_INFORMATION = DllStructCreate($tagKEY_VIRTUALIZATION_INFORMATION)

Thanks in advance.

Link to comment
Share on other sites

I don't think there is a direct way to do that in autoit but it is a 32-bit data struct no matter what the function it is passed to treats it as

so DllStructCreate("ULONG") would work

DllStructCreate("USHORT;USHORT")

or even 

DllStructCreate("BYTE;BYTE;BYTE;BYTE")

whatever you pass it to isn't going to care but for you the first will probably be the easiest

since to set or retrieve the data it has to be done as a chunk

Local $bVirtualizationCandidate, $bVirtualizationEnabled, $bVirtualSource

Local $aBits = [1,2,4,8,16]
Local $tData = DllStructCreate("ULONG")

Somecallthat_changes($tData)

$iVal = DllStructGetData($tData, 1)

If BitAnd($iVal, $aBits[0]) Then $bVirtualizationCandidate = True
If BitAnd($iVal, $aBits[1]) Then $bVirtualizationEnabled = True
If BitAnd($iVal, $aBits[4]) Then $bVirtualSource = True

ConsoleWrite("$bVirtualizationCandidate = " & ($bVirtualizationCandidate ? "True" : "False") & _
" $bVirtualizationEnabled = " & ($bVirtualizationEnabled ? "True" : "False") & _
" $bVirtualSource = " & ($bVirtualSource ? "True" : "False") &@CRLF)

Func SomeCallthat_changes(Byref $tD)
Local $iVal = BitOR($aBits[0],$aBits[4])
DllStructsetData($tD, 1, $iVal)
EndFunc

You don't necessarily need $aBits[] but it makes it easier to grok

Also pay attention to the Endian

Edited by Bilgus
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...