Calling a DLL from VBA

2019-08-01 02:45发布

问题:

I need to call a DLL function from Access using VBA. The prototype for the DLL function is

int __stdcall myFunction(const char* data,
        int rows, int cols, int sl, int fullsize, 
        double aspect_ratio,
        double y,
        void** ppResult);

The one I declared in Access:

Private Declare Function DllImport_myFunction _
        Lib "myFunctionDLL.dll" _
        Alias "myFunction" _
            (ByVal data As String, _
             ByVal rows As Long, _
             ByVal cols As Long, _
             ByVal sl As Long, _
             ByVal fullsize As Long, _
             ByVal aspectRatio As Double, _
             ByVal y As Double, _
             ByRef handle As Long)

When I try to call from Access, Access crashed with an access violation. I placed a breakpoint in the first statement of the DLL function, but it was not hit.

Is the declaration incorrect?

回答1:

You are missing the return

Private Declare Function DllImport_myFunction Lib "myFunctionDLL.dll" Alias "myFunction" _
(ByVal data As String, _
 ByVal rows As Long, _
 ByVal cols As Long, _
 ByVal sl As Long, _
 ByVal fullsize As Long, _
 ByVal aspectRatio As Double, _
 ByVal y As Double, _
 ByRef handle As Long
) As Long

and probably need to add extern "C" to avoid mangling.