CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How can return a String from C++ DLL to VB?

  1. #1
    Join Date
    Jun 2000
    Posts
    11

    How can return a String from C++ DLL to VB?

    I am returning a HASH DEFINE string from a DLL developed using C++ to VB as BSTR. But in some cases VB cannot get the string at all.What all should I do for this to get the same correctly. Can you suggest me with a better idea.

    My C++ code is as given below:

    BSTR __stdcall ReturnFn( )
    {
    return (BSTR)_T("RETURN VALUE");
    }

    Thanks in advance




  2. #2
    Join Date
    Sep 2000
    Location
    Ottawa, Ontario
    Posts
    356

    Re: How can return a String from C++ DLL to VB?

    Try returning a char instead. BSTR is a 32bit pointer so you need to get return ByRef and not ByVal

    Jean-Guy


  3. #3
    Join Date
    May 1999
    Location
    Germany
    Posts
    106

    Re: How can return a String from C++ DLL to VB?

    Hi,

    your code cannot work, because you just cast a string to a BSTR. But a BSTR has a 4 Byte header in front of its actual text, holding the length of the text.

    Try

    BSTR __stdcall ReturnFn( )
    {
    return SysAllocString(OLESTR("RETURN VALUE"));
    }

    OLESTR("text") is the same as L"text", but you should use OLESTR("text").

    The memory of the string is freed by the VB-client.

    Hope this helps
    Peter


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




On-Demand Webinars (sponsored)