CString to LPCTSTR conversion
Asked Answered
S

2

15

I have a CString variable that i a need to convert to LPCTSTR(const char*) .I need this conversion so that i can use it as an argument in a function .

The CString look like :

CString sqlTemp = _T("INSERT INTO "+ sw1 +" (filename, "+ sw2 +") VALUE ("+ sw7 +","+ sw3 +" ) ");

It contains an query. The prototype of the function is :

int WriteBlob(LPCTSTR szSqlStat, LPCTSTR szFilePath)

So could you show me an exemple of how to convert to LPCTSTR ? It may be trivial but i am a c++ beginner and i still get a hang of it.

Thanks .

Sadomasochism answered 27/9, 2012 at 11:56 Comment(1)
please try this (LPCTSTR)(LPTSTR)sqlTemp to WriteBlog functionAlpenstock
T
17

One method of conversion is like this:

CString str;

str = "Hello";

LPCSTR szTemp = (LPCSTR)(LPCTSTR)str;
Tweedsmuir answered 27/9, 2012 at 11:58 Comment(4)
Can you also explain as to why there are so many different formats (LPCTSTR, CString, char*, string, etc...) which are all basically the same? Or is there some difference I just cannot see?Dissolution
fell string is an object so you can call methods on it unlike all the others. char* is basically an array of chars. Cstring is also an array of chars but they can take 2 bytes depending on the encoding. LPCTSTR is a pointer to a constant string, so you can't modify it.Tweedsmuir
" Cstring is also an array of chars" no, it's not.Yodel
@TamásSzelei It does point at an LPSTR / LPWSTR though.Turbine
T
0
CString str; // the given string
CStringA strA(str); // a helper string
LPCSTR ptr = strA;

Reference MSDN

Televisor answered 25/8, 2015 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.