VBA cannot dim a UInteger - vba

I try to dim an unsigned integer using the line:
dim curStart as UInteger
and on running, I receive the error:
Compile Error
"User-defined type not defined"
Is there a reference library that is required in VBA to allow unsigned integers?
I'm using Visual Basic for Applications 7.1.1068

You’re out of luck - VBA does not support unsigned types.
Use a Long instead, which is a superset of a 16 bit unsigned type.

Related

Declare Struct for DLL in OpenOffice Basic

I've got some (32 bit) Office VBA I'd like to port to Libre Office or similar:
Type wsaData
wVersion As Integer
wHighVersion As Integer
szDescription As String * WSA_DescriptionSize
szSystemStatus As String * WSA_SysStatusSize
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As String * 200
End Type
Public Declare Function WSAStartup Lib "ws2_32.dll" (wVersionRequested As Integer, lpWSAData As wsaData) As Long
However, the packing and type declarations for Libre Office Basic are different than MS Office VBA, and the Libre/Open office version crashes irretrievably. (Unlike MS Office for Windows, LO doesn't provide stack protection to DLL/dylib calls)
So far I haven't found any documentation for 'Declare Function', (which must be somewhere), but I also specifically need guidelines for passing strings and structs to declared dlls (which FAIK may not even be possible)
If anyone's done that specific struct before I'll be pleased :), but I also need guidelines.

Correct declaration of DWord in VBA

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

Unknown SetProcessDpiAwareness Error Result

I have 1 computer out of 50 that is returning a non zero result from SetProcessDpiAwareness and I can't find any information on it. I am setting the DPI Awareness to Unaware. I have one computer that is returning a value of 105621835743232(Decimal). It seems to still be setting the DPI Awareness to unaware like it should but gives a return value that is not expected.
Private Declare Function SetProcessDpiAwareness Lib "shcore.dll" (ByVal Value As PROCESS_DPI_AWARENESS) As Long
Private Function SetDPI() As Long
'Results from SetProcessDPIAwareness
'Const S_OK = &H0&
'Const E_INVALIDARG = &H80070057
'Const E_ACCESSDENIED = &H80070005
Dim lngResult As Long
lngResult = SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_DPI_Unaware)
Return lngResult
End Function
This is a clickonce winforms application so I can't use the manifest to set DPI.
Any help with locating documentation would be greatly welcomed!
Thanks in advance.
This API function's return value is a 32 bit integer so you should use Integer rather than Long. Note that Long is a 64 bit type.
I also strongly recommend using p/invoke rather than Declare. The latter exists for compatibility reasons. P/invoke offers much greater control of the import process.

UInt64 - Automation Type Not SUpported

I'm trying to implement a SHA512 algorithm on VBA, Excel 2003.
This is what i'm using as a basis: http://www.saphir2.com/sphlib/
I'm facing a problem with the use of UInt64 (unsigned long) in VBA. The following code throws the error: Variable Uses an Automation Type Not Supported.
Sub Main()
Dim notworking As UInt64
End Sub
How do I resolve this?
Unsigned integer datatype isn't supported in vba. I think you might have a library for vb.net.

VB.net Passing a structure to unmanaged dll

I'm migrating some VB6 code to VB.net,
the code contains a structure that contains 1d arrays, 2d arrays, and few other variables.
The general outline of the Vb.net structure is as under
Public Structure Test
Dim a As Single
Dim b As Single
Dim c As Single
<VBFixedArray(2)> Dim arr1() As Single
<VBFixedArray(2, 2)> Dim mdarr1(,) As Single
<VBFixedArray(4)> Dim arr2() As Byte
<VBFixedArray(4)> Dim arr3() As Short
<VBFixedArray(3, 2)> Dim mdarr2(,) As Integer
Dim d As Integer
Dim e As Decimal
End Structure
The call to the dll is declared as under
Public Declare Sub getState Lib "val.dll" (ByRef state As Test)
Elsewhere on this site I realized that we have to "marshal" the structure to allow it to be compatible with the unmanaged code that is about to accept it.
However I still receiving runtime errors when running the code, I don't have any clue of how to use the System.Runtime.InteropServices.Marshal class.
What would be the correct way to pass this structure to the dll?
EDIT:
The original VB6 data type is
Public Type Test
a As Single
b As Single
c As Single
arr1(0 To 2) As Single
mdarr1(0 To 2, 0 To 2) As Single
arr2(0 To 4) As Byte
arr3(0 To 4) As Integer
mdarr2(0 To 3, 0 To 2) As Long
d As Long
e As Currency
End Type
Do you have the source code for getState in the val.dll? If it's written in C or C++, and you have the source code or even just the headers, you could use the P/Invoke Assistant to automatically generate your VB.Net code.
Alternatively... (and please do post the original VB6 structure!)
You might need to allocate the arrays before calling getState, e.g. state.arr1 = {0.0, 0.0} etc.
The Decimal variable e could cause you a problem. In VB6 this was probably a Currency variable, and Decimal is not an exact equivalent as far as I can remember. There will be a way to tell VB.Net to marshal it like a Currency. Perhaps adding an attribute like this...
Sample code:
Imports System.Runtime
Public Structure Test
''blah blah
<InteropServices.MarshalAs(InteropServices.UnmanagedType.Currency)> _
Dim e As Decimal
''blah blah