Solved

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

Posted on 2000-05-14
13
1,226 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

24 Experts available now in Live!

Get 1:1 Help Now