The char WindowText[4096] stores the title of the window (not the window with the message) obtained with GetWindowText . When I try to do this:

 MessageBoxA(0, "Window Text = '"+ WindowText +"'! Some Text", "Some Title", MB_ICONERROR | MB_OK); 

That get: Output error AND:

Error output 2

What to do?

  • And you did not try to write Window Text one word and without a space? So, as indicated in the char WindowText[4096] declaration char WindowText[4096] ... - Vladimir
  • I made a mistake here at copy-paste, I will correct it now, everything is correct in the code, but it does not work - Super_Puper_User
  • Then the second possible (in the sense, this is exactly the problem, but not the fact that the last ;-)) problem - are you sure that you can add strings (constant and character array) in this way? Did you overload the addition operation for this? Usually you can't do that. When asked what to do, form the line BEFORE this call, or some snprintf () or something else (yes, at least through the line STL C ++). - Vladimir
  • I'm not sure about anything. I just need to insert WindowText between window titles. When I do it in the usual way for me, I get errors. - Super_Puper_User
  • С++ != С# && != Java :) - NewView

3 answers 3

For example, something like this:

 #include <string> #include <iostream> #include <windows.h> int main() { std::string sl; char WindowText[4096] = "***"; sl = "Window Text = '"; sl += WindowText; sl += "'! Some Text"; MessageBoxA(0, sl.c_str(), "Some Title" ,MB_ICONERROR | MB_OK); return 0; } 

UPD: At the request of a colleague I will add a little answer. Since the C ++ tag was indicated in the question, I give an example with the simplest constructs from this language with the maximum preservation of the entities from the question (of course, it would be better to replace all strings with the type std::string ). And of course, this is not the only possible option.

In C ++, for user-defined data types, and in particular, for classes and structures, it is possible to override (overload) the behavior of standard operations. In the case of the string class from the standard template library, the + (and += ) operator is redefined to concatenate such strings with objects of the same type and with C-style strings. In order for the compiler to use this overloaded operator it is necessary that at least one of the two operands belong to this type of string .

If you remain within the framework of the standard C library, you could use the functions I already mentioned in the comments to form the sprintf()/snprintf() string, the functions mentioned by Harry for concatenating strcat()/strncat() and even functions for copying / move strcpy()/strncpy()/memcpy()/memmove() . Preference should be given to those variants of functions where the letter has the letter n (this does not apply to memcpy()/memmove() , because the size must also be explicitly indicated in them) - these functions, as it were (in the sense of providing an opportunity, and the rest depends on the programmer), protected from overflow errors, since the size of the target memory block must be explicitly stated.

    In C / C ++, concatenating strings with the + sign makes sense if they are strings. If these are C-terms (like your literals like "Window Text = '" or the WindowText variable declared as an array char ), then they are interpreted as pointers to the first characters, and summing the pointers is nonsense, which the compiler told you.

    For this you need to use functions like strcpy , strcat and others like them. For example,

     char buf[5120] = "Window Text = '"; strcat(buf, WindowText); strcat(buf,"'! Some Text"); MessageBoxA(0, buf, .... 

      I did this:

       sprintf(Message, "%s%s%s", "Window Text = '", WindowText +"'! Some Text"); MessageBoxA(0, Message, "Some Title", MB_ICONERROR | MB_OK); 

      But you can do the same as in other answers.

      Thank you all very much!