Windows GDI Context - LoadImage - c++

Using LoadImage() causes segmentation fault. Backtracing the stack I found the following function as called last:
AlpcMaxAllowedMessageLength()
This is the function I call:
status = (HBITMAP) LoadImage(NULL, MAKEINTRESOURCE(STATUS_BMP), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));
with STATUS_BMP loaded as a valid resource bitmap file.
Has someone encountered a similar problem about this function, or just have the solution to the matter?

I believe your issue is the fact that you are specifying that you want the image to be loaded from file by the LR_LOADFROMFILE flag, which means that the second parameter needs to be the string name of the standalone image file (this usually means on disk). You might try removing the LR_LOADFROMFILE flag and see if that fixes the issue.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx
When I have used this function, it has always been from a local file on disk.

MAKEINTRESOURCE and LR_LOADFROMFILE are mutually exclusive. Drop LR_LOADFROMFILE.

Related

Cannot get an image handle from a bitmap within the resources when using "LoadImageA()" and cannot understand why

I am trying to load an image resource using the LoadImageA() function, yet it doesn't work and I don't understand why.
Here's a bit of my code :
bool isRessource = IS_INTRESOURCE(107);
// Load the resource to the HGLOBAL.
HGLOBAL imageResDataHandle = LoadImageA(
NULL,
MAKEINTRESOURCEA(107),
IMAGE_BITMAP,
0,
0,
LR_SHARED
);
HRESULT hr = (imageResDataHandle ? S_OK : E_FAIL);
The image I want to load is a bitmap saved in the resources, and represented as such within resources.h:
#define IDB_BITMAP1 107
When I execute the code, isRessource is equal to true, yet hr is equal to E_FAIL.
Any idea as to why this is happening? I am using Visual Studio 2019, and I made the image using Gimp.
After making the same image with the same format on another application (I used "Krita") and importing it again, the image finally loads with the same code (I only changed the reference to the resource). I guess that all types of bitmaps made from Gimp won't work in Visual Studio (I tried most formats of bitmaps from Gimp).
The first link searched with LoadImage gimp as a keyword is enough to answer this question.
This is some useful information:
The bitmap exported by GIMP has a broken header. Specifically, the
code seems to not write the RGBA masks, which AFAIK are not optional
in a BITMAPV5HEADER. This misaligns and changes the size of the entire
extended header, incidentally making it look like a BITMAPV4HEADER,
which explains why most programs will still open it fine. Without
having done any testing, I'd guess LoadImage() is more picky about the
values in this extended header; returning NULL is how it indicates
failure.
By the way, when you import a bitmap, the system does not remind you that the format of the image is unknown?
Like:
After testing, use LoadImage to load such an image will return NULL, and GetLastError will also return 0.

C++ Error when changing the cursor from a resource file

I'm trying to change the cursor of my mouse with a .cur file in a resource file.
When I'm try my code, I get this error:
Exception raised at 0x77EB7392 (ntdll.dll) in CleanResourceFiles.exe: 0xC0000005: Access Violation while reading location 0x00000066.
Here is the code:
HCURSOR curs = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_CURSOR1), 2, 0, 0, LR_LOADFROMFILE);
SetSystemCursor(curs, 32512);
Note : IDC_CURSOR1 is my cursor and 32512 is the ID of the classic arrow cursor. I also included <Windows.h> and my resource.h.
I'm using Visual Studio Community 2017, with Win10.
I tried other functions, like LoadCursor(). The code above is from "VineMemz".
Finally, when I tried to change my cursor with LoadFromFile() using the path to my .cur file, it works.
When calling LoadImage(), you are specifying the LR_LOADFROMFILE flag, so the lpszName parameter will be interpreted as a pointer to a null-terminated string containing the path to the .cur file to load. But, you are passing in a resource ID number instead of a file path string (I'm assuming IDC_CURSOR1 is 102 (0x66), which would be consistent with the memory address reported in the error message). You need to get rid of the LR_LOADFROMFILE flag when loading an image from resources.
Also, you need to pass in the EXE's actual module handle in the hinst parameter, not NULL (NULL can only be used with loading OEM-defined images).
Also. you should not be using "magic numbers". The 2 on LoadImage() should be replaced with the IMAGE_CURSOR constant, and the 32512 on SetSystemCursor() should be replaced with the OCR_NORMAL constant.
Try this:
HCURSOR curs = (HCURSOR) LoadImage(GetModuleHandle(), MAKEINTRESOURCE(IDC_CURSOR1), IMAGE_CURSOR, 0, 0, 0);
SetSystemCursor(curs, OCR_NORMAL);

Trying to load a Bitmap from Resources returns NULL with an error code of 1813

I've been trying to load a bitmap from my resources in order to set it as an icon in one of my programs control for a good while now, with no success at all.
What I did so far:
First, I went into the code of my .rc file, and added all my bitmaps like this in the corresponding BMP section. Note that all .bmp files are saved as 256-color-bitmaps:
IDB_01d BMP "<path>"
Afterwards, I went into my Resource.h file and inserted a define for every bitmap, looking like this:
#define IDB_01d 2000
After adding my resources, this is what I tried in my code so far:
HBITMAP hbmp = LoadBitmap(m_hInstance, MAKEINTRESOURCE(IDB_01d));
DWORD lastError = GetLastError();
m_weatherIcon.SetIcon(hbmp);
Note that m_hInstance is the HINSTANCE i got from my _tWinMain-method.
However, this is not working. The problem is currently the fact, that hbmp is NULL, due to LoadBitmap returning a NULL value.
I added a call to get the last error, and the error code I'm getting is 1813 all the time. I already did some research, and it seems like the HINSTANCE might be the problem, but I don't see how exactly.
Any advice?
you need declare in .rc file
IDB_01d BITMAP "<path>"
but you using unknown resource type BMP

Loading an image from a resource embedded in a dll

I'm developing a browser plugin (npapi) using FireBreath, and in it I'm trying to embed images which I need to load at runtime.
I've been search, reading and experimenting for two days now but I can't seem to make it work.
This is the code I ended up with:
HMODULE hModule;
LPCWSTR path = L"nptest.dll";
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, path, &hModule);
LPTSTR resourceName = MAKEINTRESOURCE(106);
HBITMAP bitmap = (HBITMAP) LoadImage(hModule, resourceName, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
The loaded bitmap seems to be empty after the execution of this code, and I can't figure out why.
At first I tried to load a PNG image, and later realized that PNGs are not supported, so I just created a BMP file using the resource editor in VS2010.
I know for sure that the BMP (with id 106) is in the compiled DLL because I used PE Explorer to check inside the DLL.
Any ideas as for why it fails to load the image resource?
Thanks a lot.

Windows GDI Context - Function fails and GetLastError() returns 0

Is it possible for a Windows API function to fail, and then, just after it, GetLastError() return 0?
I don't think that showing some code or not really matters: in which cases does it happen?
if ( !(hbmp = (HBITMAP) LoadImage(hThisInstance, MAKEINTRESOURCE(IMG),
IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION)))
printf("Last error: %d\n", GetLastError());
With:
HBITMAP hbmp
HINSTANCE hThisInstance
IMG bitmap resource
I found the problem to reside in my bitmap image 32 bits encoding. LoadImage() fails without setting an error code while trying to load it, though. I definitely think it should be considered as a error handling inaccuracy within LoadImage() itself, notwithstanding any warning about such constraints possibly claimed in online documentation.