Click to See Complete Forum and Search --> : Arrays


avni
February 23rd, 2000, 10:54 AM
Hi,

I am writing code that uses a DLL. The function is that uses the DLL, does not take any values in. It ouptputs a really long string. I have the same code in Visual C++, where the value that is returned is of type char[33].

How can I do the same in Visual Basic? I tried to define an array as follows:

option explicit
private HostId() as string

then the function definition in a module

Declare Function GenerateId Lib "PLId.DLL" (ByRef HostId() As String) As Long

this is how I'm using the function:

dim rc as long

rc = GenerateId(HostId())

This gives me an exception. If I define the array with the size like private HostId(33) as string, then the function gives me invalid function arguments error.

What I am doing wrong??? Please help!!!
Thank you in advance.
Avni

Kyle Burns
February 23rd, 2000, 11:04 AM
A VB String handles the array of characters for you. What you are telling the compiler in your declare (kind of) is long int GenerateId(char &HostId[][]);

avni
February 23rd, 2000, 11:16 AM
Hi,
That's what I thought too, but I keep getting errors when I declared the ID as just string.
I tried again to declare the ID as string, in both the function declaration and the function call. Now, the ID returns empty string....

Thank you
avni

Kyle Burns
February 23rd, 2000, 11:31 AM
Did you try sending it a "padded" string?

avni
February 23rd, 2000, 11:39 AM
Hi Kyle,

No. I don't really know how to do that... :)
Can you please provide an example???
Thank you for your help...

Avni

Kyle Burns
February 23rd, 2000, 12:06 PM
Just doing a little brushing up on my API. VB stores string variables with the C datatype BSTR. This is more than just an array of characters. The first four bytes are information on the size of the string. Most C/C++ functions that take a string argument expect a LPSTR. The book I'm reading says that VB will always pass strings by reference no matter what the declaration. If you specify ByRef, it will pass a reference to a BSTR. If you specify ByVal, it will pass a reference to a LPSTR. You might want to check this out.

avni
February 23rd, 2000, 12:16 PM
Hi Kyle,

If I pass the string in the DLL as byref, i get exception error... If I pass as byval, then my program gives error, invalid function arguments...

Thanks for all your help.

Avni

Kyle Burns
February 23rd, 2000, 12:52 PM
Dim sRetVal as string
sRetVal = Space$(33)
'call the function

avni
February 23rd, 2000, 01:43 PM
Hi Kyle,

Thank you very very much.... The code is working with the addition of that code snippet you wrote...
Thank you... I really appreciate all your help.

avni