دنبال کننده ها

۱۳۹۶ آذر ۹, پنجشنبه

Excel-VBA with C++ DLL sometimes crashes

[ad_1]



I made really simple c++ dll with only one function:



int DLL_EXPORT __stdcall foo(double *source)return 0;


and I'm trying to use it like that:



Option Explicit

Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
Private Declare PtrSafe Function FreeLibrary Lib "kernel32" (ByVal hLibModule As LongPtr) As Long

Private Declare PtrSafe Function foo Lib "MyLibrary.dll" (ByRef arr As Double) As Integer

Sub test_foo(n As Long)
Dim i As Long
Dim library_address As LongPtr
Dim library_path As String
library_path = "global_pathMyLibrary.dll"
library_address = LoadLibrary(library_path)

Dim arr() As Double
ReDim arr(1 To n) As Double

For i = 1 To n
arr(i) = CDbl(Cells(i, 1).Value)
Next

foo arr(1)

Do Until FreeLibrary(library_address) = 0
Loop

End Sub


and it usually works, but sometimes it crashes (Excel dies). Please tell me what is wrong?




[ad_2]

لینک منبع