예제 #1
0
DWORD CIconExtractor::ExtractIcon(HINSTANCE hResource, LPCTSTR id, LPCTSTR TargetICON)
{
	// Find the group icon resource
	HRSRC hRsrc = FindResource(hResource, id, RT_GROUP_ICON);

	if (!hRsrc)
		return GetLastError();

	HGLOBAL hGlobal = nullptr;
	if ((hGlobal = LoadResource(hResource, hRsrc)) == nullptr)
		return GetLastError();

	LPMEMICONDIR lpIcon = nullptr;
	if ((lpIcon = static_cast<LPMEMICONDIR>(LockResource(hGlobal))) == nullptr)
		return GetLastError();

	LPICONRESOURCE lpIR = nullptr;
	if ((lpIR = static_cast<LPICONRESOURCE>(malloc(sizeof(ICONRESOURCE) + ((lpIcon->idCount - 1) * sizeof(ICONIMAGE))))) == nullptr)
		return GetLastError();
	SecureZeroMemory(lpIR, sizeof(ICONRESOURCE) + ((lpIcon->idCount - 1) * sizeof(ICONIMAGE)));

	lpIR->nNumImages = lpIcon->idCount;
	SCOPE_EXIT
	{
		for (UINT i = 0; i < lpIR->nNumImages; ++i)
			free(lpIR->IconImages[i].lpBits);
		free(lpIR);
	};

	// Go through all the icons
	for (UINT i = 0; i < lpIR->nNumImages; ++i)
	{
		// Get the individual icon
		if ((hRsrc = FindResource(hResource, MAKEINTRESOURCE(lpIcon->idEntries[i].nID), RT_ICON)) == nullptr)
			return GetLastError();
		if ((hGlobal = LoadResource(hResource, hRsrc)) == nullptr)
			return GetLastError();

		// Store a copy of the resource locally
		lpIR->IconImages[i].dwNumBytes = SizeofResource(hResource, hRsrc);
		lpIR->IconImages[i].lpBits = static_cast<LPBYTE>(malloc(lpIR->IconImages[i].dwNumBytes));
		if (!lpIR->IconImages[i].lpBits)
			return GetLastError();

		memcpy(lpIR->IconImages[i].lpBits, LockResource(hGlobal), lpIR->IconImages[i].dwNumBytes);

		// Adjust internal pointers
		if (!AdjustIconImagePointers(&(lpIR->IconImages[i])))
			return GetLastError();
	}

	return WriteIconToICOFile(lpIR, TargetICON);
}
예제 #2
0
DWORD CIconExtractor::ExtractIcon(HINSTANCE hResource, LPCTSTR id, LPCTSTR TargetICON)
{
    LPICONRESOURCE      lpIR    = NULL;
    HRSRC               hRsrc   = NULL;
    HGLOBAL             hGlobal = NULL;
    LPMEMICONDIR        lpIcon  = NULL;

    // Find the group icon resource
    hRsrc = FindResource(hResource, id, RT_GROUP_ICON);

    if (hRsrc == NULL)
        return GetLastError();

    if ((hGlobal = LoadResource(hResource, hRsrc)) == NULL)
        return GetLastError();

    if ((lpIcon = (LPMEMICONDIR)LockResource(hGlobal)) == NULL)
        return GetLastError();

    if ((lpIR = (LPICONRESOURCE) malloc(sizeof(ICONRESOURCE) + ((lpIcon->idCount-1) * sizeof(ICONIMAGE)))) == NULL)
        return GetLastError();

    lpIR->nNumImages = lpIcon->idCount;

    // Go through all the icons
    for (UINT i = 0; i < lpIR->nNumImages; ++i)
    {
        // Get the individual icon
        if ((hRsrc = FindResource(hResource, MAKEINTRESOURCE(lpIcon->idEntries[i].nID), RT_ICON )) == NULL)
        {
            free(lpIR);
            return GetLastError();
        }
        if ((hGlobal = LoadResource(hResource, hRsrc )) == NULL)
        {
            free(lpIR);
            return GetLastError();
        }
        // Store a copy of the resource locally
        lpIR->IconImages[i].dwNumBytes = SizeofResource(hResource, hRsrc);
        lpIR->IconImages[i].lpBits =(LPBYTE) malloc(lpIR->IconImages[i].dwNumBytes);
        if (lpIR->IconImages[i].lpBits == NULL)
        {
            free(lpIR);
            return GetLastError();
        }

        memcpy(lpIR->IconImages[i].lpBits, LockResource(hGlobal), lpIR->IconImages[i].dwNumBytes);

        // Adjust internal pointers
        if (!AdjustIconImagePointers(&(lpIR->IconImages[i])))
        {
            free(lpIR);
            return GetLastError();
        }
    }

    DWORD ret = WriteIconToICOFile(lpIR,TargetICON);

    if (ret)
    {
        free(lpIR);
        return ret;
    }

    free(lpIR);

    return NO_ERROR;
}