You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA声明调用aes256_cbc_encrypt_message DLL函数失败求助

基于评论更新内容

我查阅了多篇相关文章,尝试解决在VBA中通过Declare语句调用DLL函数时遇到的问题。

我尝试了多种参数组合(ByRef/ByVal、Long、LongPtr、String等),但要么导致Excel崩溃,要么调用后无明显效果。

以下是我获取的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)

我目前使用的声明语句如下,我认为最匹配,但会导致崩溃:

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

我的调用代码如下:

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

恳请各位提供解决建议,谢谢!
C.


内容的提问来源于stack exchange,提问作者Christof De Backere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 23:35:19