The C++ string class and a VB string are totally different things. Look at the thread "returning a struct from a dll" or something like that to see the solution to your problem. The thread was started by Chris and is two pages long. The thing you need is very near to the end.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Thanks for your help CornedBee.
I found the topic, however, I still can't figure out which
information in that post helps me. If I'm supposed to use BStr,
can you give me an example....I'm not terribly comfortable with
C++ yet.
Thanks for your help CornedBee.
I found the topic, however, I still can't figure out which
information in that post helps me. If I'm supposed to use BStr,
can you give me an example....I'm not terribly comfortable with
C++ yet.
// Map the External CALLBACK function
if (lAddress != NULL || lAddress > 0)
{
//Map the external callback function
MyExGetNameProc = (ExGetNameProc)lAddress;
// Create + Fillup the tmp character array
Buff= new char[128];
memset(Buff, '\0', 128);
sprintf(Buff, "This is the return C String to Visual Basic through the callback function.");
// Allocate BSTR buffer
bstr = SysAllocStringLen(NULL, lstrlen(Buff));
// convert from char to BSTR
MultiByteToWideChar(CP_ACP,
0,
(LPSTR)Buff,
strlen(Buff),
LPWSTR(bstr),
lstrlen(Buff));
// Send the error message back to external program
if (MyExGetNameProc != NULL)
MyExGetNameProc(bstr);
// Delete the used memory
SysFreeString(bstr);
delete [] Buff;
// Reset the callback function pointer
MyExGetNameProc = NULL;
}
}
extern "C" __declspec(dllexport) BSTR MyOLEString(void)
{
// ** This function is mainly desing for handling passing string
// back to MS Visual Basic program. As MS Visual basic internally
// handling the string differently from MS Visual C++
// Purpose of this function...
// This function is will return the following string to MS Visual Basic
// #This is a function return a C String to Visual Basic through the BSTR (Basic STRing)
// as define in OLE Automation Object.
char *tmp = new char[128];
memset(tmp, '\0', 128);
sprintf(tmp, "This is a function return a C String to Visual Basic as BSTR (Basic STRing).");
return SysAllocStringByteLen(tmp, strlen(tmp));
}
extern "C" __declspec(dllexport) long GetName(LPSTR lpszBuffer, long MaxBuffer)
{
if (MaxBuffer > 0)
{
// Reset the passing character array
memset(lpszBuffer, '\0', MaxBuffer);
// Copy the string into this character array
sprintf(lpszBuffer, "This is the return C String to Visual Basic through the pass in string buffer.");
// Set the return character copy
return strlen(lpszBuffer);
}
// Set return value
return 0;
}
Chris, your second method using the MyOleString Function works
great...it was just what I was looking for. The other two give
'bad DLL calling convention' errors in VB when I run them.
My question is, (for Chris or CornedBee) I defined a class.....we'll
call it test. The following code is part of my class definition:
Code:
Class Test
{
public:
string ReturnString(void) const;
// ...
private:
string StringToReturn;
};
string Test::ReturnString(void) const
{
return StringToReturn;
}
string is a part of the C++ runtime library and classes and therefore unknown to sprinf that only supports c strings (char*). The c_str() method of string returns a const char* that can be used in such functions. Note that it is const, so you can't modify it.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.