Passing an array of strings from VBA to C

  • Thread starter Thread starter Francogrex
  • Start date Start date
F

Francogrex

Hi, I had asked this before but in a VB group they suggested I come
ask it here:
I can pass single strings between VBA and C (dll) without problem.
But now I am trying to pass an array of strings from VBA to a C file
(as a test) but that is not working at all maybe someone can give
hints. I'm using VBA for excel 2003 on winXP. Thanks.

The C file that is made into 'mydll.dll"
#include "windows.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
__declspec (dllexport) void __stdcall CFUN(LPSTR **pout)
{
FILE *in=fopen("c:/excel.txt","w");
int i;
for (i=0;i<4;i++)
{
fprintf(in, "%s\n", *(pout+i));
}
fclose(in);

}

The VBA code:
Private Declare Function CFUN& Lib "c:/mydll.dll" (ByRef pout As
String)
Sub TEST()
Dim pout(4) As String
pout(0) = "one"
pout(1) = "two"
pout(2) = "three"
pout(3) = "four"
CFUN (pout(0))
End Sub

The excel.txt file does not contains the strings
 
You are only passing the value stored in the single element pout(0), ie
"one".

Also, in the declaration you've got ByRef pout As String
but I assume want to pass the entire array, so you'll want
ByRef pout() As String
and for your C function established to receive the string array

In passing just in case
Dim pout(4) As String
allocates a 5 element array (unless you are using Option Base 1)

Regards,
Peter T
 
Back
Top