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

API, COM et SDKs Delphi Discussion :

Traduire Code C# --> Delphi


Sujet :

API, COM et SDKs Delphi

  1. #1
    Futur Membre du Club
    Inscrit en
    Mai 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Mai 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut Traduire Code C# --> Delphi
    Mes saluts
    Dans la mesure du possible, si quelqu'un puisse m'aider a en tirer profit du Code suivant en Delphi
    ------------ Voici le code dans le contexte de son auteur respectif ---------
    Introduction

    Making sure your software is used by legal buyers is a concern for programmers around the world. My professor once said that we shouldn’t give 100% of our code to the users because there are people out there that are smart enough to decompile our programs and find the various verification algorithms used. He suggested that we give users 99% of our software, but keep the remaining 1% to ourselves. This 1% is the verification algorithm to confirm only valid users can use the program; this is commonly known as “activation.”

    Activation is good, but it means our software users will need to have Internet access and that means small programmers like us have to set up a server that can validate users. Of course, only big companies with big clients can afford to do this. For the rest of us, we have to think of other ways.

    One method programmers have used since the DOS era was to bind their software to the Hard Drive Volume Serial Number. This is not a good choice, as later we all find out that every time we format the same hard drive, a new Volume Serial Number is generated.

    Background

    A better solution is to get the Hard Drive Serial Number given by the Manufacturer. This value won't change even if you format your Hard Drive. Of course, if people buy new hard drive, we have a problem. But at least we keep the regular hard-drive formatters happy!

    The Code

    First, let's create a class to store information about a hard drive:

    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
    class HardDrive
    {
     private string model = null;
     private string type = null;
     private string serialNo = null; 
     public string Model
     {
      get {return model;}
      set {model = value;}
     } 
     public string Type
     {
      get {return type;}
      set {type = value;}
     } 
     public string SerialNo
     {
      get {return serialNo;}
      set {serialNo = value;}
     }
    }
    Next, add a reference to your project. Scroll down to System.Management under ComponentName. This reference is not provided by default, so you need to add it.

    Add a "using System.Management;" at the top of your source code. System.Management allows us to access WMI objects. Put simply, WMI contains a lot information about your hardware.

    Now there's a problem: the hard drive model is found in the Win32_DiskDrive class and the serial number is found in the Win32_PhysicalMedia class. Thus, we need to query twice and integrate the information into our HardDrive class.

    Let's first create an ArrayList to store our HardDrive objects: ArrayList hdCollection = new ArrayList();. Next, we query the Win32_DiskDrive class:

    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
       ManagementObjectSearcher searcher = new
        ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
     
       foreach(ManagementObject wmi_HD in searcher.Get())
       {
        HardDrive hd = new HardDrive();
        hd.Model = wmi_HD["Model"].ToString();
        hd.Type  = wmi_HD["InterfaceType"].ToString(); 
        hdCollection.Add(hd);
       }
    Now we need to extract the serial number from the Win32_PhysicalMedia class:

    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
       searcher = new
        ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
     
       int i = 0;
       foreach(ManagementObject wmi_HD in searcher.Get())
       {
        // get the hard drive from collection
        // using index
        HardDrive hd = (HardDrive)hdCollection[i];
     
        // get the hardware serial no.
        if (wmi_HD["SerialNumber"] == null)
         hd.SerialNo = "None";
        else
         hd.SerialNo = wmi_HD["SerialNumber"].ToString();
     
        ++i;
       }
    Luckily, the WMI objects from Win32_DiskDrive are in the same order as from Win32_PhysicalMedia, so the simple code above works. Otherwise, we will have to match them using their unique DeviceID. Start a thread in this article if you think Win32_DiskDrive objects are not the same order as Win32_PhysicalMedia objects returned.

    We are done! Now we display our hard drive's information:

    Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
       // Display available hard drives
       foreach(HardDrive hd in hdCollection)
       {
        Console.WriteLine("Model\t\t: " + hd.Model);
        Console.WriteLine("Type\t\t: " + hd.Type);
        Console.WriteLine("Serial No.\t: " + hd.SerialNo);
        Console.WriteLine();
       }
    Amicalement

  2. #2
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 091
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

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

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 091
    Points : 41 067
    Points
    41 067
    Billets dans le blog
    62
    Par défaut
    Bonjour et Bienvenu sur le forum .

    En fait la question est plus simple .
    Comment obtenir le numéro de série d'un disque dur (en Delphi bien sûr)
    la réponse est dans la ici
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  3. #3
    Futur Membre du Club
    Inscrit en
    Mai 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Mai 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut Traduire Code C# --> Delphi
    Merci Cher Monsieur
    J'en suis au courant de tout ce qui bat de l'aile dans la Faq's Delphi dont mes succés y sont tributaires, mais le code en question retourne d'aprés son auteur << LE NUMERO DE SERIE D'USINAGE >> qui est unique et non variable à chaque operation formatage .
    Cordialement

  4. #4
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 091
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Loire Atlantique (Pays de la Loire)

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

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 091
    Points : 41 067
    Points
    41 067
    Billets dans le blog
    62
    Par défaut
    Je cite Microsoft
    GetVolumeInformation Function

    Retrieves information about the file system and volume associated with the specified root directory.

    To specify a handle when retrieving this information, use the GetVolumeInformationByHandleW function.

    To retrieve the current compression state of a file or directory, use FSCTL_GET_COMPRESSION.
    Syntax

    BOOL WINAPI GetVolumeInformation(
    __in_opt LPCTSTR lpRootPathName,
    __out LPTSTR lpVolumeNameBuffer,
    __in DWORD nVolumeNameSize,
    __out_opt LPDWORD lpVolumeSerialNumber,
    __out_opt LPDWORD lpMaximumComponentLength,
    __out_opt LPDWORD lpFileSystemFlags,
    __out LPTSTR lpFileSystemNameBuffer,
    __in DWORD nFileSystemNameSize
    );

    Parameters

    lpRootPathName

    A pointer to a string that contains the root directory of the volume to be described.

    If this parameter is NULL, the root of the current directory is used. A trailing backslash is required. For example, you specify \\MyServer\MyShare as "\\MyServer\MyShare\", or the C drive as "C:\".
    lpVolumeNameBuffer

    A pointer to a buffer that receives the name of a specified volume. The maximum buffer size is MAX_PATH+1.
    nVolumeNameSize

    The length of a volume name buffer, in TCHARs. The maximum buffer size is MAX_PATH+1.

    This parameter is ignored if the volume name buffer is not supplied.
    lpVolumeSerialNumber

    A pointer to a variable that receives the volume serial number.

    This parameter can be NULL if the serial number is not required.

    This function returns the volume serial number that the operating system assigns when a hard disk is formatted. To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI) Win32_PhysicalMedia property SerialNumber.
    donc je dirais que le numéro de série obtenu est bien celui d'usine "that the manufacturer assigns", d'ailleurs je ne vois pas comment ce numéro changerai par formattage
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  5. #5
    Futur Membre du Club
    Inscrit en
    Mai 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Mai 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut Implementer Win_32PhysicalMedia en Delphi
    Jusqu'a l'indication du contraire : La fonction GetVolumeInformation renvoie entre autres Le Numero de Serie du Lecteur Logique passé en paramètre et qui est bien variable à chaque operation de FORMATAGE .

    La reponse evidente reside dans le paragrphe :

    To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI)
    Win_32PhysicalMedia Property SerialNumber

    En conclusion Est ce possible d'implementer Win_32physicalMedia en Delphi ?.
    Mes respects

  6. #6
    Membre chevronné

    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    1 519
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 1 519
    Points : 2 153
    Points
    2 153
    Billets dans le blog
    1
    Par défaut
    D'après ce que j'ai lu en diagonale ça devrait être possible. Je te conseilles pour commencer d'étudier le tutorial de Laurent Dardenne à ce sujet. Ensuite tu seras probablement à même de traduire le code C# en Delphi (la seule différence que je vois c'est qu'il n'y aura pas la classe ManagementObjectSearcher sauf si tu fais un projet .NET peut-être mais on peut très bien palier à ça).
    La FAQ - les Tutoriels - Le guide du développeur Delphi devant un problème

    Pas de sollicitations techniques par MP -

  7. #7
    Membre du Club
    Inscrit en
    Juillet 2003
    Messages
    41
    Détails du profil
    Informations forums :
    Inscription : Juillet 2003
    Messages : 41
    Points : 42
    Points
    42

  8. #8
    Futur Membre du Club
    Inscrit en
    Mai 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Mai 2008
    Messages : 16
    Points : 9
    Points
    9
    Par défaut Physical Disk Serial Number (Resolu)
    C'est genereux de votre part Mr Med1112 vous avez tonifié mes carences, j'y vais en tirer profit en separant les fonctions, il me suffit de localiser les unités ou sont definies les multiples fonctions .
    Vraiment je ne m'attendais pas a un tel don .
    Je tiens à remercier tout ceux qui se sont souciés de ma question : Mrs AKa Gumlef, Sergio Mester et tous les membres du club .
    Merci beaucoup
    Bon courage

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Du code java dans delphi
    Par toure32 dans le forum Delphi
    Réponses: 2
    Dernier message: 14/06/2006, 23h26
  2. Code API de DELPHI
    Par ariel555 dans le forum API, COM et SDKs
    Réponses: 2
    Dernier message: 05/04/2006, 11h52
  3. Convertir code C en delphi
    Par jt-tronix dans le forum Langage
    Réponses: 12
    Dernier message: 29/03/2006, 20h55
  4. Code VB6 vers Delphi ?
    Par isachat666 dans le forum Langage
    Réponses: 4
    Dernier message: 11/01/2006, 15h07
  5. utilisation d'un lecteur de code barre sous delphi
    Par bm10 dans le forum Composants VCL
    Réponses: 1
    Dernier message: 19/11/2005, 20h05

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