Migration to C++ 7.0

M

Markus Strauss

First i need to tell you i am no C-Programer - i can only make some litle C
changes to the code to meet our needs.

Now i have the folowing problem. Our programm reads a SQL-String from the
resources and gives this to the oledbCommand. This is the macro which worked
fine with C++6.0:

#define DEF_RESCOMMAND(x, nResource) typedef x _CommandClass; \

static HRESULT GetDefaultCommand(LPCWSTR* ppszCommand) \

{ static char sCommand[1024]; \

static bool bInit=false; \

if (!bInit) {::LoadString(_Module.m_hInst,nResource,sCommand,1024);
bInit=true;} \

*ppszCommand = sCommand; \

return S_OK; \

}

When i compile this with C++7.0 i get the following error - pleace be
patient but i cant tell you the english errormessage only the german:

c:\Entwicklung V2.0\visKernel\qryTradeGroups.H(25): error C2440: '=' : 'char
[1024]' kann nicht in 'LPCWSTR' konvertiert werden

but means that CHAR could not be converted to LPCWSTR.

Now I changed this to the following form:

#define DEF_RESCOMMAND(x, nResource) typedef x _CommandClass; \
static HRESULT GetDefaultCommand(LPCWSTR* ppszCommand) \

{ static char sCommand[1024]; \

static bool bInit=false; \

if (!bInit) {::LoadString(_Module.m_hInst,nResource,sCommand,1024);
bInit=true;} \

*ppszCommand = CA2W(sCommand); \

return S_OK; \

}

I used a conversionfunction. Now it compiles ok, but at runtime the lifetime
of the converted string is to short, when the code leafes the code of the
macro, the string only contains 999999.... inside its okay. please tell me
what function to convert i need to use instead of CA2W or what else i should
change in the macro.


thx for your effort

Markus
 
B

Brandon Bray [MSFT]

Markus said:
When i compile this with C++7.0 i get the following error - pleace be
patient but i cant tell you the english errormessage only the german:

c:\Entwicklung V2.0\visKernel\qryTradeGroups.H(25): error C2440: '=' :
'char [1024]' kann nicht in 'LPCWSTR' konvertiert werden

but means that CHAR could not be converted to LPCWSTR.

Hi Markus,
The problem is that the original array contains ANSI characters, and the
pointer is asking for wide characters. LPCWSTR stands for long pointer to
constant wide zero-terminated string. The assignment is trying to assign a
long pointer to a constant ansi zero-terminated string.

The fix is not that hard. Change this line

{ static char sCommand[1024]; \

to

{ static wchar_t sCommand[1024]; \

This way you are working with a wide character buffer. Some of the other
function calls (such as LoadString) will likely need to be changed to using
the wide string API (such as LoadStringW). This usually happens behind the
scenes depending on whether the _UNICODE macro is defined. For more
information, check out the documentation for tchar.h.

I hope that helps get you going in the right direction. Cheerio!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top