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

Thread: Reading string out of embedded resources

  1. #1
    Join Date
    May 2012
    Posts
    43

    Reading string out of embedded resources

    Hello,

    I am using the Embedded resources to store a string.
    The client is in C# and is putting the string on a C++ program.

    When the C++ program gets opened, it reads the string.

    But there is a small problem. Once it reads the string, It only picks the first Character.

    Like if I store "Hello" in the resources. The messagebox only shows "H"

    Code:
        HRSRC hRsrc3 = FindResource(0, "5", "RT_RCDATA"); //RT_RCDATA
        HGLOBAL hGlobal3 = LoadResource(0, hRsrc3);
        LPCSTR messs = (LPCSTR)LockResource(hGlobal3);
    
        MessageBox(NULL, messs, "bonjour(s)", MB_OK);
    I tried wchar_t, char* and LPCSTR, all give the same result.

    Thanks in advance.
    Last edited by qwerz; June 10th, 2012 at 07:41 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Reading string out of embedded resources

    Quote Originally Posted by qwerz View Post
    Hello,

    I am using the Embedded resources to store a string.
    The client is in C# and is putting the string on a C++ program.

    When the C++ program gets opened, it reads the string.

    But there is a small problem. Once it reads the string, It only picks the first Character.

    Like if I store "Hello" in the resources. The messagebox only shows "H"

    Code:
        HRSRC hRsrc3 = FindResource(0, "5", "RT_RCDATA"); //RT_RCDATA
        HGLOBAL hGlobal3 = LoadResource(0, hRsrc3);
        LPCSTR messs = (LPCSTR)LockResource(hGlobal3);
    
        MessageBox(NULL, messs, "bonjour(s)", MB_OK);
    I tried wchar_t, char* and LPCSTR, all give the same result.

    Thanks in advance.
    Here is the high-level reason why your code is not working:

    The MessageBox() is an ANSI function, not a Unicode aware function. Unless you change the call to MessageBox() to a Unicode-aware function, you can try all sorts of combinations and the results will be the same. In other words, an ANSI function isn't going to magically process Unicode strings correctly.

    I say this is the high-level reason, since it can occur with any function you call that only knows about ANSI strings. Give that function a Unicode string, and nothing will make that function act differently.

    Now as to the detailed reason -- the MessageBox() function is not really a function -- it is a macro. If your build is an ANSI build, the function is really MessageBoxA. If you have a Unicode build, then the function is MessageBoxW. So you probably have your project set to ANSI (or MBCS), causing the wrong version of MessageBox() to be called.

    So you have a choice -- change your build to Unicode (IMO preferred method), or call the MessageBoxW() function directly.

    Also, look at the MessageBox() description, and the second/third parameter:

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    You will see that the second and third parameters are LPCTSTR, not LPCSTR.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 10th, 2012 at 08:20 PM.

  3. #3
    Join Date
    May 2012
    Posts
    43

    Re: Reading string out of embedded resources

    Thanks, I tried those things and none worked. Looks like it has nothing to do with that.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Reading string out of embedded resources

    Quote Originally Posted by qwerz View Post
    Thanks, I tried those things and none worked. Looks like it has nothing to do with that.
    Why not tell us what exactly is sitting at that address that is returned instead of us having to guess?

    Use your debugger -- go to that address and see what is at that memory location and the subsequent memory locations. If you expect "Hello" as the string, is it:
    Code:
    'H', 0, 'e', 0, 'l', 0, 'l', 0, 'o', 0
    ?
    In other words, a 0 byte is between each letter? If it is, then that is a Unicode string, and you did not do as stated in my previous post (the MessageBox(W) function would have worked). Also, if you changed your build to a Unicode build, you would have had a compiler error with the code you have now, as the third parameter to MessageBox would have been incorrect (you are sending a char*, and the third parameter expects a wide character string).

    Another thing -- use LPCTSTR, TCHAR, _T(), TEXT(), etc.

    Regards,

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

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: Reading string out of embedded resources

    Perhaps you'll show us your unicode aware example that not worked?
    Victor Nijegorodov

  6. #6
    Join Date
    May 2012
    Posts
    43

    Re: Reading string out of embedded resources

    Quote Originally Posted by Paul McKenzie View Post
    Why not tell us what exactly is sitting at that address that is returned instead of us having to guess?

    Use your debugger -- go to that address and see what is at that memory location and the subsequent memory locations. If you expect "Hello" as the
    Because I can not debug that line. The resources get added after I updated the resources with the C# program. So for that, I first have to fully build the program (and not debug).

    Ill try the MessageBoxW now and post the result.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: Reading string out of embedded resources

    Quote Originally Posted by qwerz View Post
    Because I can not debug that line. The resources get added after I updated the resources with the C# program. So for that, I first have to fully build the program (and not debug).

    Just update "the resources with the C# program" in your debug build (.exe)
    Then start debugging this exe.
    Victor Nijegorodov

  8. #8
    Join Date
    May 2012
    Posts
    43

    Re: Reading string out of embedded resources

    This is my test with MessageBoxW:

    Result should be "TestingString"

    Code:
        char *messs = (char*)LockResource(hGlobal3);
    
        MessageBox(NULL, (LPCTSTR)messs, "bonjour", MB_OK);
        MessageBoxW(NULL, (LPCWSTR)messs, (LPCWSTR)"bonjour", MB_OK);
    MessageBoxA only shows the first letter.
    MessageBoxW shows the incomplete "Testin"-characters with strange things around it.

  9. #9
    Join Date
    May 2012
    Posts
    43

    Re: Reading string out of embedded resources

    The problem is not in the resources I think.. because even the title has those boxes, while that string was not even from the resources but straight (LPCWSTR)"bonjour"

  10. #10
    Join Date
    Apr 1999
    Posts
    27,446

    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 12:56 PM.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Reading string out of embedded resources

    Quote Originally Posted by qwerz View Post
    while that string was not even from the resources but straight (LPCWSTR)"bonjour"
    As my post pointed out that code is invalid.

    You are taking an ANSI string, and believing that a "dumb" C-style cast knows how to turn that ANSI string into a wide string. All that cast did was to shut the compiler up. Strings must be converted, and you have to call conversion functions to do this.

    Regards,

    Paul McKenzie

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center