Click here to Skip to main content
15,509,458 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Here is an example of a problem I am having.

C++ Lib
BSTR testlib::f4str(const long fPtr)
{
_variant_t retVal = dataRS->Fields->Item[_variant_t(fPtr)]->Value;
BSTR retStr = SysAllocStringLen(retVal.bstrVal,dataRS->Fields->Item[_variant_t(fPtr)]->DefinedSize);
    return retStr;
}


VB6 Function
Declare Function f4str$ Lib "testlib.dll" (ByVal fPtr&)


So when i run this function i get back the string but every character of the string is seperated by a SOH character (start of header) i see it as a small box charcter that cannot be rendered. I know its an SOH because I put it into an editor that renders whitespace for me.

What I have sorta worked out so far is that the BSTR in C++ is not the same as the BSTR for VB6 so when I send it back I get that.

It should be noted that If I just type a string in and send it back it works fine.. so it only happens when I convert from _variant_t to BSTR.

thanks in advance
Posted
Updated 14-Mar-16 5:56am
Comments
Sergey Alexandrovich Kryukov 23-Jul-13 16:40pm    
Any particular reason to work with VB6? This is a dead thing. Keep working, and you will find a lot more problem which not many would like to consider...
—SA
Member 3183884 23-Jul-13 16:46pm    
well the goal is to get off of it after this conversion. It will tremendously help us as we build the new system if we can have the data be from the same source. Also this system is 250,000 lines of code and we have many users. So a piecemeal approach seems in order.
Sergey Alexandrovich Kryukov 23-Jul-13 17:18pm    
Well, my point is: doing anything in VB6 is a good way to loose... I wonder why?
—SA
Prasad Khandekar 23-Jul-13 16:58pm    
Hello Member,

Please have a look at this CP article (http://www.codeproject.com/Articles/4829/Guide-to-BSTR-and-C-String-Conversions).

Regards,
[no name] 23-Jul-13 20:07pm    
looks the business. Why don't you post as a solution.

change your string interface to BSTR or char* type and always avoid to interchange non-trivial objects as strings (which arent arrays of char).

VB 6 is a pain, but if you are to stuck on it with the big code base, I wonder why there isnt some working functions for that.

Never ever use objects betweens C++ and VB6. This features only works with VB.net :mad:

What the heck is dataRS?
 
Share this answer
 
Comments
Member 3183884 24-Jul-13 9:15am    
dataRS is an ADODB.Recordset

so I took your suggestion and changed the c++ code to the following

LPSTR testlib::f4str(const long fPtr)
{
return (char*)dataRS->Fields->Item[_variant_t(fPtr)]->Value.bstrVal;
}

Which fixed the problem but caused another

now I have garbage at the end of my string
"4573 BUNKER HILL ROAD  ?a =?3 C e Idsa; ss d c"

length of 77

what I should be getting for the address field is
"4573 BUNKER HILL ROAD  "

which is 45 characters long

how can I make sure that it gets chopped down correctly I tried adding SysAllocStringLen
however it went back to the same problem as before.
KarstenK 24-Jul-13 9:27am    
This wont work!!!

You should also change the input param of 4str(const long fPtr)

you can use a big byte buffer as input param, which get filled. The return code should be a long result with error/success
Member 3183884 24-Jul-13 9:50am    
|This wont work!!!

so.. are you saying there is no possible way to get this as a string that VB6 can read it as a string. Ok.... somehow that seems wrong
KarstenK 24-Jul-13 10:00am    
you cant (char*) an bstr.

you better learn to google. I found this one:

http://stackoverflow.com/questions/6134666/pass-bstr-from-c-dll-function-to-vb6-application
Member 3183884 24-Jul-13 10:19am    
Yea.. and that works perfectly... the conversion from _variant_t does not. I did learn to google and I already found that thank you
To return a String from a dll to VB6.

Declare in VB6
VB
Public Declare Function QueryString Lib "lihelper" Alias "_QueryString@4" (ByVal key As String) As String

Call in VB6
VB
Dim s as String : s = QueryString("find me")


Define in C++ DLL
C++
extern "C" __declspec(dllexport) BSTR __stdcall QueryString(LPCSTR key{
  const std::string response(key? odbc::sql::query(key) : std::string(""));
  return SysAllocStringByteLen(response.c_str(), response.size());
}

Vb6 will always convert a returning string from a DLL as if it's multibyte, that's why your Unicode-strings will only contain their first character (unless it's >0xFF of course).
Normally I allocates a buffer of reasonable predefined size and sees if the returning data fits. The function the returning the needed size. If the needs are greater, I'll give in and reallocates ...
 
Share this answer
 
Comments
jeron1 14-Mar-16 11:58am    
I hope this issue has been fixed, it's been almost three years.
Jonas Hammarberg 14-Mar-16 23:42pm    
I certainly hope so but as this page was in the result when I googled, I thought it might be proper to answer it ... albeit a bit *hmpf* late.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900