Jump to content

_WinAPI_HiDWord(), _WinAPI_LoDWord()


-Ultima-
 Share

Recommended Posts

Recently, I've been playing with functions that require knowledge of the high and low order dwords of 64-bit numbers, so I had to coax AutoIt into giving me those bits of information. Initially, I tried using BitShift with BitAND (the "normal" ways to do this), but after re-reading the documentation, I realized that they (unfortunately) work only with 32-bit integers. So I ended up having to mess around with DllStructs to get the job done. It's probably not the most efficient way to do this, but it seems to work reliably for me. In the end, I realized that I didn't actually need to extract the information manually (the high/low order dwords in int64 datatypes already seem to be in an order I can work with directly by simply using int64 instead of dword[2]) xD Not wanting any work to go to waste, I thought I'd just pass it along.

What would be better is for AutoIt's bitwise operators to support 64-bit integers, but even if that were implemented, I'm not really sure it'd get backported to 3.2.12.x, so 3.2.12.x users might still get left in the cold. Anyhow, here:

; #FUNCTION# ======================================================================================
; Name...........: _WinAPI_HiDWord
; Description ...: Returns the high dword of a long long
; Syntax.........: _WinAPI_LoWord($iLongLong)
; Parameters ....: $iLongLong - Long long value
; Return values .: Success - High order dword of the long long value
; Author ........: Ultima
; Modified.......:
; Remarks .......:
; Related .......: _WinAPI_LoDWord
; Link ..........;
; Example .......; Yes
; =================================================================================================
Func _WinAPI_HiDWord($iLongLong)
    Local $tQWORD = DllStructCreate("uint64")
    Local $tDWORDs = DllStructCreate("dword[2]", DllStructGetPtr($tQWORD))
    DllStructSetData($tQWORD, 1, $iLongLong)
    Return DllStructGetData($tDWORDs, 1, 2)
EndFunc   ;==>_WinAPI_HiDWord

; #FUNCTION# ======================================================================================
; Name...........: _WinAPI_LoDWord
; Description ...: Returns the low dword of a long long
; Syntax.........: _WinAPI_LoWord($iLongLong)
; Parameters ....: $iLongLong - Long long value
; Return values .: Success - Low order dword of the long long value
; Author ........: Ultima
; Modified.......:
; Remarks .......:
; Related .......: _WinAPI_HiDWord
; Link ..........;
; Example .......; Yes
; =================================================================================================
Func _WinAPI_LoDWord($iLongLong)
    Local $tQWORD = DllStructCreate("uint64")
    Local $tDWORDs = DllStructCreate("dword[2]", DllStructGetPtr($tQWORD))
    DllStructSetData($tQWORD, 1, $iLongLong)
    Return DllStructGetData($tDWORDs, 1, 1)
EndFunc   ;==>_WinAPI_LoDWord

If anyone can think of better ways to do this (particularly, more efficient and/or technically correct methods), I'll more-than-gladly accept, as this implementation is probably more of a hack than anything. I just threw it together so that I could move on muttley I do get the distinct feeling that my assumptions about the way integers are stored may be different depending on computer architecture, but I'm not familiar enough to know for sure.

Edit (2008-07-06 @ 19:02EST): Oh, and the obligatory example...

#AutoIt3Wrapper_Au3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
;#include <WinAPI.au3>

_Main()

Func _Main()
    Local $iLongLong = 65535 * 65535 * 65535 * 256
    MsgBox(0, $iLongLong, "HiDWord: " & _WinAPI_HiDWord($iLongLong) & @LF & "LoDWord: " & _WinAPI_LoDWord($iLongLong))
EndFunc;==>_Main

Edit (2008-07-07 @ 21:03EST): Optimized versions... for the time being... (I haven't really tested them carefully):

Func _WinAPI_HiDWord($iLongLong)
    Return BitShift(Int($iLongLong/4294967296), 16)*65536 + BitAND(Int($iLongLong/4294967296), 65535)
EndFunc;==>_WinAPI_HiDWord

Func _WinAPI_LoDWord($iLongLong)
    Return BitAnd($iLongLong, 4294967295)
EndFunc;==>_WinAPI_LoDWord

Edit: For the most part, this revised version of _WinAPI_HiDWord() works fine. Problems begin occurring on very large numbers, though. Oh well.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

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...