为什么VBA调用C DLL时,VB调用相同的DLL不会

时间:2016-10-08 16:03:53

标签: vba dll

我有一个调用C DLL的VB程序。我试图用VBA for Access做同样的事情。当调用调用ERPGetFingerprintImage(buff,tam_image)时,VB代码工作,VBA崩溃。我认为字节数组接口会产生错误。

任何线索? 提前谢谢。

这是VB代码:

  Public Class Form1
    Declare Sub ERPInitialize Lib "C:\Desenv\EBioNet\EBioNet\Release\ERPDll.dll" (ByVal hWnd As IntPtr)
    Declare Sub ERPStartCapture Lib "C:\Desenv\EBioNet\EBioNet\Release\ERPDll.dll" ()
    Declare Sub ERPIsFingerCaptured Lib "C:\Desenv\EBioNet\EBioNet\Release\ERPDll.dll" ()
    Declare Sub ERPGetFingerprintImage Lib "C:\Desenv\EBioNet\EBioNet\Release\ERPDll.dll" (fingerprintimage As Byte(), ByRef tam_image As Integer)
    Declare Sub ERPGetFingerprintTemplate Lib "C:\Desenv\EBioNet\EBioNet\Release\ERPDll.dll" (fingerprinttemplate As Byte(), ByRef tam_image As Integer)
    Declare Sub ERPGetFingerprintBitMap Lib "C:\Desenv\EBioNet\EBioNet\Release\ERPDll.dll" (ingerprinttemplate As PictureBox, ByRef tam_image As Integer)

    Dim buff() As Byte = New Byte(0) {}
    Dim tam_image As Integer

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Call ERPInitialize(IntPtr.Zero)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Call ERPStartCapture()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Call ERPIsFingerCaptured()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        tam_image = 256
        ReDim buff(1000 * 1000)

        Call ERPGetFingerprintImage(buff, tam_image)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Call ERPInitialize(PictureBox1.Handle.ToInt32)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Call ERPGetFingerprintTemplate(buff, tam_image)
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Call ERPGetFingerprintBitMap(PictureBox1, tam_image)
    End Sub
End Class

VBA代码如下:

Option Compare Database
Dim buff() As String

Dim tam_image As Integer

Private Declare Function ERPGetFingerprintImage Lib "C:\Biometria\ERPDll.dll" (ByRef fingerprintimage() As Byte, ByRef tam_image As Integer)


Private Sub Command2_Click()
On Error GoTo Command2_Click_erro

Dim buff() As Byte
tam_image = 256

   ReDim buff(500000)
   Call ERPGetFingerprintImage(buff, tam_image)
Command2_Click_exit:
   Exit Sub
Command2_Click_erro:
MsgBox (Error & " - Rotina ERPGetFingerPrintImage e Rotina ERPGetFingerPrintTemplate")
End Sub

1 个答案:

答案 0 :(得分:1)

直接传递数组会有不同的结果,Integer的大小不同,您无法忽略SubFunction之间的区别。

我需要确定非托管签名,但是看看VB.NET签名我会将VBA重写为

Declare Sub ERPGetFingerprintImage Lib "C:\Desenv\EBioNet\EBioNet\Release\ERPDll.dll" (ByRef fingerprintimage As Byte, ByRef tam_image As Long)

要调用它,您将数组作为

传递
dim tam_image as long
tam_image = 256

dim buff() as byte
redim buff(1 to 1000)

ERPGetFingerprintImage buf(lbound(buf)), tam_image