Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming Add-Ins / January 2007

Tip: Looking for answers? Try searching our database.

how to convert  'System::String ^' to 'BSTR'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lencastro - 20 Jan 2007 12:43 GMT
i am creating a addin for outlook express in vc++2005

I am trying to load xml to my code with the following code

STDMETHOD(GetCustomUI)(BSTR RibbonID, BSTR * RibbonXml)
    {
        Assembly^ assem=Assembly::GetExecutingAssembly();
        Stream^ strm=
assem->GetManifestResourceStream("test.Xmlres.test.xml");
        StreamReader^ reader= gcnew StreamReader(strm);
        *RibbonXml=  reader->ReadToEnd();
        reader->Close();
        strm->Close();
        return S_OK;

    }

but it showing an error in compile time:

error C2440: '=' : cannot convert from 'System::String ^' to 'BSTR'
       No user-defined-conversion operator available, or
       Cannot convert a managed type to an unmanaged type
IceKettle - 26 Jan 2007 01:03 GMT
I'm not very familiar with managed C++. I think you can first convert the
System::String to an  wide char,an then convert this wide char to BSTR.
From MSDN you can get the first step as follows:

// convert_string_to_wchar.cpp
// compile with: /clr
#include < stdio.h >
#include < stdlib.h >
#include < vcclr.h >

using namespace System;

int main() {
  String ^str = "Hello";

  // Pin memory so GC can't move it while native function is called
  pin_ptr<const wchar_t> wch = PtrToStringChars(str);
  printf_s("%S\n", wch);

  // Conversion to char* :
  // Can just convert wchar_t* to char* using one of the
  // conversion functions such as:
  // WideCharToMultiByte()
  // wcstombs_s()
  // ... etc
  size_t convertedChars = 0;
  size_t  sizeInBytes = ((str->Length + 1) * 2);
  errno_t err = 0;
  char    *ch = (char *)malloc(sizeInBytes);

  err = wcstombs_s(&convertedChars,
                   ch, sizeInBytes,
                   wch, sizeInBytes);
  if (err != 0)
     printf_s("wcstombs_s  failed!\n");

   printf_s("%s\n", ch);
}And in second step using ::SysAllocString to conver the wchar to BSTR or
using _bstr_t.

"lencastro" <lencastro@gmail.com>
??????:1169297004.487965.162140@11g2000cwr.googlegroups.com...
>i am creating a addin for outlook express in vc++2005
>
[quoted text clipped - 18 lines]
>        No user-defined-conversion operator available, or
>        Cannot convert a managed type to an unmanaged type
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.