Jump to content
tukangusil7

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

Recommended Posts

tukangusil7

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.

Share this post


Link to post
Share on other sites
tukangusil7

How to edit this post? There is a trivial typo in that post. I'm a newbie in this forum.

Share this post


Link to post
Share on other sites
Earthshine

you may not be able to edit yet because your post count is very low. it looks good, I am sure one of the experts will be along with your answer.


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
Bilgus
Posted (edited)

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
  • Thanks 1

Share this post


Link to post
Share on other sites
tukangusil7

Bilgus, thank you very much. I believe your're correct. I will try it.

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

×