CryptoSys API Library Manual

Notes on VBA wrapper functions

Use the VBA wrapper functions to avoid the pre-dimensioning issues for functions that output to a string or byte array.

New in [v6.20]: All VBA wrapper functions and definitions are now in the one file basCryptoSys.bas. The file basCryptoSysWrappers.bas is withdrawn and must not be used with v6.20 and above.

No need to make a first pass to find the required length, or pre-dimension the output variable using String(nChars, " ") or ReDim lpOutput(nBytes-1), and then call again (a potentially dangerous sequence of events). The wrapper functions do it all in one line.

Dim nChars As Long
Dim strModuleName As String

' The old long way...
nChars = API_ModuleName("", 0, 0)
Debug.Assert nChars > 0
strModuleName = String(nChars, " ")
nChars = API_ModuleName(strModuleName, nChars, 0)
Debug.Print strModuleName   ' C:\WINDOWS\SYSTEM32\diCryptoSys.dll

' The new one-liner with a wrapper function
strModuleName = apiModuleName()
Debug.Assert Len(strModuleName) > 0

If the core VBA function name is FOO_FuncName(), then the wrapper function will be fooFuncName(). The function fooFuncName() will return the output as a string or byte array directly.

If an error occurs, the result will be a zero-length string or array. Use the API_ErrorCode function to get more information.

Byte array variables

Wrapper functions that output to a byte array return the array directly. No need to ReDim. Input byte arrays are passed directly without needing the (0) and without needing to compute the length beforehand.

' The old way - we need all the byte array lengths
Dim lpKey() As Byte
Dim lpIV() As Byte
Dim lpPlain() As Byte
Dim lpCipher() As Byte
Dim nBytes As Long
Dim nDataLen As Long
Dim nKeyLen As Long
Dim nIvLen As Long
' Set values for lpPlain, lpKey, lpIV [cut]
nDataLen = cnvBytesLen(lpPlain)
nKeyLen = cnvBytesLen(lpKey)
nIvLen = cnvBytesLen(lpIV)
nBytes = CIPHER_EncryptBytes(ByVal 0&, 0, lpPlain(0), nDataLen, lpKey(0), nKeyLen, lpIV(0), nIvLen, "Aes128/CBC/OneAndZeroes", 0)
Debug.Assert nBytes > 0
ReDim lpCipher(nBytes - 1)
nBytes = CIPHER_EncryptBytes(lpCipher(0), nBytes, lpPlain(0), nDataLen, lpKey(0), nKeyLen, lpIV(0), nIvLen, "Aes128/CBC/OneAndZeroes", 0)

With wrapper function:

lpCipher = cipherEncryptBytes(lpPlain, lpKey, lpIV, "Aes128/CBC/OneAndZeroes", 0)
That's 4 extra variables we don't need, and seven lines of code is reduced to one. Note the (0) needed in the raw function, e.g. lpPlain(0), is not required with the wrapper function.
[PREV: Other Issues For VB6/VBA Users...]   [Contents]   [Index]   
   [NEXT: Empty byte arrays...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.