I'm using Code::Blocks to write a DLL in C which I intend to use in a Winbatch script but for the moment I'm testing it using Excel VBA. The moment the VBA script runs the DLL getVersion() function Excel crashes. Searching on the internet for several days: I did not find a proper solution.
The C code is like this
#define MQTTPUB_VERSION "V3.1.1Test"
DLL_EXPORT BSTR __stdcall WINAPI getVersion(void)
{
return MQTTPUB_VERSION ;
}
The VBA code is like this
Public Declare Function mqttPubMsg Lib "mqttPubMsg.dll" _
(ByVal MQTT_ADDRESS As String, ByVal MQTT_CLIENTID As String, ByVal MQTT_TOPIC As String, ByVal MQTT_PAYLOAD As String) As Long
Public Declare Function getVersion Lib "mqttPubMsg.dll" () As String
Sub Test_DDL_mqttPubMsg()
'
' to test mqttPubMsg.DLL used in Visual Basic (VBA)
'
Dim DLLVersion As String * 35
Dim WorkDir As String
DLLVersion = Space(35)
WorkDir = ThisWorkbook.Path
ChDir WorkDir
If Dir(WorkDir & "\mqttPubMsg.dll", vbDirectory) = vbNullString Then
MsgBox "DLL not found"
Else
On Error GoTo DLLError
DLLVersion = getVersion() 'Excel crashes on executing this statement
End If
MsgBox ("Version DDL: " & DLLVersion)
Exit Sub
DLLError:
MsgBox ("DDL error")
End Sub
A C program calling the DLL and the getVersion() function works OK.
What could be the cause of this runtime error and how to solve it.
Thanks in advance.