CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2006
    Location
    England
    Posts
    72

    MessageBox() variables as parameters

    I am having some embarrassing problems that I can't get my head around. I expect many people here will be able to help with this one.

    I want a message box to output a variables content, but I am running into difficulties. My code basically goes something like this:

    Code:
        int iErrorCode = GetLastError();
        char szErrorText[256];
        ZeroMemory(szErrorText, sizeof(szErrorText));
        sprintf(szErrorText, "Error Code Returned: %d", iErrorCode);
    
        int iMessageReturn = MessageBox(NULL,
                    szErrorText,
                    L"Title of the box!",
                    MB_ICONERROR | MB_RETRYCANCEL);
    I think this is something to do with the unicode setting but I don't know how to rectify. Can someone please help by telling me how I can convert the char variable into a LPCWSTR that the MessageBox() function won't moan about?

    Thanks,
    Tom

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: MessageBox() variables as parameters

    Code:
        int iErrorCode = GetLastError();
        TCHAR szErrorText[256];
        ZeroMemory(szErrorText, sizeof(szErrorText));
        stprintf(szErrorText, TEXT("Error Code Returned: %d"), iErrorCode);
    
        int iMessageReturn = MessageBox(NULL,
                    szErrorText,
                    TEXT("Title of the box!"),
                    MB_ICONERROR | MB_RETRYCANCEL);
    Best regards,
    Igor

  3. #3
    Join Date
    May 2006
    Location
    England
    Posts
    72

    Re: MessageBox() variables as parameters

    Quote Originally Posted by Igor Vartanov View Post
    Code:
        int iErrorCode = GetLastError();
        TCHAR szErrorText[256];
        ZeroMemory(szErrorText, sizeof(szErrorText));
        stprintf(szErrorText, TEXT("Error Code Returned: %d"), iErrorCode);
    
        int iMessageReturn = MessageBox(NULL,
                    szErrorText,
                    TEXT("Title of the box!"),
                    MB_ICONERROR | MB_RETRYCANCEL);
    Awesome, thanks for the response.
    What header do I need to include for stprintf() support?
    Thanks again

    EDIT: Appears to be working with _stprintf(). Presumably just a typo in the previous post!?
    Last edited by AnotherMuggle; October 11th, 2009 at 02:23 PM.

  4. #4
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: MessageBox() variables as parameters

    _stprintf() and stprintf() are the same.
    Regards,
    Ramkrishna Pawar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured