原理
通过调用windows API(PrivateExtractIcons)来获取HICON,再使用go调用C++编译好的dll来获取exe的图标到指定的位置。
C++代码实现
这一环节对于没接触过C/C++的人来说,可能并不是很友好,故在此献上我编译好的dll。
百度网盘
提取码: 784u
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <malloc.h>
typedef struct
{
WORD idReserved; // must be 0
WORD idType; // 1 = ICON, 2 = CURSOR
WORD idCount; // number of images (and ICONDIRs)
// ICONDIR [1...n]
// ICONIMAGE [1...n]
} ICONHEADER;
//
// An array of ICONDIRs immediately follow the ICONHEADER
//
typedef struct
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes; // for cursors, this field = wXHotSpot
WORD wBitCount; // for cursors, this field = wYHotSpot
DWORD dwBytesInRes;
DWORD dwImageOffset; // file-offset to the start of ICONIMAGE
} ICONDIR;
//
// After the ICONDIRs follow the ICONIMAGE structures -
// consisting of a BITMAPINFOHEADER, (optional) RGBQUAD array, then
// the color and mask bitmap bits (all packed together
//
typedef struct
{
BITMAPINFOHEADER biHeader; // header for color bitmap (no mask header)
//RGBQUAD rgbColors[1...n];
//BYTE bXOR[1]; // DIB bits for color bitmap
//BYTE bAND[1]; // DIB bits for mask bitmap
} ICONIMAGE;
//
// Write the ICO header to disk
//
static UINT WriteIconHeader(HANDLE hFile, int nImages)
{
ICONHEADER iconheader;
DWORD nWritten;
// Setup the icon header
iconheader.idReserved = 0; // Must be 0
iconheader.idType = 1; // Type 1 = ICON (type 2 = CURSOR)
iconheader.idCount = nImages; // number of ICONDIRs
// Write the header to disk
WriteFile(hFile, &iconheader, sizeof(iconheader), &nWritten, 0);
// following ICONHEADER is a series of ICONDIR structures (idCount of them, in fact)
return nWritten;
}
//
// Return the number of BYTES the bitmap will take ON DISK
//
static UINT NumBitmapBytes(BITMAP* pBitmap)
{
int nWidthBytes = pBitmap->bmWidthBytes;
// bitmap scanlines MUST be a multiple of 4 bytes when stored
// inside a bitmap resource, so round up if necessary
if (nWidthBytes & 3)
nWidthBytes = (nWidthBytes + 4) & ~3;
return nWidthBytes * pBitmap->bmHeight;
}
//
// Return number of bytes written
//
static UINT WriteIconImageHeader(HANDLE hFile, BITMAP* pbmpColor, BITMAP* pbmpMask)
{
BITMAPINFOHEADER biHeader;
DWORD nWritten;
UINT nImageBytes;
// calculate how much space the COLOR and MASK bitmaps take
nImageBytes = NumBitmapBytes(pbmpColor) + NumBitmapBytes(pbmpMask);
// write the ICONIMAGE to disk (first the BITMAPINFOHEADER)
ZeroMemory(&biHeader, sizeof(biHeader));
// Fill in only those fields that are necessary
biHeader.biSize = sizeof(biHeader);
biHeader.biWidth = pbmpColor->bmWidth;
biHeader.biHeight = pbmpColor->bmHeight * 2; // height of color+mono
biHeader.biPlanes = pbmpColor->bmPlanes;
biHeader.biBitCount = pbmpColor->bmBitsPixel;
biHeader.biSizeImage = nImageBytes;
// write the BITMAPINFOHEADER
WriteFile(hFile, &biHeader, sizeof(biHeader), &nWritten, 0);
// write the RGBQUAD color table (for 16 and 256 colour icons)
if (pbmpColor->bmBitsPixel == 2 || pbmpColor->bmBitsPixel == 8)
{
}
return nWritten;
}
//
// Wrapper around GetIconInfo and GetObject(BITMAP)
//
static BOOL GetIconBitmapInfo(HICON hIcon, ICONINFO* pIconInfo, BITMAP* pbmpColor, BITMAP* pbmpMask)
{
if (!GetIconInfo(hIcon, pIconInfo))
return FALSE;
if (!GetObject(pIconInfo->hbmColor, sizeof(BITMAP), pbmpColor))
return FALSE;
if (!GetObject(pIconInfo->hbmMask, sizeof(BITMAP), pbmpMask))
return FALSE;
return TRUE;
}
//
// Write one icon directory entry - specify the index of the image
//
static UINT WriteIconDirectoryEntry(HANDLE hFile, int nIdx, HICON hIcon, UINT nImageOffset)
{
ICONINFO iconInfo;
ICONDIR iconDir;
BITMAP bmpColor;
BITMAP bmpMask;
DWORD nWritten;
UINT nColorCount;
UINT nImageBytes;
GetIconBitmapInfo(hIcon, &iconInfo, &bmpColor, &bmpMask);
nImageBytes = NumBitmapBytes(&bmpColor) + NumBitmapBytes(&bmpMask);
if (bmpColor.bmBitsPixel >= 8)
nColorCount = 0;
else
nColorCount = 1 << (bmpColor.bmBitsPixel * bmpColor.bmPlanes);
// Create the ICONDIR structure
iconDir.bWidth = (BYTE)bmpColor.bmWidth;
iconDir.bHeight = (BYTE)bmpColor.bmHeight;
iconDir.bColorCount = nColorCount;
iconDir.bReserved = 0;
iconDir.wPlanes = bmpColor.bmPlanes;
iconDir.wBitCount = bmpColor.bmBitsPixel;
iconDir.dwBytesInRes = sizeof(BITMAPINFOHEADER) + nImageBytes;
iconDir.dwImageOffset = nImageOffset;
// Write to disk
WriteFile(hFile, &iconDir, sizeof(iconDir), &nWritten, 0);
// Free resources
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
return nWritten;
}
static UINT WriteIconData(HANDLE hFile, HBITMAP hBitmap)
{
BITMAP bmp;
int i;
BYTE* pIconData;
UINT nBitmapBytes;
DWORD nWritten;
GetObject(hBitmap, sizeof(BITMAP), &bmp);
nBitmapBytes = NumBitmapBytes(&bmp);
pIconData = (BYTE*)malloc(nBitmapBytes);
GetBitmapBits(hBitmap, nBitmapBytes, pIconData);
// bitmaps are stored inverted (vertically) when on disk..
// so write out each line in turn, starting at the bottom + working
// towards the top of the bitmap. Also, the bitmaps are stored in packed
// in memory - scanlines are NOT 32bit aligned, just 1-after-the-other
for (i = bmp.bmHeight - 1; i >= 0; i--)
{
// Write the bitmap scanline
WriteFile(
hFile,
pIconData + (i * bmp.bmWidthBytes), // calculate offset to the line
bmp.bmWidthBytes, // 1 line of BYTES
&nWritten,
0);
// extend to a 32bit boundary (in the file) if necessary
if (bmp.bmWidthBytes & 3)
{
DWORD padding = 0;
WriteFile(hFile, &padding, 4 - bmp.bmWidthBytes, &nWritten, 0);
}
}
free(pIconData);
return nBitmapBytes;
}
//
// Create a .ICO file, using the specified array of HICON images
//
BOOL SaveIcon3(TCHAR* szIconFile, HICON hIcon[], int nNumIcons)
{
HANDLE hFile;
int i;
int* pImageOffset;
if (hIcon == 0 || nNumIcons < 1)
return FALSE;
// Save icon to disk:
hFile = CreateFile(szIconFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
//
// Write the iconheader first of all
//
WriteIconHeader(hFile, nNumIcons);
//
// Leave space for the IconDir entries
//
SetFilePointer(hFile, sizeof(ICONDIR) * nNumIcons, 0, FILE_CURRENT);
pImageOffset = (int*)malloc(nNumIcons * sizeof(int));
//
// Now write the actual icon images!
//
for (i = 0; i < nNumIcons; i++)
{
ICONINFO iconInfo;
BITMAP bmpColor, bmpMask;
GetIconBitmapInfo(hIcon[i], &iconInfo, &bmpColor, &bmpMask);
// record the file-offset of the icon image for when we write the icon directories
pImageOffset[i] = SetFilePointer(hFile, 0, 0, FILE_CURRENT);
// bitmapinfoheader + colortable
WriteIconImageHeader(hFile, &bmpColor, &bmpMask);
// color and mask bitmaps
WriteIconData(hFile, iconInfo.hbmColor);
WriteIconData(hFile, iconInfo.hbmMask);
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
}
//
// Lastly, skip back and write the icon directories.
//
SetFilePointer(hFile, sizeof(ICONHEADER), 0, FILE_BEGIN);
for (i = 0; i < nNumIcons; i++)
{
WriteIconDirectoryEntry(hFile, i, hIcon[i], pImageOffset[i]);
}
free(pImageOffset);
// finished!
CloseHandle(hFile);
return TRUE;
}
int saveIcon(TCHAR* filename, TCHAR* iconFile) {
/*HICON hIconLarge;
HICON hIconSmall;
BOOL ret;
if (ExtractIconEx(filename, 0, &hIconLarge, &hIconSmall, 1) == 0) {
return 1;
}*/
HICON* hIconsDefault;
hIconsDefault = new HICON();
UINT* idsDefault;
idsDefault = new UINT();
int nIcons = PrivateExtractIcons(filename,
0, 128, 128, hIconsDefault, idsDefault, 1, 0);
if (nIcons <= 0) {
return -1;
}
BOOL ret;
ret = SaveIcon3(iconFile, hIconsDefault, 1);
if (ret) {
return 0;
}
return -1;
}
wchar_t* CharToWchar1(const char* c)
{
wchar_t* pwszUnicode;
int iSize = MultiByteToWideChar(CP_UTF8, 0, c, -1, NULL, 0); //iSize =wcslen(pwsUnicode)+1=6
pwszUnicode = (wchar_t*)malloc(iSize * sizeof(wchar_t)); //不需要 pwszUnicode = (wchar_t *)malloc((iSize+1)*sizeof(wchar_t))
MultiByteToWideChar(CP_UTF8, 0, c, -1, pwszUnicode, iSize);
return pwszUnicode;
}
void HIconToIco(const char* pszPath, const char* path) {
saveIcon(CharToWchar1(pszPath), CharToWchar1(path));
}
网上看到还有一种方法,但是弄出来的icon会失真,应该是saveIcon中有些什么不同,没具体深究。
#include "stdafx.h"
#include <windows.h>
#include <olectl.h>
#pragma comment(lib, "oleaut32.lib")
HRESULT SaveIcon(HICON hIcon, const wchar_t* path) {
// Create the IPicture intrface
PICTDESC desc = { sizeof(PICTDESC) };
desc.picType = PICTYPE_ICON;
desc.icon.hicon = hIcon;
IPicture* pPicture = 0;
HRESULT hr = OleCreatePictureIndirect(&desc, IID_IPicture, FALSE, (void**)&pPicture);
if (FAILED(hr)) return hr;
// Create a stream and save the image
IStream* pStream = 0;
CreateStreamOnHGlobal(0, TRUE, &pStream);
LONG cbSize = 0;
hr = pPicture->SaveAsFile(pStream, TRUE, &cbSize);
// Write the stream content to the file
if (!FAILED(hr)) {
HGLOBAL hBuf = 0;
GetHGlobalFromStream(pStream, &hBuf);
void* buffer = GlobalLock(hBuf);
HANDLE hFile = CreateFile(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (!hFile) hr = HRESULT_FROM_WIN32(GetLastError());
else {
DWORD written = 0;
WriteFile(hFile, buffer, cbSize, &written, 0);
CloseHandle(hFile);
}
GlobalUnlock(buffer);
}
// Cleanup
pStream->Release();
pPicture->Release();
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
HICON hIcon = (HICON)LoadImage(0, L"c:\\windows\\system32\\perfcentercpl.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if (!hIcon) return GetLastError();
HRESULT hr = SaveIcon(hIcon, L"c:\\temp\\test.ico");
return hr;
}
二、使用go调用dll
这边使用的是syscall.LazyProc,go还有其他的调用dll的方式以及其优劣就不在此篇赘述了。
var hIconToIco *syscall.LazyProc
func getHIconToIcoProc() (*syscall.LazyProc, error) {
hIconDll := filepath.Join(`D:\hicon`, "config", "HICON.dll")
if !common.IsFileExist(hIconDll) {
return nil, errors.New("no dll file")
}
if hIconToIco == nil {
hIconToIco = syscall.NewLazyDLL(hIconDll).NewProc(`HIconToIco`)
}
return hIconToIco, nil
}
func extractIconToDestPath(exePath string) (destIconPath string, err error){
if !common.IsFileExist(exePath) {
return "", errors.New("no exe path")
}
defer func() {
if err1 := recover(); err1 != nil {
common.Log.Error(err1)
err = fmt.Errorf("%v", err1)
}
}()
p, err := syscall.BytePtrFromString(exePath)
if err != nil {
return "", err
}
destIconPath = `D:\hicon\1.ico`
//destIconPath = getDestPath(exePath)
if dir := filepath.Dir(destIconPath); !common.IsDirExist(dir) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return "", err
}
}
d, err := syscall.BytePtrFromString(destIconPath)
if err != nil {
return "", err
}
proc, err := getHIconToIcoProc()
if err != nil {
return "", err
}
proc.Call(uintptr(unsafe.Pointer(p)), uintptr(unsafe.Pointer(d)))
return
}
2020.1.3更新
最新方案是使用g++编译c++文件为静态库(.a)文件,很惭愧,对于C++编译这块的知识并不是很到位,之前也尝试过,但是没有成功。现找到了解决方案,原来是编译的时候,缺少了库的引用。
使用g++编译cpp文件
g++ -o HICON.a -c HICON.cpp -lgdi32 -DUNICODE
文件目录结构
一共三个文件,HICON.h,HICON.a,main.go,文件目录结构如下图:
下面再贴一下这三个文件
main.go
// g++ -o HICON.a -c HICON.cpp -lgdi32 -DUNICODE
package main
/*
#cgo CFLAGS: -I "./include"
#cgo LDFLAGS: -L "./lib" -lHICON -lgdi32 -lstdc++
#include "HICON.h"
*/
import "C"
import "fmt"
func main() {
res := C.HIconToIco(C.CString(`C:\Program Files\Honglian\BootMagix\BootMagix.exe`), C.CString(`E:\中文\test.ico`))
fmt.Println(res)
}
HICON.h
#pragma once
#ifndef PCH_H
#define PCH_H
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <malloc.h>
#ifdef __cplusplus
extern "C" {
#endif
extern int HIconToIco(const char* pszPath, const char* path);
#ifdef __cplusplus
}
#endif
#endif //PCH_H
HICON.cpp
#include "HICON.h"
//
// ICONS (.ICO type 1) are structured like this:
//
// ICONHEADER (just 1)
// ICONDIR [1...n] (an array, 1 for each image)
// [BITMAPINFOHEADER+COLOR_BITS+MASK_BITS] [1...n] (1 after the other, for each image)
//
// CURSORS (.ICO type 2) are identical in structure, but use
// two monochrome bitmaps (real XOR and AND masks, this time).
//
typedef struct
{
WORD idReserved; // must be 0
WORD idType; // 1 = ICON, 2 = CURSOR
WORD idCount; // number of images (and ICONDIRs)
// ICONDIR [1...n]
// ICONIMAGE [1...n]
} ICONHEADER;
//
// An array of ICONDIRs immediately follow the ICONHEADER
//
typedef struct
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes; // for cursors, this field = wXHotSpot
WORD wBitCount; // for cursors, this field = wYHotSpot
DWORD dwBytesInRes;
DWORD dwImageOffset; // file-offset to the start of ICONIMAGE
} ICONDIR;
//
// After the ICONDIRs follow the ICONIMAGE structures -
// consisting of a BITMAPINFOHEADER, (optional) RGBQUAD array, then
// the color and mask bitmap bits (all packed together
//
typedef struct
{
BITMAPINFOHEADER biHeader; // header for color bitmap (no mask header)
//RGBQUAD rgbColors[1...n];
//BYTE bXOR[1]; // DIB bits for color bitmap
//BYTE bAND[1]; // DIB bits for mask bitmap
} ICONIMAGE;
//
// Write the ICO header to disk
//
static UINT WriteIconHeader(HANDLE hFile, int nImages)
{
ICONHEADER iconheader;
DWORD nWritten;
// Setup the icon header
iconheader.idReserved = 0; // Must be 0
iconheader.idType = 1; // Type 1 = ICON (type 2 = CURSOR)
iconheader.idCount = nImages; // number of ICONDIRs
// Write the header to disk
WriteFile(hFile, &iconheader, sizeof(iconheader), &nWritten, 0);
// following ICONHEADER is a series of ICONDIR structures (idCount of them, in fact)
return nWritten;
}
//
// Return the number of BYTES the bitmap will take ON DISK
//
static UINT NumBitmapBytes(BITMAP* pBitmap)
{
int nWidthBytes = pBitmap->bmWidthBytes;
// bitmap scanlines MUST be a multiple of 4 bytes when stored
// inside a bitmap resource, so round up if necessary
if (nWidthBytes & 3)
nWidthBytes = (nWidthBytes + 4) & ~3;
return nWidthBytes * pBitmap->bmHeight;
}
//
// Return number of bytes written
//
static UINT WriteIconImageHeader(HANDLE hFile, BITMAP* pbmpColor, BITMAP* pbmpMask)
{
BITMAPINFOHEADER biHeader;
DWORD nWritten;
UINT nImageBytes;
// calculate how much space the COLOR and MASK bitmaps take
nImageBytes = NumBitmapBytes(pbmpColor) + NumBitmapBytes(pbmpMask);
// write the ICONIMAGE to disk (first the BITMAPINFOHEADER)
ZeroMemory(&biHeader, sizeof(biHeader));
// Fill in only those fields that are necessary
biHeader.biSize = sizeof(biHeader);
biHeader.biWidth = pbmpColor->bmWidth;
biHeader.biHeight = pbmpColor->bmHeight * 2; // height of color+mono
biHeader.biPlanes = pbmpColor->bmPlanes;
biHeader.biBitCount = pbmpColor->bmBitsPixel;
biHeader.biSizeImage = nImageBytes;
// write the BITMAPINFOHEADER
WriteFile(hFile, &biHeader, sizeof(biHeader), &nWritten, 0);
// write the RGBQUAD color table (for 16 and 256 colour icons)
if (pbmpColor->bmBitsPixel == 2 || pbmpColor->bmBitsPixel == 8)
{
}
return nWritten;
}
//
// Wrapper around GetIconInfo and GetObject(BITMAP)
//
static BOOL GetIconBitmapInfo(HICON hIcon, ICONINFO* pIconInfo, BITMAP* pbmpColor, BITMAP* pbmpMask)
{
if (!GetIconInfo(hIcon, pIconInfo))
return FALSE;
if (!GetObject(pIconInfo->hbmColor, sizeof(BITMAP), pbmpColor))
return FALSE;
if (!GetObject(pIconInfo->hbmMask, sizeof(BITMAP), pbmpMask))
return FALSE;
return TRUE;
}
//
// Write one icon directory entry - specify the index of the image
//
static UINT WriteIconDirectoryEntry(HANDLE hFile, int nIdx, HICON hIcon, UINT nImageOffset)
{
ICONINFO iconInfo;
ICONDIR iconDir;
BITMAP bmpColor;
BITMAP bmpMask;
DWORD nWritten;
UINT nColorCount;
UINT nImageBytes;
GetIconBitmapInfo(hIcon, &iconInfo, &bmpColor, &bmpMask);
nImageBytes = NumBitmapBytes(&bmpColor) + NumBitmapBytes(&bmpMask);
if (bmpColor.bmBitsPixel >= 8)
nColorCount = 0;
else
nColorCount = 1 << (bmpColor.bmBitsPixel * bmpColor.bmPlanes);
// Create the ICONDIR structure
iconDir.bWidth = (BYTE)bmpColor.bmWidth;
iconDir.bHeight = (BYTE)bmpColor.bmHeight;
iconDir.bColorCount = nColorCount;
iconDir.bReserved = 0;
iconDir.wPlanes = bmpColor.bmPlanes;
iconDir.wBitCount = bmpColor.bmBitsPixel;
iconDir.dwBytesInRes = sizeof(BITMAPINFOHEADER) + nImageBytes;
iconDir.dwImageOffset = nImageOffset;
// Write to disk
WriteFile(hFile, &iconDir, sizeof(iconDir), &nWritten, 0);
// Free resources
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
return nWritten;
}
static UINT WriteIconData(HANDLE hFile, HBITMAP hBitmap)
{
BITMAP bmp;
int i;
BYTE* pIconData;
UINT nBitmapBytes;
DWORD nWritten;
GetObject(hBitmap, sizeof(BITMAP), &bmp);
nBitmapBytes = NumBitmapBytes(&bmp);
pIconData = (BYTE*)malloc(nBitmapBytes);
GetBitmapBits(hBitmap, nBitmapBytes, pIconData);
// bitmaps are stored inverted (vertically) when on disk..
// so write out each line in turn, starting at the bottom + working
// towards the top of the bitmap. Also, the bitmaps are stored in packed
// in memory - scanlines are NOT 32bit aligned, just 1-after-the-other
for (i = bmp.bmHeight - 1; i >= 0; i--)
{
// Write the bitmap scanline
WriteFile(
hFile,
pIconData + (i * bmp.bmWidthBytes), // calculate offset to the line
bmp.bmWidthBytes, // 1 line of BYTES
&nWritten,
0);
// extend to a 32bit boundary (in the file) if necessary
if (bmp.bmWidthBytes & 3)
{
DWORD padding = 0;
WriteFile(hFile, &padding, 4 - bmp.bmWidthBytes, &nWritten, 0);
}
}
free(pIconData);
return nBitmapBytes;
}
//
// Create a .ICO file, using the specified array of HICON images
//
BOOL SaveIcon3(TCHAR* szIconFile, HICON hIcon[], int nNumIcons)
{
HANDLE hFile;
int i;
int* pImageOffset;
if (hIcon == 0 || nNumIcons < 1)
return FALSE;
// Save icon to disk:
hFile = CreateFile(szIconFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
//
// Write the iconheader first of all
//
WriteIconHeader(hFile, nNumIcons);
//
// Leave space for the IconDir entries
//
SetFilePointer(hFile, sizeof(ICONDIR) * nNumIcons, 0, FILE_CURRENT);
pImageOffset = (int*)malloc(nNumIcons * sizeof(int));
//
// Now write the actual icon images!
//
for (i = 0; i < nNumIcons; i++)
{
ICONINFO iconInfo;
BITMAP bmpColor, bmpMask;
GetIconBitmapInfo(hIcon[i], &iconInfo, &bmpColor, &bmpMask);
// record the file-offset of the icon image for when we write the icon directories
pImageOffset[i] = SetFilePointer(hFile, 0, 0, FILE_CURRENT);
// bitmapinfoheader + colortable
WriteIconImageHeader(hFile, &bmpColor, &bmpMask);
// color and mask bitmaps
WriteIconData(hFile, iconInfo.hbmColor);
WriteIconData(hFile, iconInfo.hbmMask);
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
}
//
// Lastly, skip back and write the icon directories.
//
SetFilePointer(hFile, sizeof(ICONHEADER), 0, FILE_BEGIN);
for (i = 0; i < nNumIcons; i++)
{
WriteIconDirectoryEntry(hFile, i, hIcon[i], pImageOffset[i]);
}
free(pImageOffset);
// finished!
CloseHandle(hFile);
return TRUE;
}
int saveIcon(TCHAR* filename, TCHAR* iconFile) {
/*HICON hIconLarge;
HICON hIconSmall;
BOOL ret;
if (ExtractIconEx(filename, 0, &hIconLarge, &hIconSmall, 1) == 0) {
return 1;
}*/
HICON* hIconsDefault;
hIconsDefault = new HICON();
UINT* idsDefault;
idsDefault = new UINT();
int nIcons = PrivateExtractIcons(filename,
0, 128, 128, hIconsDefault, idsDefault, 1, 0);
if (nIcons <= 0) {
return -1;
}
BOOL ret;
ret = SaveIcon3(iconFile, hIconsDefault, 1);
if (ret) {
return 0;
}
return -1;
}
wchar_t* CharToWchar1(const char* c)
{
wchar_t* pwszUnicode;
int iSize = MultiByteToWideChar(CP_UTF8, 0, c, -1, NULL, 0); //iSize =wcslen(pwsUnicode)+1=6
pwszUnicode = (TCHAR*)malloc(iSize * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, c, -1, pwszUnicode, iSize);
return pwszUnicode;
}
int HIconToIco(const char* pszPath, const char* path) {
return saveIcon(CharToWchar1(pszPath), CharToWchar1(path));
}