Getting ByRef BSTR out of C++ into VB6
First up I'd like to thank Paul McKenzie & Igor Vartanov for helping me out on my first steps into C++, __stdcall was indeed the answer.
So on to the next hurdle, I have a VB6 app that calls a function in a VC++ (not .NET), this function returns an int 0 or 1 for success/fail and a string parameter is passed ByRef (example below). When this function is called from VB6 it returns success value of 1 but the ByRef only gives me the first character of the string I'm expecting.
C++
__declspec(dllexport) int __stdcall PassChars(BSTR* str)
{
char* pCharval = "this string";
CComBSTR ret(pCharval);
*str = ret.Copy();
::SysFreeString(ret);
return 1;
}
VB6
Private Declare Function PassChars Lib "D:\Projects\VB6 Projects\Pointers\References\Pointers.dll" (ByRef value1 As String) As Long
Dim ret As Long
Dim val As String
ret = PassChars(val)
MsgBox (val)
Message Box Displays: t
I have read as much as I can including Siddhartha's posts on converting strings but no-one has provided an example of ByRef that I have managed to see working.
Much respect to all C++ coders willing to help us noobs,
Thanks
Re: Getting ByRef BSTR out of C++ into VB6
The first question here is: what is this
Quote:
Originally Posted by trip-l
Code:
::SysFreeString(ret);
supposed to do? :confused:
Re: Getting ByRef BSTR out of C++ into VB6
My understanding, which is slight, is that ccombstr comes with potential memory leaks so ::SysFreeString() clears the ccombstr after I've copied it to str.
http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx - Explicitly Freeing the CComBSTR Object
Re: Getting ByRef BSTR out of C++ into VB6
Quote:
Originally Posted by
trip-l
No need to do that - its destructor will free the memory.