Jump to content

C++ WinAPI: dynamic message box text?

Go to solution Solved by fizzlesticks,

Use a wstring then pass the message box the c_str. 

    wstring str = L"Message "s + to_wstring(GetLastError());
    wstring cap = L"Title"s;

    auto ret = MessageBox(nullptr, str.c_str(), cap.c_str(), MB_OKCANCEL);

 

        MessageBox(NULL,
                   L"Failed to create application window\n" + (char)GetLastError(),
                   L"ERROR",
                   NULL);

This outputs "lication window". I have tried other methods of dynamically deciding the message box's text such as using a string (which is didn't like) and building the text outside the MessageBox() function. Not sure what to try now.

 

 

Link to comment
https://linustechtips.com/topic/723529-c-winapi-dynamic-message-box-text/
Share on other sites

Link to post
Share on other sites

Use a wstring then pass the message box the c_str. 

    wstring str = L"Message "s + to_wstring(GetLastError());
    wstring cap = L"Title"s;

    auto ret = MessageBox(nullptr, str.c_str(), cap.c_str(), MB_OKCANCEL);

 

1474412270.2748842

Link to post
Share on other sites

21 minutes ago, fizzlesticks said:

Use a wstring then pass the message box the c_str. 


    wstring str = L"Message " + to_wstring(GetLastError());
    wstring cap = L"Title";

    auto ret = MessageBox(nullptr, str.c_str(), cap.c_str(), MB_OKCANCEL);

 

to_wstring (and consequently to_string) don't seem to be in the MinGW installation on my machine. :/ I'll need to try and sort that.

Thanks for the help, though. :) 

Link to post
Share on other sites

2 minutes ago, pipnina said:

to_wstring (and consequently to_string) don't seem to be in the MinGW installation on my machine. :/ I'll need to try and sort that.

Thanks for the help, though. :) 

They were added in C++11, you'll need to add the --std=c++11 option when compiling or upgrade if it doesn't support it.

1474412270.2748842

Link to post
Share on other sites

Just now, fizzlesticks said:

They were added in C++11, you'll need to add the --std=c++11 option when compiling or upgrade if it doesn't support it.

I've installed the most recent 64-bit version of MinGW and linked it in Code::Blocks. I compiled the program with 

wstring msg = L"Failed to create application window\n" + to_wstring());

And now it works as intended. Clearly the 32-bit version of MinGW didn't fully support C++11 features.

 

Thank you :) 

Link to post
Share on other sites

I'm looking at MSDN

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx

I'm thinking you might need to put brackets around the string concatenation to cast it to LPCWSTR

             ☼

ψ ︿_____︿_ψ_   

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×