Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BMP from dll to file

Posted on 2004-03-24
13
Medium Priority
?
370 Views
Last Modified: 2013-11-20
can someone tell me how i can get a bmp resource from the dll to the drive. This is for a desktop changer with a set of wallpapers included in the dll and the ability to select one from the disk. so all i need is a way to get bmp from dll to disk ex.c:\wallpaper.bmp
0
Comment
Question by:zeurx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 10674576
Open the DLL in Visual Studio as a resource. You can then export the bitmaps in the DLL.
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10675168
1. Use FindResource or FindResourceEx to get a handle to that bitmap resource.
2. Use LoadResource to load it into memory as it is stored in the file.
3. Use LockResource to get a pointer to that memory block.
4. Write the memory block to file. You have two small things to keep in mind:
a.) Write 'B' and 'M' as the two first bytes of the file; these are the Windows Bitmap file signatures but aren't stored in the resource.
b.) The first 4 bytes of the memory block give you the size of the resource. These 4 bytes aren't to be written into the file.
0
 

Author Comment

by:zeurx
ID: 10676187
on step 4, could u explane that somemore
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 6

Expert Comment

by:joghurt
ID: 10676350
a.) If you look into a .BMP file, the first two bytes are 'B' and 'M'. If you look into the bitmap resource, these two bytes are missing. So if you want to create a valid .BMP file, you have to write these two bytes yourself.

b.) If you look into the bitmap resource (to the memory pointed by the value you get from LockResource), the first 4 bytes are the size of the bitmap resource. You don't have to write these 4 bytes to the file, because .BMP files don't have this value. But this size value should be used for how many bytes do you have to write to the file.

char* bmpResource = LockResource (...)

DWORD size = 0;
memcpy (&size, bmpResource, sizeof (DWORD));

WriteToFile ("BM", 2);
WriteToFile (bmpResource + sizeof (DWORD)), size);
0
 

Author Comment

by:zeurx
ID: 10676392
would WriteToFile be the same as WriteFile or do i need to add a include?
0
 
LVL 6

Accepted Solution

by:
joghurt earned 150 total points
ID: 10677229
WriteToFile is a pseudo-command in the above example. Use

DWORD written = 0;
WriteFile (hFile, buf, count, &written, NULL);

instead of WriteToFile (buf, count);
0
 

Author Comment

by:zeurx
ID: 10684590
     // Set File //
      
      TCHAR WinDir[INFO_BUFFER_SIZE];
      GetWindowsDirectory(WinDir,sizeof(WinDir));
      CString Wallpaper; Wallpaper.Format("%s",WinDir);
      CString sHeight; sHeight.Format("%i",height); CString sWidth; sWidth.Format("%i",width);
      Wallpaper += "\\Shadower-"+sWidth+"x"+sHeight+".bmp";
      AfxMessageBox(Wallpaper);
      // Get Resource //
      HMODULE hModule = LoadLibrary("Shadower.dll");
      CString size; size.Format("%i",width);
      LPCTSTR lpFile; lpFile = "SHADOWBG_"; lpFile =+ (char *) (LPCTSTR) size;
      AfxMessageBox(lpFile,MB_OK,NULL);
      HRSRC hRes = FindResource(hModule,lpFile,RT_BITMAP);
      HGLOBAL hResLoad = LoadResource(hModule,hRes);
      LPVOID bmpResource = LockResource(hResLoad);
      // Save to Bmp //
      DWORD Size = 0;
      memcpy (&Size, bmpResource, sizeof (DWORD));
      DWORD written = 0;
      LPSTR Wall = (char *) (LPCTSTR) Wallpaper;
      HANDLE hFile = CreateFile(Wall,
                                                GENERIC_WRITE,
                                                FILE_SHARE_READ,
                                                NULL,
                                                CREATE_ALWAYS,
                                                FILE_ATTRIBUTE_READONLY,
                                                NULL);
      WriteFile(hFile, "BM", 2, &written, NULL);
      WriteFile(hFile, bmpResource, Size, &written, NULL);

      UnlockResource(hResLoad);
      FreeLibrary(hModule);
      // Set Wallpaper //
      Reg.SetRootKey(HKEY_CURRENT_USER);
      Reg.SetKey("Control Panel\\Desktop",0);
      Reg.WriteString("Wallpaper",Wallpaper);
      Reg.WriteString("WallpaperStyle","2");
      SystemParametersInfo(SPI_SETDESKWALLPAPER,0,NULL,SPIF_SENDCHANGE|SPIF_UPDATEINIFILE);

when i run this code i get an error...
this is what i use to get to this function...

#include "..\dll\Wallpaper.h"

            CWallpaper Wallpaper;
            Wallpaper.ExtractBMP(768,1024);

i get an error and when i go to debug it it shoots me the the  .extract part. anyhelp
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10685269
Which line gives you the error? And if you debug step-by-step, what's the result of each line? If anything fails, what's the GetLastError() value after the failure? Etc.
Btw, you should write WriteFile(hFile, bmpResource + sizeof(DWORD), Size, &written, NULL); instead.

And I think you can use LoadLibraryEx function with the LOAD_LIBRARY_AS_DATAFILE flag. It prevents the DLL from running it's startup code.
0
 

Author Comment

by:zeurx
ID: 10686667
NULL,LOAD_LIBRARY_AS_DATAFILE give me an error C2036: 'LPVOID" : unknown size
0
 

Author Comment

by:zeurx
ID: 10686896
ok i found my real prob.
when i go to get the width of what file they want ex. 1024x768 it dosent add SHADOWBG_ in front of the 1024

LPTSTR lpsz = new TCHAR[size.GetLength()+1];
      _tcscpy(lpsz, size);
      LPCTSTR lpFile; lpFile = "SHADOWBG_"; lpFile =+ lpsz;

it will not add just override
0
 

Author Comment

by:zeurx
ID: 10693138
ok i got it to get the LPCTSTR to get right now but when i copy to memory i get a error that kills the program.

LPVOID bmpResource = LockResource(hResLoad);
...
memcpy (&Size, bmpResource, sizeof (DWORD));

and i get the error

Just-In-Time Debugging
---------------------------------------------------------
An exception 'System.NullReferenceException' has occurred in
Shadower.exe.
0
 

Author Comment

by:zeurx
ID: 10693235
ok that all works now... just had to set memcpy's last arr to 0

but i cant open the file,
---------------------------
Paint
---------------------------
A sharing violation occurred while accessing E:\windows\Shadower-1024x768.bmp.
---------------------------
OK  
---------------------------
 is what i get when i try to open it in paint.

      // Save to Bmp //
      DWORD Size = 0;
      memcpy (&Size, bmpResource, 0);
      DWORD written = 0;
      LPSTR Wall = (char *) (LPCTSTR) Wallpaper;
      HANDLE hFile = CreateFile(Wall,
                                                GENERIC_WRITE,// | GENERIC_READ,
                                                 0,//FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                                                NULL,
                                                CREATE_ALWAYS,
                                                FILE_ATTRIBUTE_READONLY,
                                                NULL);
      WriteFile(hFile, "BM", 2, &written, NULL);
      WriteFile(hFile, bmpResource, Size, &written, NULL);

is my code
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10694590
Well, the purpose of using a debugger is not only showing you which line crashes. You can check values of variables, too. In your case "bmpResource" seems to be NULL (as I thought you will take care of error checking). Now if you try to copy anything from NULL, it will crash.

And just think a little about setting memcpy's last parameter to zero. Now you copy zero bytes, e.g., nothing. Now how would "size" get initialized?

Now, for the heaven's sake, please use the "Step" and "Watch" functions of your debugger and check all the returned values beginning from FindResource. If any of the resource functions return NULL, modify your code so that you call GetLastError () after it and check the returned value.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question