Correct declaration of DWord in VBA

时光怂恿深爱的人放手 提交于 2019-12-11 16:47:01

问题


I'm using an API function which returns a DWORD

Because I want intellisense on the LoWord and HiWord, rather than using a Long:

Declare Sub myAPI(ByRef outVariable As Long)

...as suggested in this list of WinAPI -> VBA datatype conversions, I'm using a type:

Public Type DWORD 'same size as Long, but intellisense on members is nice
    '@Ignore IntegerDataType
    LoWord As Integer
    '@Ignore IntegerDataType
    HiWord As Integer
End Type

Declare Sub myAPI(ByRef outVariable As DWORD)

However RubberDuck's IntegerDataType inspection reminded me that on 32 bit systems VBA converts 2-byte Integers to 4-byte Longs internally, so I'm wondering whether my DWORD declaration is really 4 consecutive bytes as expected, or 8.

I'm not familiar enough with pointers and bits & bytes to picture in my head what's going on, but I imagine the API somehow knows to fill only the lower half of each part, as I've been getting the results I expect (I think) from the API.


回答1:


Your user defined type is 4 bytes is size, because Integer is 2 bytes in size.

You can check this for yourself:

Dim dw as DWORD
Dim size as Integer
size = LenB(dw)


来源:https://stackoverflow.com/questions/57891281/correct-declaration-of-dword-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!