How load file  from resource file

Maverick_Cool
Maverick_Cool used Ask the Experts™
on
I have added cab file to to vc++ resource file(.rc)
I want to load this file at run time. My project is non-mfc based.
please provide code
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I am not exactly sure what you mean by 'load', but if you want to access the bytes of a resource, you first call FindResource to find the resource, followed by LoadResource to load it into memory, followed by LockResource to gain access to the bytes.

http://msdn.microsoft.com/en-us/library/ms648042(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms648046(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms648047(VS.85).aspx

Something like the following code snippet


HRSRC	hRes;
HGLOBAL	hData;
LPVOID	pRes;
 
if (hRes = ::FindResource((HMODULE) hInst, MAKEINTRESOURCE(ID_MVRES), _T("BINARY")))
{
	if (hData = ::LoadResource((HMODULE) hInst, hRes))
	{
		if (pRes = ::LockResource((HGLOBAL) hData))
		{
			// pRes now pointer to the first byte of the resource
		}
	}
}

Open in new window

Author

Commented:
I have already have little bit of code but its giving 4 bytes , the file is cab type.

  TCHAR  sResName[9]  = _T("#197");
	TCHAR sRestype[4] = _T("CAB");
	HRSRC hres = FindResource(NULL, sResName, sRestype);
	HGLOBAL	hbytes = LoadResource(NULL, hres);
	LPVOID pdata = LockResource(hbytes);
	LPBYTE sData = (LPBYTE)pdata;
	LPTSTR strData = (LPTSTR)sData;

Open in new window

Commented:
What do you mean when you say it is "giving 4 bytes".  You do realize that a cabinet file is a binary file, not a text file?  You are casting pdata to a LPTSTR, what are you expecting to see?  You can get the size of the resource by calling SizeofResource:

http://msdn.microsoft.com/en-us/library/ms648048(VS.85).aspx
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
see my goal:
I the have file name say a.cab
now i want to retrieve that file resource file put it to :
FILE *f;
File is defined in wchar.h as
typedef struct _iobuf FILE;
please provide code

Author

Commented:
or put put it to char* buf and get its length

Author

Commented:
at the end it will go to std::string.
Commented:
I still don't understand what you are trying to do.  A .cab file is like a .zip file.  It is a binary file which contains a set of files in a compressed form.  If you put a .cab file into a resource, you can access the bytes of that file, but if you want to extract files from the .cab, you will probably need to copy it to a temporary file on your disk.  Is that what you are trying to do?

Author

Commented:
I figured a work aroubd

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial