![]() |
Passing an array of strings from VBA to C
Hi, I had asked this before but in a VB group they suggested I come
ask it he 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 |
Passing an array of strings from VBA to C
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 "Francogrex" wrote in message ... Hi, I had asked this before but in a VB group they suggested I come ask it he 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 |
All times are GMT +1. The time now is 04:37 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com