Just tried the following code and I can pass the array of strings to the C DLL from VB, thanks everyone help is truly appreciated.
VB
Code:
Private Declare Sub ReadOutArrayOfStrings _
Lib "MyC_Dll.dll" (theArray() As String)
Private Sub Command1_Click()
Dim StringArray(9) As String
StringArray(0) = "All"
StringArray(1) = "Work"
StringArray(2) = "And"
StringArray(3) = "No"
StringArray(4) = "Play"
StringArray(5) = "Makes"
StringArray(6) = "Jack"
StringArray(7) = "A"
StringArray(8) = "Dull"
StringArray(9) = "Boy"
ReadOutArrayOfStrings StringArray
End Sub
MyC_DLL.dll
Code:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <oleauto.h>
void __stdcall ReadOutArrayOfStrings(LPSAFEARRAY* StringArray)
{
char** StrPtr = 0;
long LowerBound = 0;
long UpperBound = 0;
int i;
SafeArrayGetLBound(*StringArray,1,&LowerBound);
SafeArrayGetUBound(*StringArray,1,&UpperBound);
SafeArrayAccessData(*StringArray, (void**)&StrPtr);
for(i = LowerBound; i <= UpperBound;++i)
MessageBox(HWND_DESKTOP,StrPtr[i],"Hi from the DLL",MB_OK);
SafeArrayUnaccessData(*StringArray);
}
MyC_DLL.def
Code:
EXPORTS
ReadOutArrayOfStrings
What I would like to do is modify the array somehow but when I try VB crashes. This is what I am trying (all code is exactly the same apart from the additional few lines in bold). Is there a reason for the VB program crashing, or am I just programming poorly ?
MyC_DLL.dll
Code:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <oleauto.h>
void __stdcall ReadOutArrayOfStrings(LPSAFEARRAY* StringArray)
{
char** StrPtr = 0;
long LowerBound = 0;
long UpperBound = 0;
int i;
SafeArrayGetLBound(*StringArray,1,&LowerBound);
SafeArrayGetUBound(*StringArray,1,&UpperBound);
SafeArrayAccessData(*StringArray, (void**)&StrPtr);
for(i = LowerBound; i <= UpperBound;++i)
MessageBox(HWND_DESKTOP,StrPtr[i],"Hi from the DLL",MB_OK);
for(i = LowerBound; i <= UpperBound;++i)
strcat(StrPtr[i],"_modified");
MessageBox(HWND_DESKTOP,StrPtr[i],"Hi from the DLL",MB_OK);
SafeArrayUnaccessData(*StringArray);
}
With this modified array is there a method for me to pass it back (or generally pass an array) to the VB program so I can manipulate or output the modified individual elements of the array.
I thank everyone for their help in dealing with my problems it is magnificient dealing with a forum that actively cares and helps its members. I think this post should be quite useful to others in the future as well!!
Cheers
ADDENDUM (For completeness):
To get this to work properly I needed to change the "Struct Member Alignment" in VC++6.0 to be "4 Bytes" because VB uses 4bytes alignment and VC++ uses 8bytes by default.
To change go to Project>Settings
Project Settings Dialogue appears
CLick on the DLL in the window and select 'All Configurations', click on the "C/C++" tab, then "Code Generation" in the "Category" combo box. Under "Struct Member Alignment" change from default 8bytes to 4bytes