Solved

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

Posted on 2000-05-14
13
1,225 Views
Last Modified: 2008-03-06
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
Comment
Question by:alex_registry
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 2808826
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
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2808961
The article:
   MSJ August, 1995 - Interface Enumeration/GetActiveObject vs. MFC

Can be found at:
   http://www.microsoft.com/MSJ/0895/activex0895.htm
0
 

Author Comment

by:alex_registry
ID: 2808965
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
 
LVL 23

Expert Comment

by:chensu
ID: 2809009
The actual function to do the conversion is the Win32 API function WideCharToMultiByte.
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2815306
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
 

Author Comment

by:alex_registry
ID: 2829064
Does any one has a class solution?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:alex_registry
ID: 2829072
Adjusted points from 200 to 245
0
 

Author Comment

by:alex_registry
ID: 2829073
char* <-> BSTR
wchar_t* <-> BSTR
char* <-> wchar_t*

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

alex_registry
0
 
LVL 23

Expert Comment

by:chensu
ID: 2829218
The _bstr_t class is exactly what you want. The source code is in the comutil.h file.
0
 

Author Comment

by:alex_registry
ID: 2926161
Adjusted points from 245 to 440
0
 

Author Comment

by:alex_registry
ID: 2926162
I wanna a class. Class please.
0
 

Accepted Solution

by:
aljehin earned 440 total points
ID: 2926169
#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
 

Author Comment

by:alex_registry
ID: 2926175
Answer accepted
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now