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:
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:
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:
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:
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
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
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
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