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