Solved

BMP from dll to file

Posted on 2004-03-24
13
363 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
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 50 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best tools for Backup and restore of Wordpress site 6 57
Doc'in system (example?) BA 7 112
isEverywhere  challenge 19 77
has77  challenge 9 85
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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