Updated based on comments.
I have read several articles on here about similar problems of people trying to solve issues when trying to use a DLL function inside VBA through a declare.
I tried so many variants (byref/byval, long, longptr, string ...) but either excel crashes, either the call doesn't do anything apparently
This is the info I have on the DLL:
'/// <summary> Encrypt the message. </summary>
'/// <param name="penc_key_bin"> The binary encryption key. </param>
'/// <param name="penc_iv_bin"> The binary encryption iv. </param>
'/// <param name="punenc_data"> The unencrypted data buffer. </param>
'/// <param name="unenc_offset"> The offset of the first byte of the unencrypted buffer. </param>
'/// <param name="unenc_len"> Length of the unencrypted data to encrypt. </param>
'/// <param name="penc_data"> [out] The encrypted data buffer. </param>
'/// <param name="enc_offset"> The offset of the first byte of the encrypted data buffer. </param>
'/// <param name="penc_len"> [out] Length of the encrypted data. </param>
'/// <returns> The standard SPC return code. </returns>
'unsigned char aes256_cbc_encrypt_message(
' unsigned char *penc_key_bin,
' unsigned char *penc_iv_bin,
' unsigned char *punenc_data,
' long unenc_offset,
' long unenc_len,
' unsigned char *penc_data,
' long enc_offset,
' long *penc_len)
And this is what I currently have what I feel should best fit .. but causes a crash
Private Declare PtrSafe Function aes256_cbc_encrypt_message Lib "Flexc Encryption.dll" _
(ByVal penc_key_bin As LongPtr, _
ByVal penc_iv_bin As LongPtr, _
ByVal punenc_data As LongPtr, _
ByVal unenc_offset As Long, _
ByVal unenc_len As Long, _
ByVal penc_data As LongPtr, _
ByVal enc_offset As Long, _
ByVal penc_len As Long) _
As Long
I use is like here:
Sub test()
Dim Key() As Byte
Dim IV() As Byte
Dim res As Variant
Dim strDec() As Byte
Dim strEnc() As Byte
Dim decOff As Long
Dim decLen As Long
Dim plainASCII As String
plainASCII = "TESTTESTTESTTESTTESTTESTTESTTEST"
decOff = 0
decLen = Len(plainASCII)
res = check_dll_activated()
strDec = stringToByteArr(plainASCII)
ReDim strEnc(decLen - 1)
Key = stringToByteArr(DecodeHex("0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"))
IV = stringToByteArr(DecodeHex("00000000000000000000000000000000"))
res = aes256_cbc_encrypt_message((Key(0)), (IV(0)), (strDec(0)), 0, 32, (strEnc(0)), decOff, decLen)
'variant 1: res = aes256_cbc_encrypt_message(VarPtr(Key(0)), VarPtr(IV(0)), VarPtr(strDec(0)), 0, 32, VarPtr(strEnc(0)), decOff, decLen)
'variant 2: res = aes256_cbc_encrypt_message(VarPtr(Key), VarPtr(IV), VarPtr(strDec), 0, 32, VarPtr(strEnc), decOff, decLen)
End Sub
Private Function stringToByteArr(inpStr As String) As Byte()
Dim outByte() As Byte
ReDim outByte(Len(inpStr) - 1)
For i = 1 To Len(inpStr)
outByte(i - 1) = Asc(Mid(inpStr, i, 1))
Next i
'outByte = inpStr
stringToByteArr = outByte
End Function
Private Function DecodeHex(ByVal HexString As String) As String
Dim thisChar As String
Dim ascii As Integer
Dim ret As String
'Keep going until we exhaust all the source string
Do While Len(HexString) > 0
'Get the next two-digit hex number
thisChar = Left(HexString, 2)
'Remove this hex number from the source string
HexString = Mid(HexString, 3)
'Get the value in decimal of this hex number
ascii = CInt(Val("&H" & thisChar))
'Convert it to a character
ret = ret & Chr(ascii)
Loop
'All done
DecodeHex = ret
End Function
Any advice is helpful. C.