File not found error with LoadImage() (Windows)
EDIT: I've added a few more lines of the program to the code snippet below.
I have t开发者_如何转开发he 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)?
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.
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.
Your Image lives in a TEMP Path I believe as the execution is happening...
Environment::GetEnvironmentVariable("TEMP") + "\Button.bmp";
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.
精彩评论