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. if I add a text file to the resource, how could I get this file in the programe?

if I add a text file to the resource, how could I get this file in the programe?

Scheduled Pinned Locked Moved C / C++ / MFC
htmljsonquestionlearning
8 Posts 3 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.
  • B Offline
    B Offline
    benben
    wrote on last edited by
    #1

    Hi: I add a text file(or html file) to the resource of the project, how could I get this file in the programe?I want to get its content .Is there any API function to do so? Thanks Benben

    E J 2 Replies Last reply
    0
    • B benben

      Hi: I add a text file(or html file) to the resource of the project, how could I get this file in the programe?I want to get its content .Is there any API function to do so? Thanks Benben

      E Offline
      E Offline
      Eugene Pustovoyt
      wrote on last edited by
      #2

      And how you have added it? Has wrote a his path as string resource or how? Best regards, Eugene Pustovoyt

      B 1 Reply Last reply
      0
      • B benben

        Hi: I add a text file(or html file) to the resource of the project, how could I get this file in the programe?I want to get its content .Is there any API function to do so? Thanks Benben

        J Offline
        J Offline
        Jens Doose
        wrote on last edited by
        #3

        Try these Win32-API functions: FindResource LoadResource SizeofResource LockResource That will do the trick! Yours, Jens

        B 1 Reply Last reply
        0
        • J Jens Doose

          Try these Win32-API functions: FindResource LoadResource SizeofResource LockResource That will do the trick! Yours, Jens

          B Offline
          B Offline
          benben
          wrote on last edited by
          #4

          But could you give me an demo? Thanks Benben

          J 1 Reply Last reply
          0
          • B benben

            But could you give me an demo? Thanks Benben

            J Offline
            J Offline
            Jens Doose
            wrote on last edited by
            #5

            Sure. Let's assume you have added a textfile to your resource with ID=IDR_TEXTFILE, and your custom resource type has the name "TEXTFILE". You can extract the resource with the following code:

            #include "stdafx.h"
            #include "resource.h"

            #include <TCHAR.h>

            int APIENTRY WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            LPSTR lpCmdLine,
            int nCmdShow)
            {
            HRSRC hRsrc = ::FindResource( hInstance,
            MAKEINTRESOURCE( IDR_TEXTFILE ),
            _T("TEXTFILE") ); // *** My custom res type

            if ( NULL != hRsrc )
            {
            HGLOBAL hGlob = ::LoadResource( NULL, hRsrc );

            if ( NULL != hGlob )
            {
              DWORD dwSize  = ::SizeofResource( NULL, hRsrc );
              LPVOID lpVoid = ::LockResource( hGlob );
            
              if ( NULL != lpVoid )
              {
                // \*\*\* Now you can work with the loaded resource.
            
                // \*\*\* In this case, we now it is a text file,
                // \*\*\* so cast it to a string
                LPCTSTR lpszString = reinterpret\_cast< LPCTSTR>( lpVoid );
            
                MessageBox( NULL, lpszString, \_T("From resource"), MB\_OK );
              }
            }
            

            };

            return 0;
            }

            That's it ;-) Jens

            B 1 Reply Last reply
            0
            • J Jens Doose

              Sure. Let's assume you have added a textfile to your resource with ID=IDR_TEXTFILE, and your custom resource type has the name "TEXTFILE". You can extract the resource with the following code:

              #include "stdafx.h"
              #include "resource.h"

              #include <TCHAR.h>

              int APIENTRY WinMain(HINSTANCE hInstance,
              HINSTANCE hPrevInstance,
              LPSTR lpCmdLine,
              int nCmdShow)
              {
              HRSRC hRsrc = ::FindResource( hInstance,
              MAKEINTRESOURCE( IDR_TEXTFILE ),
              _T("TEXTFILE") ); // *** My custom res type

              if ( NULL != hRsrc )
              {
              HGLOBAL hGlob = ::LoadResource( NULL, hRsrc );

              if ( NULL != hGlob )
              {
                DWORD dwSize  = ::SizeofResource( NULL, hRsrc );
                LPVOID lpVoid = ::LockResource( hGlob );
              
                if ( NULL != lpVoid )
                {
                  // \*\*\* Now you can work with the loaded resource.
              
                  // \*\*\* In this case, we now it is a text file,
                  // \*\*\* so cast it to a string
                  LPCTSTR lpszString = reinterpret\_cast< LPCTSTR>( lpVoid );
              
                  MessageBox( NULL, lpszString, \_T("From resource"), MB\_OK );
                }
              }
              

              };

              return 0;
              }

              That's it ;-) Jens

              B Offline
              B Offline
              benben
              wrote on last edited by
              #6

              Thanks Benben

              J 1 Reply Last reply
              0
              • E Eugene Pustovoyt

                And how you have added it? Has wrote a his path as string resource or how? Best regards, Eugene Pustovoyt

                B Offline
                B Offline
                benben
                wrote on last edited by
                #7

                just try "import" the file to your resource Thanks Benben

                1 Reply Last reply
                0
                • B benben

                  Thanks Benben

                  J Offline
                  J Offline
                  Jens Doose
                  wrote on last edited by
                  #8

                  Hmmm, one annotation: The more I think about it the more I get convinced, that it is not allowed to just cast the memory pointer to an LPCTSTR, because the trailing zero is missing. Instead it should be copied to an according buffer. So it should be:

                  LPCTSTR lpszString = reinterpret\_cast< LPCTSTR>( lpVoid );
                  TCHAR \*tcBuffer    = new TCHAR\[ dwSize + 1 \];
                  if ( NULL != tcBuffer )
                  {
                    ZeroMemory( tcBuffer, ( dwSize + 1 ) \* sizeof( TCHAR ) );
                    memcpy( tcBuffer, lpszString, dwSize \* sizeof( TCHAR ) );
                  }
                  

                  But this is only the case with strings. Byte buffer or similar can't be used directly anyway (like LPCTSTR). Jens

                  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