-2

How do I concatenate LPCTSTRs for the MessageBox? The syntax in Java is like this

LPCTSTR str1 = "String2";
LPCTSTR str2 = "";
LPCTSTR str3 = "String1";
LPCTSTR finalMsg = "";

finalMsg = str1 + str2 + str3;

What is the syntax in C++/Win32? Thanks!

Swordfish
  • 12,971
  • 3
  • 21
  • 43
mengmeng
  • 1,266
  • 2
  • 22
  • 46
  • The "syntax" is to use string classes, not pointers. Also, your code would fail to compile if your application is Unicode. An `LPCTSTR` is a pointer to either a narrow or wide string, depending on the build type. – PaulMcKenzie Oct 12 '18 at 03:29
  • You can't modify the memory, `finalMsg` points to because `LPCTSTR` is a pointer to `const TCHAR`. – Swordfish Oct 12 '18 at 03:42

3 Answers3

2

Don't use TCHAR which is intended for code that can target Windows versions that do not support Unicode, i.e. Windows 98.

Make use of the standard library string classes to handle memory management and concatenation.

std::wstring str1 = L"String2";
std::wstring str2 = L"";
std::wstring str3 = L"String1"
std::wstring finalMsg = str1 + str2 + str3;
MessageBoxW(..., finalMsg.c_str(), ...);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
-1

Use strcat() to copy old C-style strings.

Or more precisely _tcscat() on most platforms to work with whatever character type you are using.

Be sure to preallocate your string buffer so that it's big enough to hold whatever the result is.

TCHAR buff[128];

_tcscpy(buff, str1);
_tcscat(buff, str2);
_tcscat(buff, str3);
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
-1
#include <windows.h>
#include <tchar.h>

int main()
{
    LPCTSTR str1 = _T("String2");
    LPCTSTR str2 = _T("");
    LPCTSTR str3 = _T("String1");

    LPTSTR finalMsg = new TCHAR[_tcslen(str1) + _tcslen(str2) + _tcslen(str3) + 1];
    _tcscpy(finalMsg, str1);
    _tcscat(finalMsg, str2);
    _tcscat(finalMsg, str3);

    MessageBox(nullptr, finalMsg, _T("Message"), MB_OK);

    delete[] finalMsg;
}

Plan-B: Use the std string classes:

#include <windows.h>
#include <tchar.h>

#include <string>

using tstring = std::basic_string<TCHAR>;

int main()
{
    tstring str1 = _T("String2");
    tstring str2 = _T("");
    tstring str3 = _T("String1");

    tstring finalMsg = str1 + str2 + str3;

    MessageBox(nullptr, finalMsg.c_str(), _T("Message"), MB_OK);
}

If you don't need to support ANSI-only versions of Windows, you can drop TCHAR and ...TSTR altogether and use std::wstring, which is a template specialization of std::basic_string for wchar_t.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • Hello, this works fine! Thank you! It's still my 3rd day with C++. Thanks for your help! – mengmeng Oct 12 '18 at 03:52
  • 3
    "It's still my 3rd day with C++". Then it would be probably better to work through a good(tm) textbook about C++ instead of playing with the WinAPI. – Swordfish Oct 12 '18 at 03:53
  • 2
    Not a great answer. Far better to use `wstring`, the `+` operator and then `c_str` when calling `MessageBox`. It's disappointing that this answer was accepted which makes no attempt to dispel the myth of `TCHAR` and continues using C style strings. – David Heffernan Oct 12 '18 at 04:04
  • @DavidHeffernan Agreed, although `std::wstring` is not necessarily correct either, since the whole thing with TCHAR is that it doesn't have to be a `wchar_t`. Of course, in practice we all use `std::wstring` knowing that our projects only target one character type, and we bury our guilty conscience in order to get on with things. ;) It would be more correct to use this approach and then invoke `MessageBoxW` explicitly. – paddy Oct 12 '18 at 04:10
  • @paddy No guilt at all in targeting Unicode. The guilt would be still using TCHAR as if it was still the 20th century. So yes `wstring` is correct here. And yes, invoke the W version explicitly. That's how I edited Jonathan's CW answer. – David Heffernan Oct 12 '18 at 04:12
  • The edit is still problematic, continuing with `TCHAR` but calling `MessageBoxW` – David Heffernan Oct 12 '18 at 04:28
  • @DavidHeffernan Was not done intentionally. – Swordfish Oct 12 '18 at 04:30
  • @RemyLebeau Might I ask why you removed the other link? – Swordfish Oct 12 '18 at 04:57
  • @Swordfish A history lesson is all it takes to understand why UTF-16 is used by Windows. Windows adopted UCS2 (as it was known) before UTF-8 had been invented. – David Heffernan Oct 12 '18 at 05:08
  • @DavidHeffernan Um. I know? – Swordfish Oct 12 '18 at 05:12
  • So why the snarky comment about MS using UTF-16. This hasn't been a terribly edifying process has it. – David Heffernan Oct 12 '18 at 05:23
  • @DavidHeffernan better? – Swordfish Oct 12 '18 at 05:25
  • Not really. Did you know that Java uses UTF16 natively. I think Windows isn't really that special and the comment is just a needless and unhelpful distraction. – David Heffernan Oct 12 '18 at 05:33
  • @DavidHeffernan Know what? Edit the post to your precious liking. I'm done with the topic. – Swordfish Oct 12 '18 at 05:34
  • @Swordfish "*Might I ask why you removed the other link?*" - I didn't remove any link. The link didn't exist yet at the time I started making my edit. You were making edits around the same time I was, it likely just got lost during parallel edits. – Remy Lebeau Oct 12 '18 at 06:48
  • "*Did you know that Java uses UTF16 natively*" - in fact, many programming languages have standardized on UTF-16 for their native string handling. UTF-8 is typically better for use in things like text storage, communication exchanges, etc but it is not very good for in-memory string processing. UTF-16 tends to be better for that instead. It tends to be a better balance between speed and overhead than other UTFs. – Remy Lebeau Oct 12 '18 at 06:53
  • @DavidHeffernan: I don't know if you are the one downvoting everyone. You might feel that `TCHAR`s are ancient history. But it's just answering the question that was asked. For all I know, the OP is modifying older code and has to work with `TCHAR`s. Who downvotes for that? – Jonathan Wood Oct 12 '18 at 14:48
  • @JonathanWood Well, your answer is a buffer overrun waiting to happen isn't it. – David Heffernan Oct 12 '18 at 14:54
  • @DavidHeffernan: Which I also covered in my answer, didn't I? I used to program old-style C, likely before you started programming. And it would be naive of you to assume I was getting buffer runs all over the place. Like I said, just answering the question asked. – Jonathan Wood Oct 12 '18 at 15:01