Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1258
  • Last Modified:

BSTR <-> char* conversions using C++ class

Hi all,

I tried to find the article where Don Box discusses many variations of
BSTR <-> char* conversion. I desperately need it.

Can any one send me that article from MSJ August 1995 Interfaces and the Registry/GetActiveObject Vs. MFC?

Anybody has code (C++ class) on this topic?

Thanks a lot.  

 
0
alex_registry
Asked:
alex_registry
  • 7
  • 3
  • 2
  • +1
1 Solution
 
chensuCommented:
You may use the W2A macro.

String Conversion Macros
http://msdn.microsoft.com/library/default.asp?URL=/library/devprods/vs6/visualc/vcmfc/_atl_string_conversion_macros.htm

TN059: Using MFC MBCS/Unicode Conversion Macros
http://msdn.microsoft.com/library/default.asp?URL=/library/devprods/vs6/visualc/vcmfc/_mfcnotes_tn059.htm

You may also use the _bstr_t class that has the _bstr_t::wchar_t* and _bstr_t::char* operators if you are using Visual C++ 5.0 or later.

The MSJ August 1995 source code can be downloaded at
http://www.microsoft.com/msj/code1993to1997/MSJAUG95.EXE
0
 
mnewton022700Commented:
The article:
   MSJ August, 1995 - Interface Enumeration/GetActiveObject vs. MFC

Can be found at:
   http://www.microsoft.com/MSJ/0895/activex0895.htm
0
 
alex_registryAuthor Commented:
I had visited all links you posted. I would like to see C++ functions that deal with these conversions.

Anyway, I really appreciate your effort. Thanks a lot.

Alex
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chensuCommented:
The actual function to do the conversion is the Win32 API function WideCharToMultiByte.
0
 
mnewton022700Commented:
Alex,

I guess that you must be debating who to award the points to.

You should award them to the person who best answered your question. If that person was Chensu, then award them to him. If it was me, then reject Chensu's answer and accept my comment.

If you aren't sure, then just give them to Chensu since he answered first.
0
 
alex_registryAuthor Commented:
Does any one has a class solution?
0
 
alex_registryAuthor Commented:
Adjusted points from 200 to 245
0
 
alex_registryAuthor Commented:
char* <-> BSTR
wchar_t* <-> BSTR
char* <-> wchar_t*

So, there should be 6 conversions covered be this class(es).

alex_registry
0
 
chensuCommented:
The _bstr_t class is exactly what you want. The source code is in the comutil.h file.
0
 
alex_registryAuthor Commented:
Adjusted points from 245 to 440
0
 
alex_registryAuthor Commented:
I wanna a class. Class please.
0
 
aljehinCommented:
#include <comdef.h>

#ifndef _CONV_H
#define _CONV_H



class toBSTR
{

public:

   // Foreign constructors
   toBSTR (const char    *p8);
   toBSTR (const wchar_t *p16);

   // Native Constructor
   toBSTR (const BSTR     bstr);

   // Non-virtual destructor (this class is concrete)
   ~toBSTR (void);

   // Native conversion operator
   operator const BSTR (void) const;

private:

   // Native BSTR string
   BSTR   m_bstr;

   // Is foreign?
   BOOL   m_bIsForeign;

   // Protect against assignment: just declare the prototypes of the copy
   // constructor and assignment operator, and DO NOT implement them
   toBSTR (const toBSTR&);
   toBSTR& operator= (const toBSTR&);

};


// Foreign constructors require allocation of a native
// string and conversion
inline toBSTR::toBSTR (const char *p8)
: m_bstr (SysAllocStringLen (0, strlen (p8))), m_bIsForeign (TRUE)
  // SysAllocStringLen appends a NULL characther
{
   // If the 1st arg. is NULL, mbstowcs() returns the required size
   // of the destination string.
   size_t size = mbstowcs (0, p8, strlen (p8)) + 1;
   mbstowcs (m_bstr, p8, size);
}

inline toBSTR::toBSTR (const wchar_t *p16)
: m_bstr (SysAllocString(p16)), m_bIsForeign (TRUE)
{
}


// Native constructor is a pass-through.
inline toBSTR::toBSTR (const BSTR bstr)
: m_bstr (bstr), m_bIsForeign (FALSE)
{
}

// Simply give out the native wideness string.
inline toBSTR::operator const BSTR (void) const
{
   return m_bstr;
}


// Destructor: Delete native string only if synthesized
// in foreign constructor.
inline toBSTR::~toBSTR (void)
{
  if (m_bIsForeign)
  {
    SysFreeString(m_bstr);
  }
}


class toCStringW
{

public:

   // Native constructor
   toCStringW (const wchar_t *p16);

   // Foreign constructors
   toCStringW (const char    *p8);
   toCStringW (const BSTR     bstr);

   // Non-virtual destructor (this class is concrete)
   ~toCStringW (void);

   // Native conversion operator
   operator const wchar_t * (void) const;

private:

   // Native wideness string: wchar_t*
   wchar_t *m_sz;

   // Is foreign?
   BOOL     m_bIsForeign;

   // Protect against assignment
   toCStringW (const toCStringW&);
   toCStringW& operator= (const toCStringW&);
};


// Native constructor is a pass-through
inline toCStringW::toCStringW (const wchar_t *p16)
: m_sz ((wchar_t *)p16), m_bIsForeign (FALSE)
{
}


// Foreign constructors require allocation of a native
// string and conversion
inline toCStringW::toCStringW (const char *p8)
: m_bIsForeign (TRUE)
{
   // Calculate required buffer size (some characters may
   // already occupy 16-bits under DBCS)
   size_t size = mbstowcs (0, p8, strlen(p8)) + 1;

   // Alloc native string and convert
   if (m_sz = new wchar_t[size])
   {
      mbstowcs (m_sz, p8, size);
   }
}


inline toCStringW::toCStringW (const BSTR  bstr)
: m_bIsForeign (TRUE)
{
   // Calculate required buffer size
   size_t size = wcstombs (0, bstr, wcslen (bstr)) + 1;

   // Alloc native string and convert
   if (m_sz = new wchar_t[size])
   {
      m_sz = bstr;
   }
}



// Simply give out the native wideness string
inline toCStringW::operator const wchar_t * (void) const
{
   return m_sz;
}

// Delete w_char* string only if synthesized in foreign constructor
inline toCStringW::~toCStringW (void)
{
  if (m_bIsForeign)
  {
    delete[] m_sz;
  }
}


class toCString
{

public:

   // Native constructor
   toCString (const char *p8);

   // Foreign constructors
   toCString (const wchar_t *p16);
   toCString (const BSTR     bstr);

   // Non-virtual destructor
   ~toCString (void);

   // Native conversion operator
   operator const char * (void) const;

private:

   // Native wideness string: char*
   char *m_sz;

   // Is foreign?
   BOOL  m_bIsForeign;


   // Protect against assignment
   toCString (const toCString&);
   toCString& operator= (const toCString&);
};


// Native constructor is a pass-through
inline toCString::toCString (const char *p8)
: m_sz ((char*)p8), m_bIsForeign (FALSE)
{
}


// Foreign constructor requires allocation of a native
// string and conversion
inline toCString::toCString (const wchar_t *p16)
: m_bIsForeign(TRUE)
{

   // Calculate required buffer size (some characters may
   // require more than one byte under DBCS)
   size_t size = wcstombs (0, p16, wcslen (p16)) + 1;

   // Alloc native string and convert
   if (m_sz = new char[size])
   {
      wcstombs (m_sz, p16, size);
   }
}



// Simply give out the native wideness string
inline toCString::operator const char * (void) const
{
   return m_sz;
}


// Delete native string only if synthesized in foreign constructor
inline toCString::~toCString (void)
{
   if (m_bIsForeign)
   {
      delete[] m_sz;
   }
}


#endif  // _CONV_H
0
 
alex_registryAuthor Commented:
Answer accepted
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now