I'm trying to use a call in a library which is requesting a char array and I'm not sure how I can provide that through VBA.
Every call I'm trying to make is causing excel to crash fantastically with no error message or feedback.
Here is the documentation I'm looking at (With function name changed):
FunctionABC( unsigned short nCount,
char *ServiceName,
unsigned short Starting,
char *Point[],
unsigned short nPoint,
double Value[],
char *Time[],
unsigned short nTime,
char *Status[],
unsigned short nStatus,
char *Desc[],
unsigned short nDesc,
char *Units[],
unsigned short nUnits
);
The return value is an integer where a non-zero number is returned if the call is unsuccessful.
Here is the code I wrote in excel...
Public Declare PtrSafe Function FunctionABC Lib "SOMEAPI.dll" (ByVal nCount As Integer, _
ByVal ServiceName As String, ByVal nStarting As Integer, Point() As String, _
nPoint As Integer, value() As Double, Time() As String, nTime As Integer, _
Status() As String, nStatus As Integer, Desc() As String, nDesc As Integer, Units() As String, _
nUnits As Integer) As Integer
Sub test()
Dim nCount As Integer
Dim ServiceName As String
Dim nStarting As Integer
nCount = 1
ServiceName = "DMS.UNIV5"
nStarting = 0
Dim Point(1) As String
Dim nPoint As Integer
Dim value(1) As Double
Dim Time(1) As String
Dim nTime As Integer
Dim Status(1) As String
Dim nStatus As Integer
Dim Desc(1) As String
Dim nDesc As Integer
Dim Units(1) As String
Dim nUnits As Integer
Dim retval As Integer
retval = FunctionABC(nCount, ServiceName, nStarting, Point, nPoint, value, Time, nTime, Status, nStatus, Desc, nDesc, Units, nUnits)
End Sub
So trying to run this code in VBA freezes the UI and causes excel to restart. I don't get any feedback or error message so it's difficult to tell what the issue is, but I'm really not sure how to pass an unsized char array by reference from VBA over to this DLL like the documentation is requesting, and searching around has not provided any clear results.
I tried a lot of variations of this call, adding and removing symbols, using different data types like ListObject
, trying to dynamically resize, and such but no luck, everything I pass in makes excel implode on itself.
At one point, having unsized arrays declared caused excel to crash when I tried to just 'view' the code. I ended up having to disable macros and go into Visual Studio to specify an array size in order to stop the crashes from happening.
The most frustrating thing about all of this is that there is no error message or log for me to debug so I'm just blindly guessing at this point on what is going on.