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

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);

Related

I can't get SHGetFileInfo to return an icon location

I'm on Windows 10 Pro and Visual Studio 2013, and I'm using SHGetFileInfoW to get an icon location (path + index) for a file type:
std::wstring wFile { L"a.bas" };
SHFILEINFOW fi {};
DWORD success = ::SHGetFileInfoW(wFile.c_str(),
FILE_ATTRIBUTE_NORMAL,
&fi,
sizeof(fi),
SHGFI_USEFILEATTRIBUTES | SHGFI_ICONLOCATION);
No matter whether wFile refers to an existing file or is just any filename, the call returns 1 indicating success. The iIcon member of fi is set to a number, but szDisplayString is empty. Not just the drive letter is overwritten with \0 (as seemed to happen here) but it is completely filled with \0.
Microsoft recommends using IExtractIcon::GetIconLocation as an alternative, but I need to get the icon for files which are not on a local filesystem, so I can't get an IShellInfo object which would get me this interface pointer.
Getting an icon handle works, on the other hand. Is this function just buggy or am I doing something wrong? Is there a workaround?
Icons can be dynamically generated and might not expose the path to its images. Icon handlers communicate this to the shell by setting the GIL_NOTFILENAME flag in their IExtractIcon::GetIconLocation implementation. If GIL_SIMULATEDOC is set the shell must also typically generate a icon on the fly.
If you call SHGetFileInfo with the SHGFI_SELECTED flag set then then function probably has to generate a new icon no matter which file type you are asking for.
If you are displaying a file list in a ListView/TreeView then you typically use SHGFI_SYSICONINDEX|SHGFI_SHELLICONSIZE|SHGFI_SMALLICON and use the system image list.
Use SHGFI_ICON if you need a HICON.
If SHGFI_ICONLOCATION is specified then SHGetFileInfo uses IExtractIcon:
Retrieve the name of the file that contains the icon representing the file specified by pszPath, as returned by the IExtractIcon::GetIconLocation method of the file's icon handler.

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

How to apply image effects to a JPEG in a memory buffer using LEADTOOLS 19

I'm making a Windows executable using C++, LEADTOOLS19, and VS2015 to read an image from a server, apply an image effect to it using LEADTOOLS, and display it in a browser.
The server provides me the image as an array of chars containing the JPEG encoding of the image (starting with "ÿØÿà") and the length of this buffer. Most of the LEADTOOLS functions read images from files, but I don't want to have to write it to disk just to read it as a bitmap.
The first thing I tried was the StartFeedLoad function:
//pImageData is the buffer of JPEG data, and imageLength is the
//server-provided size of pImageData in bytes
LBuffer buf((L_VOID *)pImageData, imageLength);
LFile imgFile;
LBitmap bitmap;
imgFile.SetBitmap(&imgbitmap);
// Initialize the file-load process
imgmemfile.StartFeedLoad(8, 0,
LOADFILE_ALLOCATE | LOADFILE_STORE, NULL);
imgmemfile.FeedLoad(&buf);
imgmemfile.StopFeedLoad();
With this code, I get this exception when trying to run StartFeedLoad:
Exception thrown at 0x000007F855BC2662 (ltwvcax.dll) in getimage.exe:
0xC0000005: Access violation reading location 0x0000000000000148.
I tried a few different things before calling StartFeedLoad, and tried changing the parameters I was passing it, but got that exception every time.
With that not working, the next method I tried was to save the buffer as an in-memory file using the LEADTOOLS library LMemoryFile class:
LBuffer buf((L_VOID *)pImageData, imageLength);
LMemoryFile imgmemfile;
BITMAPHANDLE pbit;
//The bitmap the image will be loaded into
LBitmap bitmap;
imgmemfile.SetBitmap(&bitmap);
//Load the buffer to the image
ret = imgmemfile.LoadMemory(buf, 0, ORDER_RGBORGRAY, LOADFILE_ALLOCATE | LOADFILE_STORE, NULL);
At this point, LoadMemory returns WRPERR_INVALID_PARAMETERS: One or more invalid parameters were specified. I've tried different bitsPerPixel values, color orders, and with or without adding another NULL parameter as fileInfo but still get the same error.
I feel like I need to do something else to "prep" the bitmap to load, but I don't know it's size or anything else to initialize it.
Thanks!
EDIT 5/9/16: Added "GetInfo" as indicated by Leadtools:
//Load image
LBuffer buf((L_VOID *)pImageData, imageLength);
//LFile imgmemfile;
FILEINFO fileInfo = FILEINFO();
LMemoryFile imgmemfile;
BITMAPHANDLE pbit;
if (LBase::GetLoadedLibraries() & LT_FIL == 0)
return false;
LBitmap bitmap;
imgmemfile.SetBitmap(&bitmap);
ret = imgmemfile.GetInfo(buf, &fileInfo, sizeof(FILEINFO), 0, NULL);
ret = imgmemfile.LoadMemory(buf, 0, ORDER_RGBORGRAY, LOADFILE_ALLOCATE | LOADFILE_STORE, NULL, &fileInfo);
ret = imgmemfile.Save(&buf, FILE_JPEG, 8, 30, NULL);
The code gets past the additional library check, but GetInfo returns -2041, indicating that LTFIL isn't loaded.
You should use LMemoryFile::GetInfo and LMemoryFile::LoadMemory if you have the whole file in memory at the start. If you don't, then FeedLoad is the way to go. There is an example here: https://www.leadtools.com/help/leadtools/v19/main/clib/lfile__startfeedload.html
You can find a full working example in your LEADTOOLS installation folder: C:\LEADTOOLS 19\Examples\ClassLibrary\MSVC\FeedLoad
The functions that LEADTOOLS Support gave me were correct, but I was still ahving the issue because I was linking to both the Unicode and ANSI versions of the Leadtools C++ class library (Ltwvc_x.lib AND Ltwvc_ax.lib). When I removed the Unicode library (from my ANSI project) everything worked fine.

Windows GDI Context - LoadImage

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.

C++ LogFont Embed Into XPS

I am attempting to marry a couple of APIs together to facilitate XPS Printing. As True Type Fonts are sometimes restricted on how they may be used it is suggested that you query the OS (Windows) for the license associated with a font. The proscribed method I've found for doing that looks like this:
HDC hDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
// logfont is a valid instance of LOGFONTW
HGDIOBJ hfont = ::CreateFontIndirect(&logfont);
if (!SelectObject(hDC, hfont))
return;
ULONG privstatus = 0;
LONG ttStatus;
ttStatus = TTGetEmbeddingType(hDC, &privstatus);
At this point ttStatus should be E_NONE if TTGetEmbeddingType succeeded and privstatus should be one of {EMBED_PREVIEWPRINT, EMBED_EDITABLE, EMBED_INSTALLABLE, EMBED_NOEMBEDDING}. I had this example working Friday. Today when I run my executable TTGetEmbeddingType returns 0x0A (E_NOTATRUETYPEFONT) instead of E_NONE. Wat? Am I missing something fundamental about the OS's ability to determine whether a font can be embedded?
The error message text you quoted ("The environment is incorrect") belongs to the ERROR_BAD_ENVIRONMENT system error code, which has a numeric value of 10 (0x0A). However, TTGetEmbeddingType() does not return a system error code. The TTGetEmbeddingType() documentation states:
If successful, returns E_NONE.
This function reads the embedding privileges stored in the font and transfers the privileges to pulPrivStatus.
Otherwise, returns an error code described in Embedding-Function Error Messages.
If you look at the actual definitions in T2embapi.h, a return value of 0x000A is E_NOTATRUETYPEFONT
The specified font is not a TrueType font.