1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| #include <windows.h>
#ifndef _GDIPLUS_H
#define _GDIPLUS_H
struct IDirectDrawSurface7;
typedef signed short INT16;
typedef unsigned short UINT16;
#include <pshpack8.h> // set structure packing to 8
namespace DllExports {
#include "GdiplusMem.h"
};
#include "GdiplusBase.h"
#include "GdiplusEnums.h"
#include "GdiplusTypes.h"
#include "GdiplusInit.h"
#include "GdiplusPixelFormats.h"
#include "GdiplusColor.h"
#include "GdiplusMetaHeader.h"
#include "GdiplusImaging.h"
#include "GdiplusGpStubs.h"
#include "GdiplusHeaders.h"
#endif // !_GDIPLUS_HPP
#define long_proc typedef long (__stdcall *zProc)
typedef struct {
float m[5][5];
} ColorMatrix;
typedef struct {
PROPID id; // ID of this property
ULONG length; // Length of the property value, in bytes
WORD type; // Type of Image property
VOID* value; // property value
WORD dummy; // add an extra WORD to be DWORD aligned (for a total of 16 bytes)
} ItemProperty;
typedef struct {
float left;
float top;
float right;
float bottom;
} RECTF;
static HMODULE gdiplib() {
static HMODULE hGdip;
if (hGdip == 0) { hGdip = LoadLibrary(L"GDIPLUS"); }
return hGdip;
}
static long Load_GDIPLUS() {
long nRet = 0;
if (gdiplib()) { nRet = -1; }
return nRet;
}
static long GdipGetImageEncodersSize(OUT long &num, OUT unsigned int &size) {
long nRet = -1; // Error
HMODULE hModule = gdiplib();
if (hModule) {
long_proc (long*, unsigned int*);
static zProc hProc;
if (hProc == 0) { hProc = (zProc) GetProcAddress(hModule, "GdipGetImageEncodersSize"); }
if (hProc) { nRet = hProc(&num, &size); }
}
return nRet;
}
static long GdipGetImageEncoders(IN long numEncoders, IN unsigned int size, ImageCodecInfo *encoders) {
long nRet = -1; // Error
HMODULE hModule = gdiplib();
if (hModule) {
long_proc (long, unsigned int, ImageCodecInfo*);
static zProc hProc;
if (hProc == 0) { hProc = (zProc) GetProcAddress(hModule, "GdipGetImageEncoders"); }
if (hProc) { nRet = hProc(numEncoders, size, encoders); }
}
return nRet;
}
static long GdipCreateBitmapFromHICON(IN HICON hIcon, OUT LONG_PTR &lpImg) {
long nRet = -1; // Error
lpImg = 0;
HMODULE hModule = gdiplib();
if (hModule) {
long_proc (HICON, LONG_PTR*);
static zProc hProc;
if (hProc == 0) { hProc = (zProc) GetProcAddress(hModule, "GdipCreateBitmapFromHICON"); }
if (hProc) { nRet = hProc(hIcon, &lpImg); }
}
return nRet;
}
static long GdipSaveImageToFile(IN LONG_PTR img, IN WCHAR* szFilename, IN GUID *clsidEncoder, IN EncoderParameters *params) {
long nRet = -1; // Error
HMODULE hModule = gdiplib();
if (hModule) {
long_proc (LONG_PTR, WCHAR*, GUID*, EncoderParameters*);
static zProc hProc;
if (hProc == 0) { hProc = (zProc) GetProcAddress(hModule, "GdipSaveImageToFile"); }
if (hProc) { nRet = hProc(img, szFilename, clsidEncoder, params); }
}
return nRet;
}
static long GdipDisposeImage(IN LONG_PTR lpImg) {
long nRet = -1; // Error
HMODULE hModule = gdiplib();
if (hModule) {
long_proc (LONG_PTR);
static zProc hProc;
if (hProc == 0) { hProc = (zProc) GetProcAddress(hModule, "GdipDisposeImage"); }
if (hProc) { nRet = hProc(lpImg); }
}
return nRet;
}
static long GdiplusStart (OUT LONG_PTR &hGDIplus, GdiplusStartupInput &inputbuf, IN LONG_PTR outputbuf) {
long nRet = -1; // Error
HMODULE hModule = gdiplib();
if (hModule) {
long_proc (LONG_PTR*, GdiplusStartupInput*, LONG_PTR);
zProc hPROC = (zProc) GetProcAddress(hModule, "GdiplusStartup");
if (hPROC) {
nRet = hPROC(&hGDIplus, &inputbuf, outputbuf);
}
}
return nRet;
}
static long GdiplusShutdown (IN LONG_PTR hGDIplus) {
long nRet = -1; // Error
HMODULE hModule = gdiplib();
if (hModule) {
long_proc (LONG_PTR);
static zProc hProc;
if (hProc == 0) { hProc = (zProc) GetProcAddress(hModule, "GdiplusShutdown"); }
if (hProc) { nRet = hProc(hGDIplus); }
}
return nRet;
}
#pragma warning(disable: 6385) // disable reading invalid data from 'pImageCodecInfo'
static long GetEncoderClsid(IN WCHAR* format, OUT GUID& ClassID) {
long nRet = -1; // Failure
long num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GdipGetImageEncodersSize(num, size);
if (size) {
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo) {
if (GdipGetImageEncoders(num, size, pImageCodecInfo) == Ok) {
for (long K = 0; K < num; ++K) {
if (wcscmp(pImageCodecInfo[K].MimeType, format) == 0 ) {
ClassID = pImageCodecInfo[K].Clsid;
nRet = K; // Success
break;
}
}
}
free(pImageCodecInfo);
}
}
return nRet; // Failure
}
static BOOL SaveIconToFile(HICON hIcon, IN WCHAR* destFile, int size) {
BOOL bRet = FALSE;
LONG_PTR img = 0;
if (GdipCreateBitmapFromHICON(hIcon, img) == Ok) {
GUID encoder;
if (GetEncoderClsid(L"image/png", encoder) == Ok) {
if (GdipSaveImageToFile(img, destFile, &encoder, NULL) == Ok) {
bRet = TRUE;
}
}
GdipDisposeImage(img);
}
return bRet;
}
__declspec(dllexport) BOOL ExtractAndSaveIcon(IN WCHAR* sourceFile, int iconID, IN WCHAR* destFile, int size) {
HMODULE hModule = LoadLibraryEx(sourceFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
if (hModule == NULL) {
//printf("Failed to load source file: %ls\n", sourceFile);
return FALSE;
}
HRSRC hRes = FindResource(hModule, MAKEINTRESOURCE(iconID), RT_GROUP_ICON);
if (hRes == NULL) {
//printf("Failed to find icon resource\n");
FreeLibrary(hModule);
return FALSE;
}
HGLOBAL hMem = LoadResource(hModule, hRes);
if (hMem == NULL) {
//printf("Failed to load icon resource\n");
FreeLibrary(hModule);
return FALSE;
}
LPVOID pRes = LockResource(hMem);
if (pRes == NULL) {
//printf("Failed to lock icon resource\n");
FreeLibrary(hModule);
return FALSE;
}
HICON hIcon = CreateIconFromResourceEx(
(PBYTE)pRes,
SizeofResource(hModule, hRes),
TRUE,
0x00030000,
size, size,
LR_DEFAULTCOLOR
);
if (hIcon == NULL) {
//printf("Failed to create icon from resource\n");
FreeLibrary(hModule);
return FALSE;
}
BOOL result = SaveIconToFile(hIcon, destFile, size);
DestroyIcon(hIcon);
FreeLibrary(hModule);
return result;
}
static LONG_PTR gdiphandle(IN LONG_PTR hGDIplus, IN long RW) {
static LONG_PTR wasHandle;
if (RW) { wasHandle = hGDIplus; }
return wasHandle;
}
static LONG_PTR zGdipStart () {
// Load the GDI+ Dll
GdiplusStartupInput GpInput;
GpInput.GdiplusVersion = 1;
LONG_PTR hGDIplus;
hGDIplus = gdiphandle(0, 0);
if (hGDIplus == 0) {
if (GdiplusStart(hGDIplus, GpInput, NULL) == 0) {
gdiphandle(hGDIplus, 1);
}
}
return gdiphandle(0, 0);
}
// GDIPLUS unload (unload the GDIPLUS.DLL)
static void zGdipEnd (IN LONG_PTR hGDIplus) {
if (hGDIplus) { GdiplusShutdown(hGDIplus); }
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD nReason, LPVOID lpReserved) {
static LONG_PTR hGDIplus;
BOOL bRet = TRUE;
if (nReason == DLL_PROCESS_ATTACH) {
bRet = BOOL(Load_GDIPLUS());
hGDIplus = zGdipStart();
} else if (nReason == DLL_PROCESS_DETACH) {
hGDIplus = gdiphandle(0, 0);
zGdipEnd(hGDIplus);
}
return bRet;
} |
Partager