LPCTSTR to const char*

Flaviu_ 1,031 Reputation points
2021-06-20T07:37:49.51+00:00

In a MFC project, I have encoutnered an issue using CDatabase::ExecuteSQL method, whend I have tried to deliver a std::string as a parameter:

void ExecuteSQL(LPCTSTR lpszSQL);

std::string sql;
ExecuteSQL(sql.c_str());

in Use Unicode Character Set configuration only.

The error is:

'void CDatabase::ExecuteSQL(LPCTSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCTSTR'

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

So, which one of conversion is better ? reinterpret_cast or C-style cast ? Or doesn't matter ?

Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,636 Reputation points
    2021-06-20T08:18:06.47+00:00

    A cast won't solve the problem. The std::string is encapsulating a narrow string. So the member function c_str() is returning a const char * pointer. You need to convert the result to UNICODE or otherwise change the code to use std::wstring when building for UNICODE.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.