Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Std::string loadstring???

Std::string loadstring???

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
20 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gmallax

    Hi, CString sz; sz.LoadString( HINSATANCE hin,UINT id); How to achieve the above thing by using std::string instead of cstring??? Thanx.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    Hi, First you need to call FindResource[^] to find the resource string then you can call SizeofResource[^] to get the size of the string so you can resize your std::string and finally call LoadString[^] to copy it. Best Wishes, -David Delaune

    L 1 Reply Last reply
    0
    • G gmallax

      Hi, CString sz; sz.LoadString( HINSATANCE hin,UINT id); How to achieve the above thing by using std::string instead of cstring??? Thanx.

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #5

      Load it into a character array first then assign the result to the std::string.

      std::string load_string_from_resource( HINSTANCE app_instance, unsigned string_ID )
      {
      char buffer[ 4096 ];
      unsigned bytes_copied = LoadString( app_instance, string_ID, buffer, sizeof( buffer ) );
      if( !bytes_copied )
      throw std::runtime_error( "Resource not found!" );

      return std::string( buffer, bytes\_copied );
      

      } (Code not tested, Errors and Omissions Excluded) Cheers, Ash

      S 1 Reply Last reply
      0
      • A Aescleal

        Load it into a character array first then assign the result to the std::string.

        std::string load_string_from_resource( HINSTANCE app_instance, unsigned string_ID )
        {
        char buffer[ 4096 ];
        unsigned bytes_copied = LoadString( app_instance, string_ID, buffer, sizeof( buffer ) );
        if( !bytes_copied )
        throw std::runtime_error( "Resource not found!" );

        return std::string( buffer, bytes\_copied );
        

        } (Code not tested, Errors and Omissions Excluded) Cheers, Ash

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #6

        If you're using Unicode it can be done more efficiently than that:

        std::wstring LoadString(HINSTANCE hInstance, UINT uID)
        {
        LPCWSTR pString;
        int res = LoadStringW(hInstance, uID, (LPWSTR)(&pString), 0);
        if (res == 0)
        return std::wstring(); // No such string.
        return std::wstring(pString, res);
        }

        MSDN on LoadString[^]:

        If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.

        NOTE: Raw string resources are not NULL terminated.

        Steve

        A 1 Reply Last reply
        0
        • S Stephen Hewitt

          If you're using Unicode it can be done more efficiently than that:

          std::wstring LoadString(HINSTANCE hInstance, UINT uID)
          {
          LPCWSTR pString;
          int res = LoadStringW(hInstance, uID, (LPWSTR)(&pString), 0);
          if (res == 0)
          return std::wstring(); // No such string.
          return std::wstring(pString, res);
          }

          MSDN on LoadString[^]:

          If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.

          NOTE: Raw string resources are not NULL terminated.

          Steve

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #7

          Cool, I'd either forgotten about or never known the behaviour of LoadString when you pass zero as the buffer size. What's the reason this wouldn't work on ANSI as well?

          S 1 Reply Last reply
          0
          • A Aescleal

            Cool, I'd either forgotten about or never known the behaviour of LoadString when you pass zero as the buffer size. What's the reason this wouldn't work on ANSI as well?

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #8

            I'm not positive, but it doesn't seem to. Note that the documentation says it returns a pointer to the raw resource and string resources are always Unicode, so perhaps this is a reason. In fact, in the ANSI case 0xFFFFffff was returned to indicate an error (when 0 is passed as the buffer size), which isn't mentioned in the documentation.

            Steve

            A 1 Reply Last reply
            0
            • L Lost User

              Hi, First you need to call FindResource[^] to find the resource string then you can call SizeofResource[^] to get the size of the string so you can resize your std::string and finally call LoadString[^] to copy it. Best Wishes, -David Delaune

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              Hi, Not sure who gave me a one vote... but the solution I presented is a secure and safe method for loading a std::string from a resource. The method presented by Aescleal will fail for a string greater than the 4096 constant. The wstring solution presented by Stephen Hewitt just flat out doesn't work for std::string Best Wishes, -David Delaune

              S A 2 Replies Last reply
              0
              • L Lost User

                Hi, Not sure who gave me a one vote... but the solution I presented is a secure and safe method for loading a std::string from a resource. The method presented by Aescleal will fail for a string greater than the 4096 constant. The wstring solution presented by Stephen Hewitt just flat out doesn't work for std::string Best Wishes, -David Delaune

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #10

                It does for a wstring (which was all it was intended to, it wasn't a reply to the OP but to Aescleal). It's documented. MSDN (on LoadString):

                nBufferMax [in] int The size of the buffer, in characters. The string is truncated and null-terminated if it is longer than the number of characters specified. If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.

                Secondly, I tried it (after reading the documentation thoroughly first) and verified that it did work. Finally, your technique will not work. LoadResource returns nothing that LoadString can consume.

                Steve

                L 2 Replies Last reply
                0
                • S Stephen Hewitt

                  I'm not positive, but it doesn't seem to. Note that the documentation says it returns a pointer to the raw resource and string resources are always Unicode, so perhaps this is a reason. In fact, in the ANSI case 0xFFFFffff was returned to indicate an error (when 0 is passed as the buffer size), which isn't mentioned in the documentation.

                  Steve

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #11

                  Thanks for that, I'll stick that on the list as the first new thing I've learnt today. Ash

                  1 Reply Last reply
                  0
                  • L Lost User

                    Hi, Not sure who gave me a one vote... but the solution I presented is a secure and safe method for loading a std::string from a resource. The method presented by Aescleal will fail for a string greater than the 4096 constant. The wstring solution presented by Stephen Hewitt just flat out doesn't work for std::string Best Wishes, -David Delaune

                    A Offline
                    A Offline
                    Aescleal
                    wrote on last edited by
                    #12

                    I didn't give you a 1 vote but the method I outlined will work for strings greater than 4096 characters, it just won't load the whole string. If you're really worried about that you have to use the OS/2 vintage "try it once to fail, once to succeed" method of sizing buffers. And it'd be fairly easy to change Stephen's method to work for std::string - depending on how much you know about the resource and the characters in it it's either trivial (use the two iterator string constructor) or slightly harder (use wcstombs). Ash

                    1 Reply Last reply
                    0
                    • S Stephen Hewitt

                      It does for a wstring (which was all it was intended to, it wasn't a reply to the OP but to Aescleal). It's documented. MSDN (on LoadString):

                      nBufferMax [in] int The size of the buffer, in characters. The string is truncated and null-terminated if it is longer than the number of characters specified. If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.

                      Secondly, I tried it (after reading the documentation thoroughly first) and verified that it did work. Finally, your technique will not work. LoadResource returns nothing that LoadString can consume.

                      Steve

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      Stephen Hewitt wrote:

                      Finally, your technique will not work. LoadResource returns nothing that LoadString can consume.

                      Hi Stephen, You may be correct. I was thinking that you could just do something like this:

                      HRSRC hres = FindResource(NULL, MAKEINTRESOURCE(IDS_SOMESTRING), RT_STRING);
                      DWORD dwSizeRes = SizeofResource(NULL,hres);
                      char *p = NULL;
                      int result = LoadString((HINSTANCE)&__ImageBase,AFX_IDS_APP_TITLE,p,0);
                      std::string s(p,dwSizeRes);
                      

                      Which is not much different than what you posted. With the method that you posted...I am thinking that the std::string would need a NULL terminator in the string resource. I admit that I have not tried either method (including the one I just typed above). I will test both methods later when I get some free time. Best Wishes, -David Delaune

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        It does for a wstring (which was all it was intended to, it wasn't a reply to the OP but to Aescleal). It's documented. MSDN (on LoadString):

                        nBufferMax [in] int The size of the buffer, in characters. The string is truncated and null-terminated if it is longer than the number of characters specified. If this parameter is 0, then lpBuffer receives a read-only pointer to the resource itself.

                        Secondly, I tried it (after reading the documentation thoroughly first) and verified that it did work. Finally, your technique will not work. LoadResource returns nothing that LoadString can consume.

                        Steve

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        Hey Steve :) I just spent 30 minutes testing both of our code samples. I could not get the code you wrote working for std::string probably because the resource strings are actually stored in Unicode within the PE files. However for wstrings your code is absolutely perfect. I suspect... that the brilliant code you posted could be actually be modified to utilize wcstombs and work for both ANSI and Unicode builds. As for my code sample... I had even less success although I was eventually able to get it working. I had to re-read some of the MSDN docs and ended up using FindResourceEx with the language identifier. To make things worse the address I was recieving was the start of the string resource block. I had to load the string block into an HGLOBAL and walk the string table before I could even read the string. :doh: Anyway I just wanted to let you know that you were correct. :) Best Wishes, -David Delaune

                        S 1 Reply Last reply
                        0
                        • G gmallax

                          Hi, CString sz; sz.LoadString( HINSATANCE hin,UINT id); How to achieve the above thing by using std::string instead of cstring??? Thanx.

                          A Offline
                          A Offline
                          Alain Rist
                          wrote on last edited by
                          #15

                          See my answer including Stephen Hewitt contribution at http://www.codeproject.com/Tips/86496/Load-a-Windows-string-resource-into-a-std-string-o.aspx[^] cheers, AR

                          When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                          G 2 Replies Last reply
                          0
                          • L Lost User

                            Hey Steve :) I just spent 30 minutes testing both of our code samples. I could not get the code you wrote working for std::string probably because the resource strings are actually stored in Unicode within the PE files. However for wstrings your code is absolutely perfect. I suspect... that the brilliant code you posted could be actually be modified to utilize wcstombs and work for both ANSI and Unicode builds. As for my code sample... I had even less success although I was eventually able to get it working. I had to re-read some of the MSDN docs and ended up using FindResourceEx with the language identifier. To make things worse the address I was recieving was the start of the string resource block. I had to load the string block into an HGLOBAL and walk the string table before I could even read the string. :doh: Anyway I just wanted to let you know that you were correct. :) Best Wishes, -David Delaune

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #16

                            Thanks.

                            Steve

                            1 Reply Last reply
                            0
                            • A Alain Rist

                              See my answer including Stephen Hewitt contribution at http://www.codeproject.com/Tips/86496/Load-a-Windows-string-resource-into-a-std-string-o.aspx[^] cheers, AR

                              When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                              G Offline
                              G Offline
                              gmallax
                              wrote on last edited by
                              #17

                              Very nice and useful..Thanx :)

                              A 1 Reply Last reply
                              0
                              • G gmallax

                                Very nice and useful..Thanx :)

                                A Offline
                                A Offline
                                Alain Rist
                                wrote on last edited by
                                #18

                                gmallax wrote:

                                Very nice and useful

                                Then please upvote. Thanks, AR

                                When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                                1 Reply Last reply
                                0
                                • A Alain Rist

                                  See my answer including Stephen Hewitt contribution at http://www.codeproject.com/Tips/86496/Load-a-Windows-string-resource-into-a-std-string-o.aspx[^] cheers, AR

                                  When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                                  G Offline
                                  G Offline
                                  gmallax
                                  wrote on last edited by
                                  #19

                                  hi, here we declare char buffer as char buffer[ 1024 ] = { '\0' }.. How to release this?

                                  A 1 Reply Last reply
                                  0
                                  • G gmallax

                                    hi, here we declare char buffer as char buffer[ 1024 ] = { '\0' }.. How to release this?

                                    A Offline
                                    A Offline
                                    Alain Rist
                                    wrote on last edited by
                                    #20

                                    It is released at the next }

                                    ReturnType SomeClass::SomeFunction(SomeParams)
                                    {
                                    char buffer[1024] = {0}; // this a stack allocation
                                    // any code ...
                                    return ReturnType::SomeValue;
                                    } // buffer is deallocated here

                                    cheers, AR

                                    When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups