Call C++ DLL from VBA - problem with string and LPCSTR*

renatoab

New Member
Joined
Jun 14, 2009
Messages
32
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

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
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
See if How To Pass a String Between Visual Basic and Your C DLL helps, specifically the DisplayStringByRef C function and the VB/VBA declaration and call.

I'd seen this example before. I tried this way, but it didn't work in my case.
This example is a little bit different: (1) the example has a string, I have an array if strings (2) I use VBA while the example uses VB.
I'm not sure if this makes any difference, but didn't work for me.

Thank you
 
Upvote 0
Problem when passing LPCSTR* to dll -> trying to debug -> problem when trying to write text file

I'm trying to debug to see what happens inside the dll, to fix the problem. I'm writing a text file in the dll (using fopen c++ command) to print the values but I'm having problem with it.

When I run the dll from other sources, the txt is generated with no problem. But when I run from VBA, an error occurs, and Excel closes.
I know the problem is with the text file, because without it the dll runs (with wrong results because my difficult with LPCSTR). I don't think there is any problem with directories, because the xlsm, dll and txt are in the same directory, in which I have writing permission, and is included in the environment variable PATH. I tried absolute and relative references, but the same occurs.

Do someone know anything different about writing text files from a dll called from VBA?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top