#include <stdio.h>
#include <dos.h>
#include <string.h>
// Versao em C da funcao IsWriteProtected
// Parametros de entrada
// Drive
// Tipo: char
// Proposito: numero do drive a ser testado, A = 0, B = 1
// SerialNumber
// Tipo: unsigned long *
// Proposito: retornar o numero de serie do drive testado
//
// Parametros de saida
// Tipo: int
// Significado:
// 0 = desprotegido
// 1 = protegido
// -1 = erro de leitura
//
// Autor: Data:
// Wenderson Teixeira 05/09/98 Conversao de Asm p/ C
// Wenderson Teixeira 05/05/98 Agora funciona em modo large
// Wenderson Teixeira 05/05/98 Trocada int 25h/26h por int 13h
//
// Obs.:
// 1 - Quando da erro de leitura, o valor de SerialNumber fica
// inalterado.
// 2 - Ao trocar para a int 13h, corrigiu-se o problema de nao
// conseguir ler o numero de serie no Windows 95/98, porem,
// da erro de leitura quando o disco acabou de ser inserido
// no drive, o que antes nao ocorria com as int 25h e 26h.
// 3 - Nao foi testado a leitura de HD's (80h, 81h, etc), ja
// que o tamanho do setor e diferente e provavelmente
// acarretara em erro
int IsWriteProtected(char Drive, unsigned long *SerialNumber)
{
char buffer[1024];
union REGS r;
struct SREGS s;
s.es = FP_SEG(buffer);
s.ds = _DS;
r.h.ah = 2;
r.h.al = 1;
r.h.ch = 0;
r.h.cl = 1;
r.h.dh = 0;
r.h.dl = Drive;
r.x.bx = FP_OFF(buffer);
int86x(0x13, &r, &r, &s);
if(!r.x.cflag)
{
s.es = FP_SEG(buffer);
s.ds = _DS;
r.h.ah = 3;
r.h.al = 1;
r.h.ch = 0;
r.h.cl = 1;
r.h.dh = 0;
r.h.dl = Drive;
r.x.bx = FP_OFF(buffer);
int86(0x13, &r, &r);
*SerialNumber = *((unsigned long *)&buffer[39]);
if(!r.x.cflag || r.h.ah != 3)
return 0;
return 1;
}
return -1;
}
void main()
{
char str[50];
unsigned long serial;
int c = IsWriteProtected(0, &serial);
switch(c)
{
case 0:
strcpy(str, "desprotegido");
break;
case 1:
strcpy(str, "protegido");
break;
case -1:
strcpy(str, "erro de leitura");
break;
}
printf("Drive A: - %s - %04X-%04X", str, *((int *)(&serial) + 1), *(int *)&serial);
}