在另一个工作站上运行电子表格时找不到自定义DLL [英] Custom DLL not found when running spreadsheet on another workstation

查看:68
本文介绍了在另一个工作站上运行电子表格时找不到自定义DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过转到Win32-> DLL并使用预编译的头文件在Visual Studio Express 2010 C ++中创建了一个DLL。  DLL有两个我在DEF文件中导出的函数,名为AesEncrypt()和AesDecrypt()。

I have created a DLL in Visual Studio Express 2010 C++ by going to Win32->DLL and using precompiled headers.  The DLL has two functions that I export in a DEF file, called AesEncrypt() and AesDecrypt().

我的目标是从Excel到VBA使用这些函数。 我创建了一个新的电子表格和以下代码来调用这两个函数:

My goal is to use these functions from Excel through VBA.  I created a new spreadsheet and the following code to call the two functions:

'Declaration of Constants------------------------------------------------------------------------------------------------------------------------
Const ECB_CRYPT_ROW As Integer = 7     'Row number corresponding to the first ECB "Encrypted" row
Const CBC_CRYPT_ROW As Integer = 22     'Row number corresponding to the first CBC "Encrypted" row
Const CTR_CRYPT_ROW As Integer = 37     'Row number corresponding to the first CTR "Encrypted" row
Const KEY_ROW As Integer = 46        'Row number corresponding to the "Key" row
Const IV_COL As Integer = 17        'Column number corresponding to the "Initialization Vector" column
Const COL_128_BIT As Integer = 2      'Column number corresponding to the "AES-128" column
Const COL_192_BIT As Integer = 7      'Column number corresponding to the "AES-192" column
Const COL_256_BIT As Integer = 12      'Column number corresponding to the "AES-256" column


'|***********************************************************************************************************************************************
'|*** Brief:
'|***  -AesEncrypt takes abPlainText and encrypts using given mode eAesMode (ECB, CBC, or CTR), key, and IV (if applicable)
'|***    -Parameters:
'|***      -abPlainText:    The first element of a Byte array containing plaintext
'|***      -nPlainTextLength: Length of abPlainText array in Bytes
'|***      -abKey:       The first element of a Byte array containing cipher key
'|***      -nKeyLength:    Length of the abKey array in Bytes
'|***      -abIV:       The first element of a Byte array containing the Initialization Vector (if applicable)
'|***      -eAesMode:     Mode to use for AES encryption. 0 = ECB; 1 = CBC; 2 = CTR.
'|***      -abOutputBuffer:  The first element of a Byte array in which the cipher text will be placed
'|***        -NOTE:     User must ensure the Output Buffer array is at least as long as the Plain Text array'|***
'|***    -Return: Returns an int. 0 = success, !0 = error
'|***  -AesDecrypt takes abCryptText and decrypts using given mode eAesMode (ECB, CTR, or CBC), key, and IV (if applicable)
'|***    -Parameters: Similar to those for AesEncrypt, above
'|***    -Return: Returns an int. 0 = success, !0 = error
'|***  -NOTE: These function declarations assume AesEngineDll.dll is in the SAME FOLDER as this file
'|***********************************************************************************************************************************************
Private Declare Function AesEncrypt Lib "AesEngineDll.dll" (ByRef abPlainText As Byte, ByVal nPlainTextLength As Integer, _
    ByRef abKey As Byte, ByVal nKeyLength As Integer, ByRef abIV As Byte, ByVal eAesMode As Integer, ByRef abOutputBuffer As Byte) As Integer
Private Declare Function AesDecrypt Lib "AesEngineDll.dll" (ByRef abCryptText As Byte, ByVal nCryptTextLength As Integer, _
    ByRef abKey As Byte, ByVal nKeyLength As Integer, ByRef abIV As Byte, ByVal eAesMode As Integer, ByRef abOutputBuffer As Byte) As Integer
    
Sub ClearButton_Clicked()
  'Loop through all relavent cells and clear their contents
  For i = 2 To 12 Step 5
    For j = 7 To 37 Step 15
      For k = 0 To 7
        Cells(j + k, i) = ""
      Next
    Next
  Next
End Sub
Sub RunTestButton_Clicked()
  ChDrive (ThisWorkbook.Path)
  ChDir (ThisWorkbook.Path)
  
  Dim abPlainText(0 To 63) As Byte    'PlainText byte array
  Dim abKey() As Byte           'Key byte array
  Dim abIV(0 To 15) As Byte        'IV byte array - only needed for CBC and CTR modes
  Dim abCipherText(0 To 63) As Byte    'CipherText byte array
  Dim abDecryptedText(0 To 63) As Byte  'DecryptedText byte array
  Dim nEncryptResult As Integer      'Result of encryption (0=success)
  Dim sPlainText As String        'PlainText string - read in by macro
  Dim sKey As String           'Key string - output by macro
  Dim sIV As String            'IV string - output by macro
  Dim sCipherText             'Ciphertext string - output by macro
  Dim sDecryptedText As String      'Decrypted text string - output by macro
    
  ''''''''''''''ECB ENCRYPTION AND DECRYPTION''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  
  ''''''128-BIT''''''
  ReDim abKey(0 To 127) As Byte
  sKey = Cells(KEY_ROW, COL_128_BIT).Text
  Call HexStringToByteArray(sKey, abKey)
  sPlainText = Cells(ECB_CRYPT_ROW - 4, COL_128_BIT).Text & Cells(ECB_CRYPT_ROW - 3, COL_128_BIT) & Cells(ECB_CRYPT_ROW - 2, COL_128_BIT) & Cells(ECB_CRYPT_ROW - 1, COL_128_BIT)
  Call HexStringToByteArray(sPlainText, abPlainText)
  nEncryptResult = AesEncrypt(abPlainText(0), 64, abKey(0), 128, abIV(0), 0, abCipherText(0))
  For i = 1 To 4
    Cells(ECB_CRYPT_ROW + i - 1, COL_128_BIT) = Mid(ByteArrayToHexString(abCipherText), (i - 1) * 32 + 1, 32)
  Next
  nEncryptResult = AesDecrypt(abCipherText(0), 64, abKey(0), 128, abIV(0), 0, abDecryptedText(0))
  For i = 1 To 4
    Cells(ECB_CRYPT_ROW + 4 + i - 1, COL_128_BIT) = Mid(ByteArrayToHexString(abDecryptedText), (i - 1) * 32 + 1, 32)
  Next
  
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub

Sub HexStringToByteArray(ByVal sHexString As String, ByRef abByteArray() As Byte)
  If Len(sHexString) Mod 2 = 0 Then
    For i = 1 To Len(sHexString) - 1 Step 2
      abByteArray((i - 1) / 2) = CByte(("&H") & Mid(sHexString, i, 2))
    Next i
  Else
    MsgBox "Invalid Hex String Length!"
  End If
End Sub

Function ByteArrayToHexString(ByRef abByteArray() As Byte) As String
  Dim sResult As String
  sResult = ""
  Dim nCounter As Integer
  nCounter = 0
  For nIndex = LBound(abByteArray) To UBound(abByteArray)
    'ByteArrayToHexString = ByteArrayToHexString & Hex(abByteArray(nIndex))
    ByteArrayToHexString = ByteArrayToHexString & Evaluate("DEC2HEX(" & abByteArray(nIndex) & ", 2)")
    nCounter = nCounter + 1
  Next
End Function

在与此Excel文件相同的文件夹中,我放置了AesEngineDll.dll。 我运行函数"RunTestButtonClicked(),"点击电子表格中的按钮后。 这个子程序反过来加密,然后解密一些样本数据。

In the same folder as this Excel file, I have placed AesEngineDll.dll.  I run the function, "RunTestButtonClicked()," upon clicking a button within the spreadsheet.  This subroutine in turn encrypts then decrypts some sample data.

这一切都在我的机器上完美运行。 但是,当我将它移动到另一个工作站时,我收到一条错误,指出Excel无法找到DLL文件(它告诉我它无法找到该文件,我将其缩小到Encrypt()调用)。

This all works perfectly on my machine.  However, when I move it to another workstation, I get an error stating that Excel cannot find the DLL file (it tells me it can't find the file, and I narrowed it down to the Encrypt() call).

最初,我在My Documents中创建了这个电子表格,并从我的VBE2010解决方案文件夹的Debug文件夹中复制了DLL。 然后我将电子表格和DLL复制到"C:\ Shared"的文件夹中。并尝试再次运行它。 
它给出了一个错误,指出它无法找到DLL,所以我在RunTestButtonClicked()的开头添加了两行:

Originally, I had created this spreadsheet in My Documents and copied over the DLL from the Debug folder of my VBE2010 solution folder.  I then copied BOTH the spreadsheet and the DLL to a folder at "C:\Shared" and tried running it again.  It gave an error stating that it could not find the DLL, so I added the two lines at the beginning of RunTestButtonClicked():


ChDrive (ThisWorkbook.Path)<br/>
ChDir (ThisWorkbook.Path)<br/>

推荐答案

您好,

确保您设置DLL的引用(VB编辑器 - 工具 - 参考 - 浏览)

Make sure that you set a Reference to your DLL (VB Editor-Tools-Reference-Browse)

Nadia


这篇关于在另一个工作站上运行电子表格时找不到自定义DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆