David,
Ok after a few system crashing reboots I gave up on trying to just return a CString *g* That's not to say it can't be done, but it's not as easy as just doing it.
Here's the C++ I tried:
CString CCstringApp::ReturnIt( int n )
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString cReturnString;
int i;
cReturnString = "This is a C++ CString class return value";
for ( i = 0; i < n; i++ )
cReturnString = cReturnString + cReturnString;
return ( cReturnString );
}
LPSTR CCstringApp::ReturnMyBuffer(LPSTR buffer, int n)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString cReturnString;
int i;
cReturnString = "This is a C++ CString class cast as LPSTR return value in the provided buffer";
for ( i = 0; i < n; i++ )
cReturnString = cReturnString + cReturnString;
strcpy( buffer, cReturnString );
return ( buffer );
}
and the VFP code:
x = space(1024)
declare string ReturnIt in cstring.dll integer n
x = ReturnIt( 2 )
x = space(1024)
declare string ReturnMyBuffer in cstring.dll string @buffer, integer n
y = ReturnMyBuffer( @x, 2 )
?len(y)
? y
? x
The ReturnIt causes a crash which makes sense because space is allocated in the C++ stack which is destroyed before you really get back to VFP. It might be possible if the CString were some static data member of the DLL.
But in general most routines that create string return values expect the caller to have allocated the memory to hold the value. Which is what the ReturnMyBuffer does, it works with a CString, then copies it into the supplied buffer and also returns the buffer address. So it looks like you might have to wrap their DLL with your own. You probably also want to pass in max length so you don't overrun the buffer and trash VFP memory.
>Thanks for the info. I'll try and see how it goes. This DLL is produced by other group I don't have the source code.
>According to Paul Vlad Tatavu Cstrings from MFC cannot be exported to C.
>I need to find the way for this to work. Maybe I would need to do an intermediate DLL in C++ to access this and export this in C strings. I hope I don't need to add another interface !