Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::string to LPCOLESTR

627 views
Skip to first unread message

diltsman

unread,
Nov 21, 2006, 5:19:02 PM11/21/06
to
I have a function that takes a std::string and need to convert it to a
LPCOLESTR. How would I even go about that? I tried:
TCHAR * a2 = NULL;
int bufferSize = MultiByteToWideChar(CP_ACP, NULL, ProgramID.c_str(), -1,
(LPWSTR)a2, 0);
a2 = new TCHAR[bufferSize];

int conversion = MultiByteToWideChar(CP_ACP, NULL, ProgramID.c_str(), -1,
(LPWSTR)a2, bufferSize);

However, I don't know that TCHAR * is able to be converted to LPWSTR by a
cast, and I also don't know if it can then be converted to LPCOLESTR by
another cast. What would be a good way of doing this?

Igor Tandetnik

unread,
Nov 21, 2006, 5:37:31 PM11/21/06
to
diltsman <dilt...@discussions.microsoft.com> wrote:
> I have a function that takes a std::string and need to convert it to a
> LPCOLESTR. How would I even go about that? I tried:
> TCHAR * a2 = NULL;
> int bufferSize = MultiByteToWideChar(CP_ACP, NULL, ProgramID.c_str(),
> -1, (LPWSTR)a2, 0);

You obviously know that MultiByteToWideChar expects a pointer to WCHAR -
and yet you declare your buffer as TCHAR. Why?

> However, I don't know that TCHAR * is able to be converted to LPWSTR
> by a cast

So why cast, and not declare a2 to be LPWSTR in the first place?

> and I also don't know if it can then be converted to
> LPCOLESTR by another cast.

LPCOLESTR and LPCWSTR are one and the same (for all practical intents
and purposes). They are typedefs for "const wchar_t*". LPWSTR (without
C) is a typedef for "wchar_t*" (without const).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


0 new messages