CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 1999
    Posts
    8

    Passing string back to VB from C++ DLL

    I have a C++ DLL routine that returns a pointer to a character buffer that is created and managed by the DLL. The pointer is defined as a string in VB and is passed by reference. After multiple successful calls to the DLL routine, the VB program invariably gets either an "Out of memory" error or a memory access error. It appears that VB is claiming this storage as string space, and thus "cleaning it up". Is there any way to avoid this?




  2. #2
    Join Date
    Dec 1999
    Location
    Dallas, Texas, USA
    Posts
    59

    Re: Passing string back to VB from C++ DLL

    VB will not understand this. To make it generic you will have to pass back a BSTR *

    --
    Chizl
    [email protected]
    http://www.chizl.com/

  3. #3
    Join Date
    Dec 1999
    Posts
    8

    Re: Passing string back to VB from C++ DLL

    I am passing a string by reference from VB. What happens is this: I have a fixed buffer with some character "segments" in it. The segments are not null terminated. There IS a null following the last segment, however. The DLL is written to merely pass back an address to a character segment and a length. Since VB is passing the address by reference, and the DLL is changing it, each subsequent call gets a new segment and length. I don't have to do a specific string copy. The following problems are only experienced by VB. The first problem I had was what happens at the end of the string (last segment). My pointer was pointing to the null at the end of my data. When I passed this back to VB, it blew up. Apparently, it doesn't like a null string, even though the program would not reference it later. So, at the end, I set the pointer back to the beginning of my buffer (not going to use it anyway), with a length of zero. This all works fine, except when I try to reuse the buffer on another call, much of my buffer is corrupted and I get errors that that some memory address cannot be read or written. It appears that VB, at this point, is trying to manage the DLL's buffer as part of his string space!






  4. #4
    Join Date
    Dec 1999
    Location
    Dallas, Texas, USA
    Posts
    59

    Re: Passing string back to VB from C++ DLL

    The only success I have ever had with passing strings back and fourth between VB and C++ is BSTR *. You can pass it from VB as String and recieve it as a BSTR in C++, but from C++ to VB you must do something like this:
    This is a DLL I'm writing using Winsock in ATL. I have a property called MailReturnTo for email address. It's both a get and put.

    STDMETHODIMP CISMTP::get_MailReturnTo(BSTR *pVal)
    {
    USES_CONVERSION;
    *pVal = SysAllocString(A2W(m_Mail_Return_To));
    return S_OK;
    }




    This seems to work nicely with other C++ apps and VB. You can also go to http://www.sourcesite.simplenet.com/visualbasic/ to find a demo I did from VC5 and VB5. Both sides of the code is there. That was the first time I had tried writing a com object that would pass information back and fourth between VB and C++. It's really my first C++ test application for the COM world.


    --
    Chizl
    [email protected]
    http://www.chizl.com/

  5. #5
    Join Date
    Dec 1999
    Location
    India, Hyderabad
    Posts
    8

    Re: Passing string back to VB from C++ DLL

    Hi there,
    I once had the same kinda problem with my applicaiton. If possible check out the MSDN online support site and there U can get some more info about it.
    You can go to
    http://support/microsoft.com/kb/articles/Q118/6/43.asp

    Hope this will solve your problem.

    Bye
    Suku


  6. #6
    Join Date
    Sep 1999
    Location
    Northern Utah, U.S. of America
    Posts
    38

    Re: Passing string back to VB from C++ DLL

    Hey tleichen,

    In 'letting my fingers do the walkin', I came across your question and it reminded me of some struggling times in creating a DLL thats currently being in a Y2K package. The VC++ DLL file that I made is a NON-MFC one. Here is some examples about string passing...


    DLL declaration statement and procedure declaration...

    short WINAPI MM_CONVERTDATE(char *indate1, char *outdate1, char *tsw, int tswfile,
    int lcntryval) ;

    The VB Declaration...
    Declare Function MM_CONVERTDATE Lib "milmagyk.dll" (ByVal indate1 As String, _
    ByVal outdate1 As String, ByVal tsw As String, ByVal tswfile As Long, _
    ByVal lcntryval As Long) As Integer

    But a word of warning!!!... is that the string size, in this example MUST be the same size!!
    When using VC++ DLL's with VB applications, there are some rules you need to follow, or it could be distrastorous!!

    RSVP if needed.

    VBFingers...




Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured