Problem passing arrays of strings to C DLL 
Author Message
 Problem passing arrays of strings to C DLL

I'm having problems passing an array of strings to a C DLL. The first
element of the array appears OK in the dll while the remaining elements
result in garbage or an access violation.  This DLL function must also
be callable from C (which works).  I've included code snippets below.
Any help would be greatly appreciated, including pointing me to
examples.

Thanks in advance,
Gerald Aden

THE VB CODE

Private Declare Function foo Lib "test.dll" (ByRef sNames As String) As

Long Public Sub callfoo() As Long
        Dim data(0 to 2) As String * 128

        data(0) = "person1"
        data(1) = "person2"
        data(2) = "person3"

        foo data(0)

End Sub

THE C CODE

extern "C" long WINAPI foo(char* names[])
{
        int i;

        for (i = 0; i <= 2; i++) {
                printf("Name[%d] = %s", i, names[i]);
        }

Quote:
}

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.


Mon, 03 Dec 2001 03:00:00 GMT  
 Problem passing arrays of strings to C DLL
Gerald,

I suggest you try using SAFEARRAYS instead... it will make your life a whole lot
easier.

~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please post/reply to the newsgroup(s) so
that everyone can benefit from the discussion.

Klaus H. Probst, MCP

       ICQ: 22454937
      The VB Box: http://members.xoom.com/kprobst/
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quote:

>I'm having problems passing an array of strings to a C DLL. The first
>element of the array appears OK in the dll while the remaining elements
>result in garbage or an access violation.  This DLL function must also
>be callable from C (which works).  I've included code snippets below.
>Any help would be greatly appreciated, including pointing me to
>examples.



Mon, 03 Dec 2001 03:00:00 GMT  
 Problem passing arrays of strings to C DLL
The phrase "Long Public Sub callfoo() As Long" is way off!

"Public Sub callfoo()" for a sub or...
"Public Function callfoo() as Long" for a function

Also try

Instead of "foo data(0)"
Try "foo data()"

Quote:

> I'm having problems passing an array of strings to a C DLL. The first
> element of the array appears OK in the dll while the remaining elements
> result in garbage or an access violation.  This DLL function must also
> be callable from C (which works).  I've included code snippets below.
> Any help would be greatly appreciated, including pointing me to
> examples.

> Thanks in advance,
> Gerald Aden

> THE VB CODE

> Private Declare Function foo Lib "test.dll" (ByRef sNames As String) As

> Long Public Sub callfoo() As Long
> Dim data(0 to 2) As String * 128

> data(0) = "person1"
> data(1) = "person2"
> data(2) = "person3"

> foo data(0)

> End Sub

> THE C CODE

> extern "C" long WINAPI foo(char* names[])
> {
> int i;

> for (i = 0; i <= 2; i++) {
> printf("Name[%d] = %s", i, names[i]);
> }
> }

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.



Mon, 03 Dec 2001 03:00:00 GMT  
 Problem passing arrays of strings to C DLL
Ken,

Quote:
>Instead of "foo data(0)"
>Try "foo data()"

That will not work. In the C side you're expecting a pointer to the array, but
in any case, VB will not let you pass the entire array as an argument, unless
you use the proper "Arg() As Type" declaration, in which case VB will pass a
SAFEARRAY anyway, not a pointer.
The correct method is to pass the first element of the array.

~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please post/reply to the newsgroup(s) so
that everyone can benefit from the discussion.

Klaus H. Probst, MCP

       ICQ: 22454937
      The VB Box: http://members.xoom.com/kprobst/
~~~~~~~~~~~~~~~~~~~~~~~~~~~



Mon, 03 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. how to pass a string array to mfc dll

2. Passing string Array from VB3 to DLL

3. passing (string) arrays from VB to C-DLL and vice versa

4. Passing String Arrays to API DLLs

5. passing string arrays to dll

6. Passing array of strings to a dll in VB

7. help with pass string array to C DLL procedure

8. Passing string Arrays from Fortran DLL to Visual Basic

9. Passing VB String array to DLL???

10. passing vb5 arrays of strings to a vc++ 5 C dll

11. Passing Array of Strings to DLL

12. Passing string arrays to DLL (32 bit)

 

 
Powered by phpBB® Forum Software