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

0

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.

c++
cursor
loadimage
ntdll
resource-file
asked on Stack Overflow Jun 13, 2018 by Lotcelan • edited Jun 13, 2018 by Remy Lebeau

1 Answer

0

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);
answered on Stack Overflow Jun 13, 2018 by Remy Lebeau • edited Jun 13, 2018 by Remy Lebeau

User contributions licensed under CC BY-SA 3.0