Projet gus05 de développement Java
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Le Deal du moment : -28%
Brandt LVE127J – Lave-vaisselle encastrable 12 ...
Voir le deal
279.99 €

Détecter le type de fichier DLL, 32bit ou 64bit

Aller en bas

Détecter le type de fichier DLL, 32bit ou 64bit Empty Détecter le type de fichier DLL, 32bit ou 64bit

Message  Gus Dim 7 Oct - 23:02

Sous Windows, il existe actuellement deux types d'architecture : 32bit et 64bit.
Pour connaitre votre type d'architecture, il vous suffit d'afficher les propriétés de votre Windows :

Détecter le type de fichier DLL, 32bit ou 64bit Type_de_systeme_windows

Les fichiers DLL (Dynamic Link Library) utilisées par Java pour charger des fonctions natives (programmation JNI) doivent correspondre à l'architecture (32bit ou 64bit) et des DLL non adaptées entrainent des problèmes de compatibilité. Il existe ainsi des DLL 32bit et des DLL 64bit (et aussi des DLL 16bit pour les vieux ordis...) et il peut être utile de déterminer en Java à quel type de DLL on a à faire. Je vais vous expliquer comment faire cela avec un exemple d'entité de type transformation : gus.env.windows.dll.findtype

Code:
package gus05.entity.gus.env.windows.dll.findtype;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import gus05.framework.core.Entity;
import gus05.framework.features.Transform;


public class FindDllType implements Entity, Transform {

    public String getName()            {return "gus.env.windows.dll.findtype";}
    public String getCreationDate()      {return "2012.10.07";}

    public static final int MZ_MAGIC = 0x5A4D;
    public static final int PE_SIGNATURE = 0x00004550;
   
    public static final int IMAGE_FILE_MACHINE_UNKNOWN      = 0x0000; //The contents of this field are assumed to be applicable to any machine type
    public static final int IMAGE_FILE_MACHINE_AM33         = 0x01D3; //Matsushita AM33
    public static final int IMAGE_FILE_MACHINE_AMD64      = 0x8664; //x64
    public static final int IMAGE_FILE_MACHINE_ARM         = 0x01C0; //ARM little endian
    public static final int IMAGE_FILE_MACHINE_ARMV7      = 0x01C4; //ARMv7 (or higher) Thumb mode only
    public static final int IMAGE_FILE_MACHINE_EBC         = 0x0EBC; //EFI byte code
    public static final int IMAGE_FILE_MACHINE_I386         = 0x014C; //Intel 386 or later processors and compatible processors
    public static final int IMAGE_FILE_MACHINE_IA64         = 0x0200; //Intel Itanium processor family
    public static final int IMAGE_FILE_MACHINE_M32R         = 0x9041; //Mitsubishi M32R little endian
    public static final int IMAGE_FILE_MACHINE_MIPS16      = 0x0266; //MIPS16
    public static final int IMAGE_FILE_MACHINE_MIPSFPU      = 0x0366; //MIPS with FPU
    public static final int IMAGE_FILE_MACHINE_MIPSFPU16   = 0x0466; //MIPS16 with FPU
    public static final int IMAGE_FILE_MACHINE_POWERPC      = 0x01F0; //Power PC little endian
    public static final int IMAGE_FILE_MACHINE_POWERPCFP   = 0x01F1; //Power PC with floating point support
    public static final int IMAGE_FILE_MACHINE_R4000      = 0x0166; //MIPS little endian
    public static final int IMAGE_FILE_MACHINE_SH3         = 0x01A2; //Hitachi SH3
    public static final int IMAGE_FILE_MACHINE_SH3DSP      = 0x01A3; //Hitachi SH3 DSP
    public static final int IMAGE_FILE_MACHINE_SH4         = 0x01A6; //Hitachi SH4
    public static final int IMAGE_FILE_MACHINE_SH5         = 0x01A8; //Hitachi SH5
    public static final int IMAGE_FILE_MACHINE_THUMB      = 0x01C2; //ARM or Thumb (“interworking”)
    public static final int IMAGE_FILE_MACHINE_WCEMIPSV2   = 0x0169; //MIPS little-endian WCE v2

   
    private DataInputStream in;


    public Object transform(Object obj) throws Exception
    {
       InputStream is = toInputStream(obj);
       in = new DataInputStream(is);
       
       String type = extractType();
       in.close();
       is.close();
       
        return type;
    }
   
    private String extractType() throws Exception
    {
       int mz_magic = _unsigned_();
       if(mz_magic!=MZ_MAGIC) throw new Exception("Invalid MZ magic number : "+mz_magic);
       // pos: 2
       
       in.skipBytes(58);
       // pos: 60
       
       int e_lfanew = _int_();
       if(e_lfanew<=64) throw new Exception("Invalid e_lfanew value : "+e_lfanew);
       
       in.skipBytes(e_lfanew-64);
       // pos: 'e_lfanew'
       
       int pe_signature = _int_();
       if(pe_signature!=PE_SIGNATURE) throw new Exception("Invalid PE signature : "+pe_signature);
       // pos: 'e_lfanew'+4
       
       int image_file_machine = _unsigned_();
       
       switch(image_file_machine)
       {
          case IMAGE_FILE_MACHINE_AMD64:return "64";
          case IMAGE_FILE_MACHINE_I386:return "32";
          default:return "?";
       }
    }
   
    private int _int_() throws Exception
    {
       byte[] b = new byte[4];
        in.readFully(b,0,4);
        return (b[3]) << 24 | (b[2] & 0xFF) << 16 | (b[1] & 0xFF) << 8 | (b[0] & 0xFF);
    }
   
   
    private int _unsigned_() throws Exception
    {
       byte[] b = new byte[2];
        in.readFully(b,0,2);
        return (b[1] & 0xFF) << 8 | (b[0] & 0xFF);
    }
   
    private InputStream toInputStream(Object obj) throws Exception
    {
       if(obj instanceof InputStream) return (InputStream) obj;
       if(obj instanceof File) return new FileInputStream((File)obj);
       throw new Exception("Invalid data type: "+obj.getClass().getName());
    }
}

L'utilisation de cette entité est extrêmement simple : vous mettez en entrée un objet File correspondant à votre DLL, et vous obtenez en sortie une chaine de caractères qui peut prendre les valeurs : "32" (32 bit) "64" (64 bit) ou "?" (ni l'un ni l'autre...). Et vous n'avez nullement besoin de comprendre comment c'est implémenté pour utiliser cette fonctionnalité ! tongue

Pour les petits curieux qui souhaitent comprendre, je ferai un autre post dédié à la question.

Pour finir, voici une illustration d'un explorateur de fichiers que j'ai développé et qui exploite cette entité pour personnaliser les icônes des fichiers DLL suivant qu'elles sont 32bit ou 64bit :

Détecter le type de fichier DLL, 32bit ou 64bit Explorer_dll
Gus
Gus
Admin

Messages : 249
Date d'inscription : 01/09/2009

http://www.gus05.com

Revenir en haut Aller en bas

Revenir en haut

- Sujets similaires

 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum