IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

WinDev Discussion :

Extraire un icône d'une dll selon son # de ressource


Sujet :

WinDev

  1. #21
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2012
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 37
    Points : 44
    Points
    44
    Par défaut
    Patrice, je maitrise la notion de pointeur. Il m'est pour l'instant impossible de me positionner dans le fichier pour récupérer l'icône et m'est tout autant impossible d'avoir le pointeur de l'icône en mémoire puisque je n'arrive pas à charger le bon.

    Pour rappel, les informations que j'ai pour l'instant sont soit un indice d'icône (ex : 307) soit ce qui est renvoyé pas EnumResourceNamesW : 140727756193792 (handle du Module dans mon exemple précédent), 3 (type), 2426 (ID du dernier icône), 0 (hParam)
    Es-tu en train de me dire que je dois aller chercher le dernier icône de shell32.dll (dont je ne connais pas la taille d'ailleurs) à l'adresse 140727756193792 + 2426 ? mmmh, pas convaincu.

    Si tu vois ça simple, "petit défi du jour" : aurais-tu quelques min pour faire 10 lignes de code pour m'extraire, de shell32.dll, la flèche grise illustrée ci-dessus avec son numéro de ressources 16818. Moi je n'y arrive toujours pas.

  2. #22
    Membre éprouvé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    507
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 507
    Points : 904
    Points
    904
    Par défaut
    Etant développeur bas niveau, je n'ai plus en tête la syntaxe WinDEV (j'en suis resté à la version WD17).

    Alors voici le code C, pour extraire une icône de shell32.dll

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    // Function to extract an icon resource from shell32.dll
    HICON ExtractIconFromShell32(int iconID) {
        // Load the shell32.dll library
        HMODULE hShell32 = LoadLibrary(TEXT("shell32.dll"));
        if (hShell32 == NULL) {
            printf("Failed to load shell32.dll\n");
            return NULL;
        }
     
        // Find the icon resource
        HRSRC hRes = FindResource(hShell32, MAKEINTRESOURCE(iconID), RT_GROUP_ICON);
        if (hRes == NULL) {
            printf("Failed to find icon resource\n");
            FreeLibrary(hShell32);
            return NULL;
        }
     
        // Load the icon resource
        HGLOBAL hMem = LoadResource(hShell32, hRes);
        if (hMem == NULL) {
            printf("Failed to load icon resource\n");
            FreeLibrary(hShell32);
            return NULL;
        }
     
        // Lock the resource to get a pointer to the icon directory
        LPVOID pRes = LockResource(hMem);
        if (pRes == NULL) {
            printf("Failed to lock icon resource\n");
            FreeLibrary(hShell32);
            return NULL;
        }
     
        // Create an icon from the resource
        HICON hIcon = CreateIconFromResourceEx(
            (PBYTE)pRes,
            SizeofResource(hShell32, hRes),
            TRUE,
            0x00030000,
            0, 0,
            LR_DEFAULTCOLOR
        );
        if (hIcon == NULL) {
            printf("Failed to create icon from resource\n");
        }
     
        // Free the library
        FreeLibrary(hShell32);
     
        return hIcon;
    }
    Pour remplacer la macro MAKEINTRESOURCE
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    // Convert the iconID to a resource string pointer manually
    LPCWSTR lpIconID = (LPCWSTR)((ULONG_PTR)((WORD)(iconID)));

  3. #23
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2012
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 37
    Points : 44
    Points
    44
    Par défaut
    Merci pour tes efforts, Patrice.

    Mon problème est précisément là. Je n'arrive pas à transformer ce code en WLangage et essaye d'éviter de trainer les dépendances de Python ou une dll supplémentaire (que je n'ai jamais fait).

  4. #24
    Membre éprouvé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    507
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 507
    Points : 904
    Points
    904

  5. #25
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2012
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 37
    Points : 44
    Points
    44
    Par défaut
    Je connais ce post et il ne me convient pas : il utilise l'index et pas le # de ressource.

    Entre parenthèses, j'ai fait une procédure de lecture d'icônes bien plus complexe que ça. Il me manque juste de pouvoir lire ces maudits icônes selon le # de ressource, en WinDev.

  6. #26
    Membre éprouvé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    507
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 507
    Points : 904
    Points
    904
    Par défaut
    Selon moi la meilleure solution serait de passer par une DLL écrite en C, pour contourner les limitations du p-code WinDev.
    L'avantage d'utiliser directement la flat API c'est qui'il n'y aucun framework à installer tout étant déjà dans shell32.

    Utilisez-vous des chaines Unicode et un executable 64-bit ?

  7. #27
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2012
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 37
    Points : 44
    Points
    44
    Par défaut
    Oui tout est en unicode et en 64 bits.

  8. #28
    Membre éprouvé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    507
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 507
    Points : 904
    Points
    904
    Par défaut
    Je vais faire un test pour voir la taille que ferait la DLL en mode unicode 64-bit.

  9. #29
    Membre éprouvé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    507
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 507
    Points : 904
    Points
    904
    Par défaut
    En mode UNICODE 64-bit, la taille de la DLL est de 97 Ko sans dépendances externes,
    hormis GDIPLUS qui est utilisé pour enregistrer les icônes au format icon png.

    La fonction exportée de la DLL
    BOOL ExtractAndSaveIcon(IN WCHAR* srcefile, IN int iconID, IN WCHAR* destfile, IN int size)

    Voici le code de la DLL créée avec VS2022 community (pas testé).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
    }

  10. #30
    Expert éminent
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 000
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 000
    Points : 9 384
    Points
    9 384
    Par défaut
    Hello,
    Citation Envoyé par Lapalys Voir le message
    Je fais un logiciel qui utilise des icônes stockés dans les exe et les dll. Je passe l'indice (positif) de l'icône et tout va bien.
    tu ne nous as pas dit quelle taille d'icône tu voulais récupérer car dans le fichier shell32.dll il existe plusieurs variations de taille pour chaque icône.
    Au cas où tu ne trouverais pas ton bonheur , il existe un assemblage dotnet qui permet d'extraire toutes les icônes d'un fichier dll avec toutes les variations, il s'agit de IconExtractor

    Voici des copies d'écran d'une application dotnet qui utilise cet assemblage.
    Nom : IconExtractorSampleApp1.png
Affichages : 42
Taille : 42,9 KoNom : IconExtractorSampleApp2.png
Affichages : 42
Taille : 22,5 Ko

    Pour utiliser l'assemblage dans un projet Windev il faut l'ajouter au projet ainsi que l' assemblage standard dotnet System.Drawing ,

    Comme je n'ai plus windev je vous mets un example de code dotnet qui permet d'extraire les icônes de l'icône 306 dans des fichiers .png :
    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    using System;
    using System.Drawing;
    using TsudaKageyu;
     
    namespace testIconExtractor
    {
        class Program
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("Extraction!");
                // -----------------------------------------------------------------------------
                // Usage of IconExtractor class:
     
                // Construct an IconExtractor object with a file.
     
                IconExtractor ie = new IconExtractor(@"c:\windows\system32\shell32.dll");
     
                // Get the full name of the associated file.
     
                string fileName = ie.FileName;
     
                // Get the count of icons in the associated file.
     
                int iconCount = ie.Count;
     
                // Extract icon 306 and all the variations.
     
                Icon icon0 = ie.GetIcon(306);
                Icon[] splitIcons = IconUtil.Split(icon0);
                int i = 0;
                foreach (var icone in splitIcons) {
                    IconUtil.ToBitmap(icone).Save(
                        @"d:\temp\_icone306_" + i++ + "_" +
                        icone.Width + "x" + icone.Height +
                        ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
     
                Console.Write("Press any key to continue . . . ");
                Console.ReadKey(true);
            }
        }
    }

    En pièce jointe l'assemblage IconExtractor ( normalement en version any CPU) et les fichiers générés par le programme précédent.

    Ami calmant, J.P
    Fichiers attachés Fichiers attachés
    Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

  11. #31
    Membre éprouvé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    507
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 507
    Points : 904
    Points
    904
    Par défaut
    Je viens de faire un essai pour linker Extract.dll avec TClib.lib pour m'affranchir du CRT.
    Et la taille passe de 97 Ko à seulement 6 Ko, c'est sans commentaire...

    Je peux mettre l'intégralité du projet (la solution complete VS2022) sur mon forum privé pour ceux que cela intéresse.

  12. #32
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2012
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 37
    Points : 44
    Points
    44
    Par défaut
    @Jurassic Pork : habituellement je passe la taille en paramètre de l'appel API. Au pire je prend la plus grande et redimensionne. Pas de problème de ce côté. Pour ta proposition d'utiliser IconExtractor, j'ai testé ceci
    IconExtract est un IconExtractor("C:\Windows\system32\shell32.dll")
    pclMonIcône est un System.Drawing.Icon dynamique
    pclMonIcône <- IconExtract.GetIcon(63)
    pclImgIcone est un System.Drawing.Bitmap dynamique
    pclImgIcone <- pclMonIcône.ToBitmap()
    pclImgIcone.Save("C:\temp\icone63.ico")

    ce qui me donne bien l'icône de la corbeille (voir mes précédents messages), selon son indice 63 !
    En prenant son # de ressource, à savoir le 142, et pour ne pas le confondre avec l'icône d'indice 142, je devrais donc utiliser -142

    IconExtract est un IconExtractor("C:\Windows\system32\shell32.dll")
    pclMonIcône est un System.Drawing.Icon dynamique
    pclMonIcône <- IconExtract.GetIcon(-142)
    pclImgIcone est un System.Drawing.Bitmap dynamique
    pclImgIcone <- pclMonIcône.ToBitmap()
    pclImgIcone.Save("C:\temp\icone-142.ico")

    Erreur :
    L'invocation de la méthode <GetIcon(System.Int32)> du type <Icon> a échoué
    Le framework .NET a renvoyé l'erreur suivante :
    L'argument spécifié n'était pas dans les limites de la plage des valeurs valides.
    Nom du paramètre*: index

    Merci pour vos efforts, mais cette solution ne fonctionne pas non plus.

    J'insiste sur le fait que j'ai besoin de l'icône dont le # de ressource est, par exemple, le 16818 (une flèche grise, voir mes précédents messages). Extraire ce même icône par son indice 307 est facile et fonctionne déjà chez moi.

  13. #33
    Expert éminent
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 000
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 000
    Points : 9 384
    Points
    9 384
    Par défaut
    voici un programme dotnet qui affiche l'icone par son index ou par son numéro de ressource (en chiffre négatif). Il utilise l'API windows ExtractIconEx
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
     
     
    namespace DisplayIcon
    {
    	/// <summary>
    	/// Description of MainForm.
    	/// </summary>
    	///
     
     
     
     
    	public partial class MainForm : Form
    	{
    		private IntPtr iconHandle;
     
     
            [DllImport("Shell32.dll")]
            private static extern IntPtr ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, 
                                                       out IntPtr piSmallVersion, int amountIcons);
            [DllImport("User32.dll")]
            private static extern bool DestroyIcon(IntPtr hIcon);
                [DllImport("Shell32.dll")]
     
            private static extern int SHGetFileInfo(
            string pszPath, uint dwFileAttributes,
            out SHFILEINFO psfi, uint cbfileInfo,
            SHGFI uFlags);
     
     
            private struct SHFILEINFO
           {
            public SHFILEINFO(bool b)
            {
                hIcon = IntPtr.Zero; iIcon = 8; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
            }
            public IntPtr hIcon;
            public int iIcon;
            public uint dwAttributes;
            public string szDisplayName;
            public string szTypeName;
           };
     
     
           private enum SHGFI
           {
            SmallIcon = 0x00000001,
            OpenIcon = 0x00000002,
            LargeIcon = 0x00000000,
            Icon = 0x00000100,
            DisplayName = 0x00000200,
            Typename = 0x00000400,
            SysIconIndex = 0x00004000,
            LinkOverlay = 0x00008000,
            UseFileAttributes = 0x00000010
           }
     
     
    		public MainForm()
    		{
    			//
    			// The InitializeComponent() call is required for Windows Forms designer support.
    			//
    			InitializeComponent();
     
    			//
    			// TODO: Add constructor code after the InitializeComponent() call.
    			//
    		}
    		void Button1Click(object sender, EventArgs e)
    		{
    	    IntPtr largeIconPtr = IntPtr.Zero;
            IntPtr smallIconPtr = IntPtr.Zero;	
    		var filePath = @"c:\windows\system32\shell32.dll";
    		        SHFILEINFO info = new SHFILEINFO(true);
            int cbFileInfo = Marshal.SizeOf(info);
            SHGFI flags;
            flags = SHGFI.Icon | SHGFI.LargeIcon;
     
     
            // SHGetFileInfo(filePath, 0, out info, (uint)cbFileInfo, flags);
     
     
    		ExtractIconEx(filePath, -142, out largeIconPtr, out smallIconPtr, 1);
    		pictureBox1.Image = Icon.FromHandle(largeIconPtr).ToBitmap();
     
     
    		}
    	}
    }
    Le problème c'est que l'api ExtractIconEx extrait que les icônes de résolution 16x16 et 32x32 et donc pas les jumbo ou extra (256x256, 128x128).
    Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

  14. #34
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2012
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 37
    Points : 44
    Points
    44
    Par défaut
    La taille de 32x32 est suffisant pour ce que j'ai à faire pour l'instant.

    En utilisant ExtractIconEx et l'exemple de https://forum.pcsoft.fr/fr-FR/pcsoft...rence/read.awp, ça a fini par fonctionner. Bien vu, ça m'avait échappé qu'on pouvait lui passer une valeur négative et la syntaxe utilisée ne prend, bizarrement, pas en compte les icônes petit et grand. Je m'en vais intégrer cette extraction dans Betula.

    Merci !

  15. #35
    Expert éminent
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    4 000
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 4 000
    Points : 9 384
    Points
    9 384
    Par défaut
    Hello,
    Citation Envoyé par Lapalys Voir le message
    Bien vu, ça m'avait échappé qu'on pouvait lui passer une valeur négative et la syntaxe utilisée ne prend, bizarrement, pas en compte les icônes petit et grand. Je
    Dans le code de ton lien il y a :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    // Petite icône
    nbIcons = API("SHELL32","ExtractIconExA", sDefaultIcon, nIconIndex, Null, &hIcon, 1)
    Tu peux essayer à la place pour récupérer la petite icône (16x16) et la grande icône (32x32) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    // Petite et grande icône
    nbIcons = API("SHELL32","ExtractIconExA", sDefaultIcon, nIconIndex, &hLargeIcon, &hSmallIcon, 1)
    Ami calmant, J.P
    Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

  16. #36
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2012
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 37
    Points : 44
    Points
    44
    Par défaut
    Oui je parlais de la portion _handle=API("SHELL32","ExtractIconA",0,&_exefilename,0) dans lequel le dernier 0 est mon fameux indice (positif ou négatif). Cette syntaxe simplifiée me donne bien l'icône dont j'ai besoin.

    @Jurassik Pork : peux-tu me contacter en MP stp ? Merci

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. OpenGL appelé depuis une DLL perd son contexte
    Par sopsag dans le forum OpenGL
    Réponses: 7
    Dernier message: 27/10/2008, 12h39
  2. selection d'une liste selon son nom
    Par pedro99 dans le forum Général Python
    Réponses: 6
    Dernier message: 29/06/2007, 13h47
  3. Comment Ajouter une dll a son application web ?
    Par kedare dans le forum ASP.NET
    Réponses: 7
    Dernier message: 08/02/2007, 16h47
  4. [C#] Extraire l'interface d'une dll
    Par Thalion dans le forum Windows Forms
    Réponses: 4
    Dernier message: 07/10/2004, 08h01
  5. Extraire les icônes d'une DLL
    Par Globus dans le forum MFC
    Réponses: 6
    Dernier message: 13/09/2002, 13h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo