1

Windows API "CreateIconFromResourceEx" not working after windows update "Update for Microsoft Windows(KB4517389)" if I uninstall this update "CreateIconFromResourceEx" will work, but there is no any official documentation regarding deprecate of "CreateIconFromResourceEx" API.

int offset = LookupIconIdFromDirectoryEx(bCursorBuff, TRUE, 0, 0, LR_DEFAULTSIZE|LR_SHARED);

if (offset != 0) 
{                                   
    HICON m_hIcon = CreateIconFromResourceEx(bCursorBuff + offset, 0 , TRUE, 0x30000, 0, 0, LR_DEFAULTSIZE|LR_SHARED); 

    if(m_hIcon == NULL)
    {                       
        MessageBox(NULL, _T("Exception :: CreateIconFromResourceEx failde."),_T("message"),MB_OK|MB_SYSTEMMODAL);
    }           
}
Krish
  • 376
  • 3
  • 14
  • Either a) you have been using the function incorrectly and the update finally exposes it, or b) the update broke something again. So please show your code. And no, when a function in Windows API is deprecated, it is not removed. It will pretty much stay around forever. – GSerg Nov 07 '19 at 06:39
  • @GSerg Added my code, If they change API sign they should mention in MSDN but no updates in MSDN regarding this. – Krish Nov 07 '19 at 06:52
  • what GetLastError() returning ? – Santosh Dhanawade Nov 07 '19 at 07:43
  • LookupIconIdFromDirectoryEx() uses invalid flags. If that somehow never caused failure before then you now might be using a new, undocumented flag. – Hans Passant Nov 07 '19 at 08:42
  • @Satarakar GetLastError() throwing 0. – Krish Nov 07 '19 at 08:54
  • @HansPassant LookupIconIdFromDirectoryEx() Is not a problem because it returns offset value of cursor buffer it returns 22 before windows update after windows update also it returns same value 22 so here windows update making problem for CreateIconFromResourceEx. – Krish Nov 07 '19 at 08:59
  • `LookupIconIdFromDirectoryEx` is *a* problem because you are passing flag values it does not support. It may break in future, and it may even be that it is what has broken it now (imagine there is an undocumented flag `LR_FIDDLE_WITH_BYTES` which happens to be numerically equal to `LR_DEFAULTSIZE` which this function was never not supposed to accept). – GSerg Nov 07 '19 at 14:52
  • More likely though is that it's because you are passing 0 as the resource size. The [second parameter](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createiconfromresourceex#parameters) is not documented to accept a 0, and the [sample code](https://learn.microsoft.com/en-us/windows/win32/menurc/using-icons) uses `SizeOfResource`. – GSerg Nov 07 '19 at 14:53
  • @GSerg yes after adding size of resource its working. Thank you. – Krish Nov 08 '19 at 06:35
  • @GSerg But still I have one doubt before the API works without resource buffer size but after the windows update its not. ? – Krish Nov 08 '19 at 06:39

2 Answers2

0
int offset = LookupIconIdFromDirectoryEx(bCursorBuff, TRUE, 0, 0, LR_SHARED);

if (offset != 0) 
{                                   
    HICON m_hIcon = CreateIconFromResourceEx(bCursorBuff + offset, bCursorBuffSize , TRUE, 0x30000, 0, 0, LR_SHARED); 

    if(m_hIcon == NULL)
    {                       
        MessageBox(NULL, _T("Exception :: CreateIconFromResourceEx failde."),_T("message"),MB_OK|MB_SYSTEMMODAL);
    }           
}
Krish
  • 376
  • 3
  • 14
0

LookupIconIdFromDirectoryEx - parses RT_GROUP_ICON resource buffer, selects appropriate icon size and returns resource ID for selected RT_ICON resource.

Having an icon resource ID you can use this method to create HICON from resource:

HICON LoadSpecificIcon( HMODULE hMod, WORD Id ) {
    HRSRC hRsrc = FindResourceW( hMod, MAKEINTRESOURCE( Id ), RT_ICON );
    HGLOBAL hGlobal = LoadResource( hMod, hRsrc );
    BYTE* lpData = (BYTE*)LockResource( hGlobal );
    DWORD dwSize = SizeofResource( hMod, hRsrc );

    HICON hIcon = CreateIconFromResourceEx( lpData, dwSize, TRUE, 0x00030000,
                                            0, 0, LR_DEFAULTCOLOR );
    return hIcon;
}

See https://stackoverflow.com/a/20731449/1795050 for more info.

DJm00n
  • 1,083
  • 5
  • 18