Hi,
I'm having problems when I try to call a function in a C++ DLL from VBA.
There's a function "execute" in a dll. I don't have the dll code, but I have a C++ function "run_execute" that calls that function "execute", and I'm trying to do the same on a VBA code.
I'm not sure what's going wrong but there's something about string arguments.
There are arguments of type char*, that I pass as String, Byval. It seems to be working, because the error occurs after reading files that I pass this way as string.
But there are arguments of type LPCSTR*, which I use to pass arrays of string, that I don't know how to deal with. I saw some examples, that seemed to work in a simple way, like simply using string, or using pointer to string, declaring an array of type LongPtr.
Does anyone know what I'm doing wrong, and how can I make it work? Is there anything about string length? Is there anything about usign system directory for dll?
Thank you.
Here's some sample of what I'm doing
I'm having problems when I try to call a function in a C++ DLL from VBA.
There's a function "execute" in a dll. I don't have the dll code, but I have a C++ function "run_execute" that calls that function "execute", and I'm trying to do the same on a VBA code.
I'm not sure what's going wrong but there's something about string arguments.
There are arguments of type char*, that I pass as String, Byval. It seems to be working, because the error occurs after reading files that I pass this way as string.
But there are arguments of type LPCSTR*, which I use to pass arrays of string, that I don't know how to deal with. I saw some examples, that seemed to work in a simple way, like simply using string, or using pointer to string, declaring an array of type LongPtr.
Does anyone know what I'm doing wrong, and how can I make it work? Is there anything about string length? Is there anything about usign system directory for dll?
Thank you.
Here's some sample of what I'm doing
Code:
' I'm using here LongPtr, but I also tried String
' I'm running this code on Office 2010 32 bits
Public Declare Function runTR Lib "C:\myfolder\mydll.dll" _
Alias "_execute@48" _
(ByVal flag1 As Long, _
ByVal flag2 As Long, _
ByVal flag3 As Long, _
ByVal namefile1 As String, _
ByVal namefile2 As String, _
ByRef pointer_descritorEntrada() As LongPtr, _
ByRef pointer_descritorSaida() As LongPtr, _
ByRef valorEntrada() As Double, _
ByRef valorSaida() As Double) As Long
sub run
' I'm skiping the long and double declarations, to show only the main problem
Dim namefile1 As String
Dim namefile2 As String
namefile1 = "C:\myfolder\Example.plx"
namefile2 = "C:\myfolder\Example.cfg"
Dim dEntrada(1) As String
Dim dSaida(0) As String
dEntrada(0) = "Instruction_1"
dEntrada(1) = "Instruction_2"
dSaida(0) = "Result_tag_1"
Dim pointer_dEntrada(1) As LongPtr
Dim pointer_dSaida(0) As LongPtr
pointer_dEntrada(0) = StrPtr(dEntrada(0))
pointer_dEntrada(1) = StrPtr(dEntrada(1))
pointer_dSaida(0) = StrPtr(dSaida(0))
dim error_index as Long
error_index = runTR(flag1, _
flag2, _
flag3, _
namefile1, _
namefile2, _
pointer_dEntrada, _
pointer_dSaida, _
valorEntrada, _
valorSaida)
end sub