Rotinas para Manipulação de DiskDrives
Continuando nossa série de artigos, relacionados à criação de bibliotecas com rotinas padrão em Delphi, damos inicio a Operações de Hardware.
É muito bom lembrar que as rotinas vistas aqui, nessa parte de hardware, são muito utilizadas para proteger seu software, por exemplo, pois usamos rotinas, como verificação se serial, no HardDisk, para testar se nossa aplicação foi alterada, ou copiada pra outro HardDisk não habilitado entre outras coisas.
· Tratamento de arquivos;
· Operações com DisksDrives;
· Operações de Hardware; ß estamos aqui!
· Operações com Mouse;
· Operações com o Sistema;
· Operações com Strings;
Então.. mãos na massa. Crie um arquivo e dê o nome de “Hardware”, no seu Delphi.
1:
2: unit Hardware;
3:
4:
5: interface
6:
7:
8: uses
9: Windows, Dialogs, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics,
10: Printers, shellapi, MMSystem;
11:
12: function TestaPlaca(Value:integer): Boolean;
13: function DiscoNoDrive(const drive : char): boolean;
14: function NumeroSerie(Unidade:PChar):String;
15: function DetectaDrv(const drive : char): boolean;
16: function NumeroDeCores : Integer;
17: function Percentdisk(unidade: byte): Integer;
18: function IsPrinter : Boolean;
19: function CorrentPrinter :String;
20:
21: implementation
22:
23: Function TestaPlaca(Value:integer): Boolean;
24: {Testa se existe uma placa de som no seu computador}
25: begin
26: if WaveOutGetNumDevs > 0 then
27: begin
28: result := True
29: end
30: else
31: begin
32: Result := False;
33: end;
34: end;
35:
36: function DiscoNoDrive(const drive : char): boolean;
37: {Detecta se há disco no Drive}
38: var
39: DriveNumero : byte;
40: EMode : word;
41: begin
42: result := false;
43: DriveNumero := ord(Drive);
44: if DriveNumero >= ord('a') then
45: begin
46: dec(DriveNumero,$20);
47: EMode := SetErrorMode(SEM_FAILCRITICALERRORS);
48: end;
49: try
50: if DiskSize(DriveNumero-$40) = -1 then
51: begin
52: Result := False;
53: end
54: else
55: begin
56: Result := True;
57: end;
58: Except
59: SetErrorMode(EMode);
60: end;
61: end;
62:
63: function NumeroSerie(Unidade:PChar):String;
64: {Retorna o Número serial da unidade especificada}
65: var
66: VolName,SysName : AnsiString;
67: SerialNo,MaxCLength,FileFlags : DWord;
68: begin
69: try
70: SetLength(VolName,255);
71: SetLength(SysName,255);
72: GetVolumeInformation(Unidade,PChar(VolName),255,@SerialNo,
73: MaxCLength,FileFlags,PChar(SysName),255);
74: result := IntToHex(SerialNo,8);
75: except
76: result := ' ';
77: end;
78: end;
79:
80: function DetectaDrv(const drive : char): boolean;
81: {Detecta quantas unidade possui no computador}
82: var
83: Letra: string;
84: begin
85: Letra := drive + ':\';
86: if GetDriveType(PChar(Letra)) < 2 then
87: begin
88: result := False;
89: end
90: else
91: begin
92: result := True;
93: end;
94: end;
95:
96:
97: function NumeroDeCores : Integer;
98: {Retorna a quantidade atual de cores no Windows (16, 256, 65536 = 16 ou 24 bit}
99: var
100: DC:HDC;
101: BitsPorPixel: Integer;
102: begin
103: Dc := GetDc(0); // 0 = vídeo
104: BitsPorPixel := GetDeviceCaps(Dc,BitsPixel);
105: Result := 2 shl (BitsPorPixel - 1);
106: end;
107:
108: function Percentdisk(unidade: byte): Integer;
109: {Retorna a porcentagem de espaço livre em uma unidade de disco}
110: var
111: A,B, Percentual : longint;
112: begin
113: if DiskFree(Unidade)<> -1 then
114: begin
115: A := DiskFree(Unidade) div 1024;
116: B := DiskSize(Unidade) div 1024;
117: Percentual:=(A*100) div B;
118: result := Percentual;
119: end
120: else
121: begin
122: result := -1;
123: end;
124: end;
125:
126: function IsPrinter : Boolean;
127: Const
128: PrnStInt : Byte = $17;
129: StRq : Byte = $02;
130: PrnNum : Word = 0; { 0 para LPT1, 1 para LPT2, etc. }
131: Var
132: nResult : byte;
133: Begin (* IsPrinter*)
134: Asm
135: mov ah,StRq;
136: mov dx,PrnNum;
137: Int $17;
138: mov nResult,ah;
139: end;
140: IsPrinter := (nResult and $80) = $80;
141: End;
142:
143: function CorrentPrinter :String;
144: // Retorna a impressora padrão do windows
145: // Requer a unit printers declarada na clausula uses da unit
146: var
147: Device : array[0..255] of char;
148: Driver : array[0..255] of char;
149: Port : array[0..255] of char;
150: hDMode : THandle;
151: begin
152: Printer.GetPrinter(Device, Driver, Port, hDMode);
153: Result := Device+' na porta '+Port;
154: end;
155:
156: end.
Até o próximo artigo, nos veremos no artigo de bibliotecas de mouse, abraços++.