views:

51

answers:

4

EDIT: I've added a few more lines of the program to the code snippet below.

I have the following line of code in a program

BITMAP BMP;
HBITMAP hBMP;

hBMP = (HBITMAP)LoadImage(GetModuleHandle(NULL), "Test.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
if (!hBMP) return 1;

GetObject(hBMP, sizeof(BMP), &BMP);

and when I run the program, it crashes. I used GetLastError(), which returned 2/FILE_NOT_FOUND, but the file ("Test.bmp") is in the folder of the .exe. Can someone point out what's wrong with the line of code (or if the error isn't in this line)?

A: 

you should try using the full path to your application.

Also, main() arguments contain the current application path so you can add it to your string.

Eric
A: 

Your Image lives in a TEMP Path I believe as the execution is happening...

Environment::GetEnvironmentVariable("TEMP") + "\Button.bmp";

Joe Garrett
+1  A: 

Are you running in the debugger perhaps, as started from the IDE? The current working directory might be different than what you think it is.

EDIT: Using Process Explorer, from sysinternals, you can see that the current working directory is the one containing your solution file, if using Visual Studio, not the directory where your binaries are created.

Chris O
+3  A: 

Use GetCurrentDirectory to find out which directory is the default, it might not be where the executable lies. I would suggest using the full path to your file.

Mark Ransom
I tried changing it to "C:\Test.bmp" (and putting a copy of the file there), and GetLastError() returns 0, but the program still crashes when I try to access something like the width of the bitmap.
Keand64
@Keand64: The LoadImage function only returns a handle to the bitmap. To get information out of the handle, use the GetObject function. If you show some more code it would be helpful.
Mark Ransom
@Mark - Yeah, I had a GetObject() function, I just didn't think it was relevant to the question at the time. Now I added it to the code snippet I wrote though.
Keand64