Click to See Complete Forum and Search --> : Passing string back to VB from C++ DLL


tleichen
December 9th, 1999, 03:50 PM
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?

Chizl
December 10th, 1999, 12:49 AM
VB will not understand this. To make it generic you will have to pass back a BSTR *

--
Chizl
chizl@NOSPAM.karland.com
http://www.chizl.com/

tleichen
December 10th, 1999, 03:25 PM
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!

Chizl
December 10th, 1999, 09:14 PM
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
chizl@NOSPAM.karland.com
http://www.chizl.com/

Sukumar
December 16th, 1999, 09:54 PM
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

vbfingers
December 26th, 1999, 08:23 PM
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...