Calling a DLL from VBA

2019-08-01 02:29发布

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条回答
别忘想泡老子
2楼-- · 2019-08-01 03:16

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.

查看更多
登录 后发表回答