0

ALL,

All questions here are talking about LPCTSTR conversion.

But since the example at https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/version-property-example-vc?view=sql-server-ver15 uses LPCSTR - I need to perform that conversion.

I have the code at the link working, but I need the conversion since the software I am writing is operating with std::wstring (it is cross-platform and cross-DBMS).

TIA!!

Igor
  • 5,620
  • 11
  • 51
  • 103
  • 1
    Plenty of solutions here: https://stackoverflow.com/questions/6691555/converting-narrow-string-to-wide-string – Retired Ninja Dec 12 '20 at 18:35
  • 2
    How to correctly do the conversion depends on the content of your LPCSTR, is it UTF-8, is it some Windows code page, is it ASCII? etc. etc. More detail is needed. – john Dec 12 '20 at 18:41
  • @john, I think it is some Windows code page - it is a string returning from the ADO `query`. – Igor Dec 12 '20 at 19:03
  • LPCSTR is Microsoft pointer to a C-Style string (a.k.a. array of ASCII characters terminated by nul). The `wstring` type is for wide characters, i.e. encodings that don't fit within the ASCII set; usually represented by a 16-bit quantity. So you are asking to convert a pointer to an 8-bit C-Style string to a 16-bit encoding. – Thomas Matthews Dec 12 '20 at 19:05
  • @RetiredNinja, I don't think LPCSTR is the same as std::string. Do you? – Igor Dec 12 '20 at 19:05
  • @ThomasMatthews, are you saying it is impossible to convert? – Igor Dec 12 '20 at 19:07
  • I did not say it is impossible. I'm letting you know what needs to happen. Search the internet for "lpcstr conversion wstring". – Thomas Matthews Dec 12 '20 at 19:09
  • @Igor Yes, they are both narrow strings. LPCSTR and LPCTSTR are also the same when UNICODE is not defined. – Retired Ninja Dec 12 '20 at 19:12
  • @Igor Then you need MultiByteToWideChar as the code linked in the first comment uses. Ideally you would know which code page. – john Dec 12 '20 at 19:17
  • @RetiredNinja, UNICODE is defined in my case. (just curious - who does not define it in 21 century). – Igor Dec 12 '20 at 19:20
  • 1
    @Igor a lot of people, actually. Especially when maintaining legacy code that predates UNICODE, or just when writing code that doesn't need to deal with non-ASCII data. – Remy Lebeau Dec 12 '20 at 19:23
  • @Igor Who uses `TCHAR` in the 21st century? *shrug* – Retired Ninja Dec 12 '20 at 20:16
  • @RetiredNinja, apparently MS ADO does. ;-) And I have another question - I looked at the docs: https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar, and it looks like I should use CP_OEMCP as the the OS default code page. Am I right? – Igor Dec 12 '20 at 20:34

0 Answers0