Images in Dll
-
in MFC application am using JPEG images which are using as background images for my dialogs. fro the am carrying image filtes with my application instead of that can i set imaged into dll and load the images from dll whenever required?
Yes. Add them into the resource file. I've added an HTML file (contained in the 'res' sub-directory of my project) to my resources using this entry in the resource (.RC) file:
ABOUTDIALOG.HTML HTML "res\\aboutdia.htm"
You could use (for example)
TEST.JPG JPG "res\\test.jpg"
You can then use a code fragment like the following to load a resource into memory:
std::string LoadFileFromResource(HINSTANCE hmodResource, LPCTSTR name, LPCTSTR type)
{
if (!type)
type = ::PathFindExtension(name) + 1;
HRSRC rsrcFile = ::FindResource(hmodResource, name, type);
if (!rsrcFile) return std::string();
HGLOBAL gblFile = ::LoadResource(hmodResource, rsrcFile);
if (!gblFile) return std::string();
DWORD sizeFile = ::SizeofResource(hmodResource, rsrcFile);
if (!sizeFile) return std::string();
LPVOID filePointer = ::LockResource(gblFile);
if (filePointer)
{
std::string contents((char*)filePointer, sizeFile);
::FreeResource(gblFile);
return contents;
}
return std::string();
}[edit]You'd call that function a bit like this:
std::string loadedJpeg = LoadFileFromResource(resourceDllHandle, _T("TEST.JPG"), _T("JPG"));
std::string
might not be the best result type for you, as you're loading a binary file - I've only loaded text files with this...[/edit] HTH!Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
modified on Thursday, April 2, 2009 5:25 AM
-
Yes. Add them into the resource file. I've added an HTML file (contained in the 'res' sub-directory of my project) to my resources using this entry in the resource (.RC) file:
ABOUTDIALOG.HTML HTML "res\\aboutdia.htm"
You could use (for example)
TEST.JPG JPG "res\\test.jpg"
You can then use a code fragment like the following to load a resource into memory:
std::string LoadFileFromResource(HINSTANCE hmodResource, LPCTSTR name, LPCTSTR type)
{
if (!type)
type = ::PathFindExtension(name) + 1;
HRSRC rsrcFile = ::FindResource(hmodResource, name, type);
if (!rsrcFile) return std::string();
HGLOBAL gblFile = ::LoadResource(hmodResource, rsrcFile);
if (!gblFile) return std::string();
DWORD sizeFile = ::SizeofResource(hmodResource, rsrcFile);
if (!sizeFile) return std::string();
LPVOID filePointer = ::LockResource(gblFile);
if (filePointer)
{
std::string contents((char*)filePointer, sizeFile);
::FreeResource(gblFile);
return contents;
}
return std::string();
}[edit]You'd call that function a bit like this:
std::string loadedJpeg = LoadFileFromResource(resourceDllHandle, _T("TEST.JPG"), _T("JPG"));
std::string
might not be the best result type for you, as you're loading a binary file - I've only loaded text files with this...[/edit] HTH!Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
modified on Thursday, April 2, 2009 5:25 AM
-
Yes, so long as you load the DLL with a
LoadLibrary
call.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Yes, so long as you load the DLL with a
LoadLibrary
call.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
When you build the DLL, embed the jpeg files in the DLL's resources, as I described before. In your executable, call LoadLibrary for the external DLL containing the image files. That'll return the HMODULE you need to pass into the LoadFileFromResource function I gave you the source for. Then you can load the image files from the DLL as desired.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
When you build the DLL, embed the jpeg files in the DLL's resources, as I described before. In your executable, call LoadLibrary for the external DLL containing the image files. That'll return the HMODULE you need to pass into the LoadFileFromResource function I gave you the source for. Then you can load the image files from the DLL as desired.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
I created MFC dll with JPG images as you explained, but when try to load into exe, FindResource() returning ERROR_RESOURCE_DATA_NOT_FOUND. Can you suggest me what to do (I have an application inc which dialogs and contros skinned with jpg images, so am carrying jpg images with my application, for that want to create dll and load images whenever required), what type of Dll(Win32 or MFC) need to build? IF MFC why it is failing to find resource, if win32 how images can add into dll.
-
I created MFC dll with JPG images as you explained, but when try to load into exe, FindResource() returning ERROR_RESOURCE_DATA_NOT_FOUND. Can you suggest me what to do (I have an application inc which dialogs and contros skinned with jpg images, so am carrying jpg images with my application, for that want to create dll and load images whenever required), what type of Dll(Win32 or MFC) need to build? IF MFC why it is failing to find resource, if win32 how images can add into dll.
Win32 DLL is fine - just create a Win32 DLL project and add the resources. You might have to export a routine for it to make the DLL. For the MFC one - have you got the correct HMODULE for the DLL (use LoadLibrary to get the correct one)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Win32 DLL is fine - just create a Win32 DLL project and add the resources. You might have to export a routine for it to make the DLL. For the MFC one - have you got the correct HMODULE for the DLL (use LoadLibrary to get the correct one)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Ya am loading dll using Loadlibrary, but FindResource is failing. How can we know dll contains images or not? You can see this link Load JPEG images from DLL with LoadResource in Managed C++[^] , here also loading image from dll, the same way i tried but it is failed. any reasons?
modified on Saturday, April 4, 2009 9:06 AM
-
Ya am loading dll using Loadlibrary, but FindResource is failing. How can we know dll contains images or not? You can see this link Load JPEG images from DLL with LoadResource in Managed C++[^] , here also loading image from dll, the same way i tried but it is failed. any reasons?
modified on Saturday, April 4, 2009 9:06 AM
kiranin wrote:
Ya am loading dll using Loadlibrary, but FindResource is failing. How can we know dll contains images or not?
Open it in Visual Studio - that should show you if it has resources, including images.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p