Extracting files from resource
-
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.
-
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.
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()
andLoadResource()
returnsNULL
in case of an error, you are not even performing any checks for that condition too. - Are you sure the call toCreateFile()
is succeeding, try usingGetLastError()
to know why it failed. - You are not comparing the value of variableWritten
with the variableSize
to know exactly if a call toWriteFile()
succeeded or not. Points regarding your code: 1. Please moveUnlockResource()
out of the if block. 2. It is not necessary for Win32-based applications toFreeResource
. 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
-
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.
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"
-
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.
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?
-
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.
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