msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

VB6 and fixed length strings

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Enterprise
Author Message
Bit Byte



Joined: 04 Oct 2007
Posts: 2

PostPosted: Thu Sep 21, 2006 10:57 am    Post subject: VB6 and fixed length strings Reply with quote

There appear to be some (undocumented?) side effects when using fixed
length strings in VB6 - in that one can't seem to be able to concatenate
them to other strings for example, the rest of the string is lost.

An example below will hopefully, illustrate the problem further:

I have a function defined in C as ff

void getStringById(const int id, char* str)
{
if (str && strlen(str)
{
size_t size = strlen(str);
// ... some logic
stncpy(str, the_string, size);
}
}

In VB, I declare the function like this:

Public Declare function getStringById lib "myfuncs.dll" (byval id as
long, byref str_param as string)


A simple example of how I use the function in VB6:


public sub Foo(byval Id as long)
dim sName as string
dim sResult as string

sName = string(256,"*") '//so a null ptr is not passed to C func
getStringById(Id, sName)

sResult = "The name for id : " & Id & " is : " & sName
MsgBox sResult

end sub


When the message box is displayed, it only displays: "The name for id :"

sName contains the obtained string, (padded with cars to make the 256
length) - but I can't seem to use the returned string (as the above
example shows). Any ideas on how to obtain a string from C and use it in
VB (hopefully, without reverting to BSTR?)

Archived from group: microsoft>public>vb>enterprise
Back to top
View user's profile Send private message
Anthony Jones



Joined: 04 Oct 2007
Posts: 25

PostPosted: Thu Sep 21, 2006 1:29 pm    Post subject: Re: VB6 and fixed length strings Reply with quote

"Bit Byte" wrote in message@bt.com...
> There appear to be some (undocumented?) side effects when using fixed
> length strings in VB6 - in that one can't seem to be able to concatenate
> them to other strings for example, the rest of the string is lost.
>
> An example below will hopefully, illustrate the problem further:
>
> I have a function defined in C as ff
>
> void getStringById(const int id, char* str)
> {
> if (str && strlen(str)
> {
> size_t size = strlen(str);
> // ... some logic
> stncpy(str, the_string, size);
> }
> }
>
> In VB, I declare the function like this:
>
> Public Declare function getStringById lib "myfuncs.dll" (byval id as
> long, byref str_param as string)
>

By using ByRef you indicate the function takes a BSTR. You C function does
will see the first byte as 00 since the unicode value for '*' is 002A.
Hence strlen sees the length of the supplied string as 0.

You need to change the declare to:-

Public Declare function getStringById lib "myfuncs.dll" (byval id as long,
ByVal str_param as string)

Now the string will be converted to ANSI before being passed the C function.
Counter intuiatively a string is converted back from ANSI and used to
replace the original value. I know it doesn't look right but try it and see.
The meaning of byval in a function declare when the parameter is a string
has been overloaded. A poor language design choice IMO but there it is.


>
> A simple example of how I use the function in VB6:
>
>
> public sub Foo(byval Id as long)
> dim sName as string
> dim sResult as string
>
> sName = string(256,"*") '//so a null ptr is not passed to C func
> getStringById(Id, sName)
>
> sResult = "The name for id : " & Id & " is : " & sName
> MsgBox sResult
>
> end sub
>
>
> When the message box is displayed, it only displays: "The name for id :"
>
> sName contains the obtained string, (padded with cars to make the 256
> length) - but I can't seem to use the returned string (as the above
> example shows). Any ideas on how to obtain a string from C and use it in
> VB (hopefully, without reverting to BSTR?)
>
Back to top
View user's profile Send private message
Bit Byte



Joined: 04 Oct 2007
Posts: 2

PostPosted: Thu Sep 21, 2006 5:54 pm    Post subject: Re: VB6 and fixed length strings Reply with quote

Anthony Jones wrote:
> "Bit Byte" wrote in message
> @bt.com...
>
>>There appear to be some (undocumented?) side effects when using fixed
>>length strings in VB6 - in that one can't seem to be able to concatenate
>>them to other strings for example, the rest of the string is lost.
>>
>>An example below will hopefully, illustrate the problem further:
>>
>>I have a function defined in C as ff
>>
>>void getStringById(const int id, char* str)
>>{
>> if (str && strlen(str)
>> {
>> size_t size = strlen(str);
>> // ... some logic
>> stncpy(str, the_string, size);
>> }
>>}
>>
>>In VB, I declare the function like this:
>>
>>Public Declare function getStringById lib "myfuncs.dll" (byval id as
>>long, byref str_param as string)
>>
>
>
> By using ByRef you indicate the function takes a BSTR. You C function does
> will see the first byte as 00 since the unicode value for '*' is 002A.
> Hence strlen sees the length of the supplied string as 0.

This was actually a typo (the code would not have worked as posted - as
I would have been passing a ptr to a ptr. Anyway, I later fixed this by
cleaning the string in VB (i.e. replacing non-printable chars by a
whitespace (char 32) and then trimming the returned string).

>
> You need to change the declare to:-
>
> Public Declare function getStringById lib "myfuncs.dll" (byval id as long,
> ByVal str_param as string)
>
> Now the string will be converted to ANSI before being passed the C function.
> Counter intuiatively a string is converted back from ANSI and used to
> replace the original value. I know it doesn't look right but try it and see.
> The meaning of byval in a function declare when the parameter is a string
> has been overloaded. A poor language design choice IMO but there it is.
>
>
>
>>A simple example of how I use the function in VB6:
>>
>>
>>public sub Foo(byval Id as long)
>>dim sName as string
>>dim sResult as string
>>
>> sName = string(256,"*") '//so a null ptr is not passed to C func
>> getStringById(Id, sName)
>>
>> sResult = "The name for id : " & Id & " is : " & sName
>> MsgBox sResult
>>
>>end sub
>>
>>
>>When the message box is displayed, it only displays: "The name for id :"
>>
>>sName contains the obtained string, (padded with cars to make the 256
>>length) - but I can't seem to use the returned string (as the above
>>example shows). Any ideas on how to obtain a string from C and use it in
>>VB (hopefully, without reverting to BSTR?)
>>
>
>
>
Back to top
View user's profile Send private message
Bob O`Bob



Joined: 04 Oct 2007
Posts: 1456

PostPosted: Fri Sep 22, 2006 1:58 pm    Post subject: Re: VB6 and fixed length strings Reply with quote

Bit Byte wrote:

> This was actually a typo (the code would not have worked as posted - as
> I would have been passing a ptr to a ptr. Anyway, I later fixed this by



There's a lesson in there for you to learn, and if there's a next time
it could be a very painful one.

You may already have put a major crimp in your future ability to get assistance
from many of the most frequent contributors - who expect you to be able to use
copy & paste.

It is a complete waste of time to try to help someone with code which
isn't actually the code they're seeking help for. Don't waste
the very resource you're begging.



Bob
Back to top
View user's profile Send private message
MikeD



Joined: 04 Oct 2007
Posts: 3348

PostPosted: Sat Sep 23, 2006 12:13 am    Post subject: Re: VB6 and fixed length strings Reply with quote

"Bob O`Bob" wrote in message @TK2MSFTNGP02.phx.gbl...
> Bit Byte wrote:
>
>> This was actually a typo (the code would not have worked as posted - as I
>> would have been passing a ptr to a ptr. Anyway, I later fixed this by
>
>
>
> There's a lesson in there for you to learn, and if there's a next time
> it could be a very painful one.
>
> You may already have put a major crimp in your future ability to get
> assistance
> from many of the most frequent contributors - who expect you to be able to
> use
> copy & paste.
>
> It is a complete waste of time to try to help someone with code which
> isn't actually the code they're seeking help for. Don't waste
> the very resource you're begging.
>

And just to add another comment.....

NOTHING is more annoying to the people who *volunteer* their time and
answer/solve a problem only to find out the answer was basically useless
because the code that was posted was not the *actual* code used in the
program.

As a side comment, it's 1000x easier and faster to copy and paste your code
than it is to retype it.

--
Mike
Microsoft MVP Visual Basic

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Enterprise All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group