CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Reading string out of embedded resources

    Quote Originally Posted by qwerz View Post
    This is my test with MessageBoxW:
    And it is a bogus test.
    Code:
        char *messs = (char*)LockResource(hGlobal3);
        MessageBoxW(NULL, (LPCWSTR)messs, (LPCWSTR)"bonjour", MB_OK);
    Look at what you have here.

    1) You declared messs to be a char*, when obviously the string you're trying to get from the resource is a wide string. That's why I stated you use LPCTSTR, not char*

    2) You cannot cast non-wide strings to wide strings or vice-versa.. Look at the third parameter to MessageBox. Casting from one string representation to another doesn't work. Strings must be converted from one type to another using the WideCharToMultiByte or MultiByteToWideChar functions. A C-style cast does none of that. You make the same attempt on the second parameter (going back to item 1 on the list). So your entire call to MessageBox() is bad.

    Here is what you're supposed to do:

    1) Make your project Unicode.

    2) Make all of your character variables follow the Windows types for Unicode applications (LPCTSTR, TCHAR, _T(), TEXT) -- there should be no char*, LPCSTR, etc.

    3) String literals must already be Unicode, and not ANSI strings being falsely casted to wide-strings:
    Code:
    LPCTSTR messs = (LPCTSTR) LockResource(hGlobal3);
    //...
    MessageBox(NULL, messs,  _T("bonjour"),  MBOK);
    This is how your code is supposed to look.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 11th, 2012 at 11:56 AM.

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