|
-
August 16th, 2006, 11:57 AM
#1
std::string to BSTR conversion
Hi everybody,
I have a problem, i'm developing a dll in VC++ 6.0, this dll is for a Vb 6.0 app. that looks somenthing like this:
BSTR __stdcall login (BSTR user,BSTR password){
...
...
std::string validuser = functhatreturnstdstring(user,password);
return ((BSTR)validuser);
}
How can I convert/cast the std::string to BSTR??? or how can I manage the returned std::string in Vb?? I try returnin a char*, const char*, but, when i run the Vb app. it fails, and says: the instruction at memory 0x98797 referencing to memory 0x0980808 could not be read, whats wrong??? I also try returning others data types int, short, float and works fine, also returning this strange datatype BSTR that is like a char * works fine, whats wrong with char *??
in my Vb code I have something like this:
Private Declare Function login Lib "C:\juan\telcel\KlientC++\KlientCpp\Debug\KlientCpp.dll" (ByVal x As String, ByVal y As String) As String
Private Sub Command1_Click()
Text3.Text = Str(login(Text1.Text, Text2.Text))
End Sub
please Help me!!
What I need to do in Vb or Vc++ to this thing work???
-
August 16th, 2006, 03:26 PM
#2
Re: std::string to BSTR conversion
VB is expecting a BSTR data type to be returned from your dll.
If you do not have to convert the incoming BSTR to std::string then don't and just return the BSTR value.
If you have to convert to a std::string then you need to convert it back into a BSTR before you send it back to VB.
HTH
-
August 16th, 2006, 04:13 PM
#3
Re: std::string to BSTR conversion
try this:
Code:
BSTR bstrtmp; CString strTmp("hello");
bstrtmp = m_strTmp.AllocSysString();
There is one absolute truth that there is no such thing as absolute truth.
-
August 16th, 2006, 09:47 PM
#4
Re: std::string to BSTR conversion
-
August 17th, 2006, 12:56 AM
#5
Re: std::string to BSTR conversion
Code:
BSTR bstrRet;
LPWSTR szUnicode;
if (AscToUnicode (strRet.c_str (),&szUnicode))
{
bstrRet=::SysAllocString (szUnicode);
bool AscToUnicode (const char* szAsc,LPWSTR* ppUnicode)
{
int nWidthCharLen;
nWidthCharLen=::MultiByteToWideChar(CP_ACP,0,szAsc,::lstrlen (szAsc),NULL,0);
*ppUnicode=new wchar_t[nWidthCharLen];
::ZeroMemory (*ppUnicode,nWidthCharLen * 2);
//dwLen=(DWORD)(nWidthCharLen * sizeof(WCHAR));
//lpBuffer=new BYTE[dwLen];
if (::MultiByteToWideChar(CP_ACP,0,szAsc,lstrlen(szAsc),*ppUnicode,nWidthCharLen)==0)
{
delete *ppUnicode;
*ppUnicode=NULL;
return false;
}
else
return true;
}
there is other way to transfer a string from VC Dll to VB .
Code:
VC SIDE
struct MyStru
{
char szMy[1024 * 8];
};
void MyFunc(MyStru* pStru)
{
lstrcpy (pStru->szMy,"fkjsfkjdfj");
}
VB SIDE
type MyStru
{
Dim szMy as String* (1024* 8).
}
public declare Function MyFunc(byRef MyStru) ;
// I am not quite sure whether the vb code is correct or not .
but I recommend you to use ActiveX -control instead of DLL,it works perfectly.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|