Solved

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

Posted on 2000-05-14
13
1,230 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
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.

 
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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

809 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