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. Extracting files from resource

Extracting files from resource

Scheduled Pinned Locked Moved C / C++ / MFC
helplearning
5 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    What I am trying to do is extract a file from a resource then write it into the system directory of windows. It compiles but it doesn't run correctly. I defined 1001 in resource.h and I included it also. I need help with this code here: #include #include "resource.h" void main(void) { char szFile[MAX_PATH]; char sysdir[MAX_PATH]; DWORD Size; DWORD Written; HINSTANCE hMod = GetModuleHandle(NULL); HRSRC hRes = FindResource(hMod, MAKEINTRESOURCE(1001), RT_RCDATA); Size = SizeofResource(hMod, hRes); HGLOBAL hLoad = LoadResource(hMod, hRes); LPVOID lpResLock = LockResource(hLoad); GetSystemDirectory(sysdir, sizeof(sysdir)); strcat(sysdir, "\\abcdef.exe"); strcpy(szFile, sysdir); HANDLE hMake = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM, NULL); if (hMake != INVALID_HANDLE_VALUE) { WriteFile(hMake, lpResLock, Size, &Written, NULL); CloseHandle(hMake); UnlockResource(hLoad); FreeResource(hLoad); } } If anybody has code similar to this that works please post it.

    G D R D 4 Replies Last reply
    0
    • A Anonymous

      What I am trying to do is extract a file from a resource then write it into the system directory of windows. It compiles but it doesn't run correctly. I defined 1001 in resource.h and I included it also. I need help with this code here: #include #include "resource.h" void main(void) { char szFile[MAX_PATH]; char sysdir[MAX_PATH]; DWORD Size; DWORD Written; HINSTANCE hMod = GetModuleHandle(NULL); HRSRC hRes = FindResource(hMod, MAKEINTRESOURCE(1001), RT_RCDATA); Size = SizeofResource(hMod, hRes); HGLOBAL hLoad = LoadResource(hMod, hRes); LPVOID lpResLock = LockResource(hLoad); GetSystemDirectory(sysdir, sizeof(sysdir)); strcat(sysdir, "\\abcdef.exe"); strcpy(szFile, sysdir); HANDLE hMake = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM, NULL); if (hMake != INVALID_HANDLE_VALUE) { WriteFile(hMake, lpResLock, Size, &Written, NULL); CloseHandle(hMake); UnlockResource(hLoad); FreeResource(hLoad); } } If anybody has code similar to this that works please post it.

      G Offline
      G Offline
      gUrM33T
      wrote on last edited by
      #2

      So where exactly are you facing problem in this code? How are you going to know where the error occured as: - Your code never calls GetLastError() to find out if a call to WIN32 Resource API succeeded or not. - FindResource(), LockResource() and LoadResource() returns NULL in case of an error, you are not even performing any checks for that condition too. - Are you sure the call to CreateFile() is succeeding, try using GetLastError() to know why it failed. - You are not comparing the value of variable Written with the variable Size to know exactly if a call to WriteFile() succeeded or not. Points regarding your code: 1. Please move UnlockResource() out of the if block. 2. It is not necessary for Win32-based applications to FreeResource. A resource is automatically freed when its module is unloaded. Gurmeet


      BTW, can Google help me search my lost pajamas?

      My Articles: HTML Reader C++ Class Library, Numeric Edit Control

      1 Reply Last reply
      0
      • A Anonymous

        What I am trying to do is extract a file from a resource then write it into the system directory of windows. It compiles but it doesn't run correctly. I defined 1001 in resource.h and I included it also. I need help with this code here: #include #include "resource.h" void main(void) { char szFile[MAX_PATH]; char sysdir[MAX_PATH]; DWORD Size; DWORD Written; HINSTANCE hMod = GetModuleHandle(NULL); HRSRC hRes = FindResource(hMod, MAKEINTRESOURCE(1001), RT_RCDATA); Size = SizeofResource(hMod, hRes); HGLOBAL hLoad = LoadResource(hMod, hRes); LPVOID lpResLock = LockResource(hLoad); GetSystemDirectory(sysdir, sizeof(sysdir)); strcat(sysdir, "\\abcdef.exe"); strcpy(szFile, sysdir); HANDLE hMake = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM, NULL); if (hMake != INVALID_HANDLE_VALUE) { WriteFile(hMake, lpResLock, Size, &Written, NULL); CloseHandle(hMake); UnlockResource(hLoad); FreeResource(hLoad); } } If anybody has code similar to this that works please post it.

        D Offline
        D Offline
        Diddy
        wrote on last edited by
        #3

        Here is a function that i wrote a while back that does work BOOL ExtractResource(const HINSTANCE hInstance, const WORD nID, LPCTSTR szFilename) { BOOL bSuccess = FALSE; HRSRC hResource = NULL; if((hResource = ::FindResource(hInstance, MAKEINTRESOURCE(nID), _T("BINARY"))) != NULL) { HGLOBAL hFileResource = NULL; if((hFileResource = ::LoadResource(hInstance, hResource)) != NULL) { LPVOID lpFile = NULL; if((lpFile = ::LockResource(hFileResource)) != NULL) { DWORD dwSize = ::SizeofResource(hInstance, hResource); if(dwSize > 0) { HANDLE hFile = INVALID_HANDLE_VALUE; if((hFile = ::CreateFile(szFilename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) { HANDLE hFilemap = NULL; if((hFilemap = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwSize, NULL)) != NULL) { LPVOID lpBaseAddress = NULL; if((lpBaseAddress = MapViewOfFile(hFilemap, FILE_MAP_WRITE, 0, 0, 0)) != NULL) { ::CopyMemory(lpBaseAddress, lpFile, dwSize); bSuccess = TRUE; ::UnmapViewOfFile(lpBaseAddress); } ::CloseHandle(hFilemap); } ::CloseHandle(hFile); } } } } } } return bSuccess; } Call it like: ExtractResource(::AfxGetInstanceHandle (), 1001, "C:\\Whatever"); You then put your files into you RC file like this: 1001 BINARY MOVEABLE PURE "C:\\WhatEverFile"

        1 Reply Last reply
        0
        • A Anonymous

          What I am trying to do is extract a file from a resource then write it into the system directory of windows. It compiles but it doesn't run correctly. I defined 1001 in resource.h and I included it also. I need help with this code here: #include #include "resource.h" void main(void) { char szFile[MAX_PATH]; char sysdir[MAX_PATH]; DWORD Size; DWORD Written; HINSTANCE hMod = GetModuleHandle(NULL); HRSRC hRes = FindResource(hMod, MAKEINTRESOURCE(1001), RT_RCDATA); Size = SizeofResource(hMod, hRes); HGLOBAL hLoad = LoadResource(hMod, hRes); LPVOID lpResLock = LockResource(hLoad); GetSystemDirectory(sysdir, sizeof(sysdir)); strcat(sysdir, "\\abcdef.exe"); strcpy(szFile, sysdir); HANDLE hMake = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM, NULL); if (hMake != INVALID_HANDLE_VALUE) { WriteFile(hMake, lpResLock, Size, &Written, NULL); CloseHandle(hMake); UnlockResource(hLoad); FreeResource(hLoad); } } If anybody has code similar to this that works please post it.

          R Offline
          R Offline
          Roger Allen
          wrote on last edited by
          #4

          Here is how I have done it in an MFC application: In the .rc2 file, include the file you want to extract

          ResourceName ResourceType "ResourceFilename"

          To extract it:

          ExtractFile("ResourceName", "ResourceType", pathname);
          

          // note that hInstance is the instance off the DLL/exe which has the resource

          bool ExtractFile(const CString& resourceID, const CString& resourceType, const CString& filename)
          {
          // need to extract the resource out into the filename supplied
          bool bOK = true;
          HANDLE hRes = ::LoadResource(hInstance, ::FindResource(hInstance, resourceID, resourceType));
          if (hRes != INVALID_HANDLE_VALUE)
          {
          // we loaded it, not get the text from it
          DWORD sizeOfResource = ::SizeofResource(hInstance, ::FindResource(hInstance, resourceID, resourceType));
          char *lpRes = (char*)::LockResource(hRes);
          CFile file;

          	if (file.Open(filename, CFile::modeCreate | CFile::modeWrite))
          	{
          		// write the resource out to the file
          		file.WriteHuge(lpRes, sizeOfResource);
          		file.Close();
          	}
          	else
          	{
          		bOK = false;
          	}
          	// release the resource
          	::UnlockResource(hRes);
          	::FreeResource(hRes);
          }
          return bOK;
          

          }

          Roger Allen - Sonork 100.10016 Strong Sad: I am sad I am flying Who is your favorite Strong?

          1 Reply Last reply
          0
          • A Anonymous

            What I am trying to do is extract a file from a resource then write it into the system directory of windows. It compiles but it doesn't run correctly. I defined 1001 in resource.h and I included it also. I need help with this code here: #include #include "resource.h" void main(void) { char szFile[MAX_PATH]; char sysdir[MAX_PATH]; DWORD Size; DWORD Written; HINSTANCE hMod = GetModuleHandle(NULL); HRSRC hRes = FindResource(hMod, MAKEINTRESOURCE(1001), RT_RCDATA); Size = SizeofResource(hMod, hRes); HGLOBAL hLoad = LoadResource(hMod, hRes); LPVOID lpResLock = LockResource(hLoad); GetSystemDirectory(sysdir, sizeof(sysdir)); strcat(sysdir, "\\abcdef.exe"); strcpy(szFile, sysdir); HANDLE hMake = CreateFile(szFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM, NULL); if (hMake != INVALID_HANDLE_VALUE) { WriteFile(hMake, lpResLock, Size, &Written, NULL); CloseHandle(hMake); UnlockResource(hLoad); FreeResource(hLoad); } } If anybody has code similar to this that works please post it.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            See if this article is of any help.


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            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