Getting ByRef BSTR out of C++ into VB6
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Getting ByRef BSTR out of C++ into VB6

  1. #1
    Join Date
    Oct 2010
    Posts
    5

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,472

    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?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2010
    Posts
    5

    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    Re: Getting ByRef BSTR out of C++ into VB6

    Quote Originally Posted by trip-l View Post
    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
    No need to do that - its destructor will free the memory.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center