CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2006
    Posts
    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???

  2. #2
    Join Date
    Aug 2004
    Posts
    184

    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

  3. #3
    Join Date
    Aug 2003
    Posts
    79

    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.

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

  5. #5
    Join Date
    Oct 2005
    Posts
    526

    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
  •  





Click Here to Expand Forum to Full Width

Featured