• 0

_TCHAR OLECHAR conversion


Question

I have the following code

int _tmain(int argc, _TCHAR* argv[])
{
BSTR pgm = SysAllocString(argv[1]);

I get the following error when I try to compile it:

error C2664: 'SysAllocString' : cannot convert parameter 1 from '_TCHAR *' to 'const OLECHAR *'

Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I have been struggling with this for the past one hour.

How do I fix it?

Please help.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

In this case,

_TCHAR* = char*

&

OLECHAR* = unsigned short*

Therefore just casting will give incorrect results.

I was under the impression that _TCHAR and OLECHAR are synonyms. This was not the case.

Here is how I fixed the code:

WCHAR buffer[512];

ZeroMemory(buffer, sizeof(buffer));

char* param = argv[1];

MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, param, strlen(param), buffer, sizeof(buffer)-1);

BSTR pgm = SysAllocString(buffer);

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.