Busca sus n煤meros de serie, si lo hay es que hay un pendcrive pinchado.
Aqu铆 te dejo rutinas que a mi me sirven de protecci贸n contra usuarios no autorizados.
Creo que m谩s en concreto la funci贸n aUSBDrive().
Carlos G.
/* **********************************************************************
18/11/2008
Aqu铆 dejar茅 las funciones gen茅ricas que utilizar茅 para la gesti贸n de la
protecci贸n con USB.
聽-En W2000 no es operativa para usuarios no Administradores.
聽-La variable STATIC aUSB contiene el indicador l贸gico de 'DEMO' en el
聽 primer elemento, y los n煤meros de serie i descriptivo (en una array de
聽 2 elementos) de los Pendrive (USB) autorizados para usar la aplicaci贸n
聽 sin l铆mites.
聽-INICIALMENTE el primer elemento de aUSB debe ser siempre .T.
聽-La funci贸n WarningDemo() es la que se puede insertar en CUALQUIER PUNTO
聽 o PUNTOS de la aplicaci贸n para verificar/avisar si se trata de una DEMO.
聽 Lo habitual es usarla con el par谩metro .T. ( WarningDemo( .T. ) )
聽 Es esta funci贸n la que se debe modificar seg煤n las necesidades; en mi
聽 caso avisa si quedan menos de 2 minutos de demostraci贸n y pasados 5
聽 minutos cierra la aplicaci贸n.
聽 Otra opci贸n es modificar esta funci贸n para que en base al valor de
聽 retorno (.T. o .F.), realizar lo que queramos en cualquier lugar de
聽 nuestra aplicaci贸n.
聽-La funci贸n cGetSerialProtect() es la que nos dir谩 el n煤mero de serie
聽 de el USB que escojamos de la lista que nos ense帽e.
聽 Se puede usar en un men煤 de configuraci贸n de la aplicaci贸n para que el
聽 usuario nos facilite el n煤mero de serie del USB que utilizar谩.
********************************************************************** */
#include "FiveWin.ch"
#define DRIVE_REMOVABLE 聽2
/* ************************ ATENCI脫 ***********************
聽 聽N煤meros de s猫rie d'USB admesos.
聽 聽El 1er. element indica si la verificaci贸 ha tingut 猫xit.
聽 聽Los elementos posteriores son arrays de 2 elemntos con
聽 聽los n煤meros de serie y descriptivos de los USB
聽 聽autorizados.
聽 聽Inicialmente el 1er. elemento SIEMPRE debe de ser .T.
聽 聽----------------------------------------------------- */
STATIC aUSB := { .T., ;
聽 聽 聽 聽 聽 聽 聽 聽 聽{"077522058501D5A6", "Sense descriptiu" }, ;
聽 聽 聽 聽 聽 聽 聽 聽 聽{"0775030772C2BFF2", "Sense descriptiu" }, ;
聽 聽 聽 聽 聽 聽 聽 聽 聽{"07871044024E", "EMTEC 8 Gb (marr贸n)" }, ;
聽 聽 聽 聽 聽 聽 聽 聽 聽{"000AEB91EBF75A891D010114", "Kingston 16 Gb extensible (Pl脿stic negre)" }, ;
聽 聽 聽 聽 聽 聽 聽 聽 聽{"0799120338141B2D", "Imation Mini de 4 Gb (metall plata, Pl脿stic negre)" }, ;
聽 聽 聽 聽 聽 聽 聽 聽 聽{"W6YFEDS7NDAQCNKK", "Susana"} ;
聽 聽 聽 聽 聽 聽 聽 聽}
/* *************************** */
/* *************************** */
FUNCTION QuitApl()
CLOSE ALL
PostQuitMessage( 0 )
sysrefresh()
__quit()
Return NIl
/* *************************** */
/* *************************** */
/* Mostra missatge per indicar que s'est脿 en modus DEMO.
聽 聽-------------------------------------------------- */
FUNCTION WarningDemo( lVerifica )
// Dia i hora inicial.
STATIC aTimeInfo := { 0, 0 }
// 3 minuts (en segons)
Local Limita := 180
// 5 minuts (en segons)
Local Limitb := 300
Local SecFromStart := 0
If lVerifica .and. !lIsDemo( )
聽 聽 aUSB[1] := lIsOkUSB()
EndIf
If lIsDemo()
聽 聽 // Si no s'havia capturat el moment inicial, es captura ara.
聽 聽 If aTimeInfo[2] = 0
聽 聽 聽 聽 aTimeInfo[1] := Date()
聽 聽 聽 聽 aTimeInfo[2] := Seconds()
聽 聽 聽 聽 MsgAlert( "Aplicaci贸 en modus DEMOSTRACI脫." + CRLF + CRLF + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 "TOTES les opcions s贸n COMPLETAMENT operatives." + CRLF + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 "La limitaci贸 茅s de " + Str( Limitb/60, 2, 0) + " minuts per poder operar.", ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 " A T E N C I 脫 !" )
聽 聽 EndIf
聽 聽 SecFromStart := SecsFromStart( aTimeInfo[1], aTimeInfo[2] )
聽 聽 If SecFromStart > Limitb
聽 聽 聽 聽 MsgAlert( "Ha esgotat el temps de DEMOSTRACI脫", " A T E N C I 脫 !" )
聽 聽 聽 聽 //QUIT
聽 聽 聽 聽 // Es tanca l'aplicaci贸.
聽 聽 聽 聽 QuitApl()
聽 聽 ElseIf SecFromStart > Limita
聽 聽 聽 聽 MsgAlert( "Queden " + Str( (Limitb - SecFromStart) / 60, 1, 0 ) 聽+ " minuts de DEMOSTRACI脫", " A T E N C I 脫 !" )
聽 聽 EndIf
EndIf
Return aUSB[1]
/* *************************** */
/* *************************** */
STATIC function SecsFromStart( dInitDate, nInitSecs ) // by hDC
local nSeconds:= Seconds()
local nDays := Date() - dInitDate
if ValType( nDays ) == "D" 聽// C3 temporary workaround
聽 聽 nDays = Day( Date() ) - Day( dInitDate )
endif
if nDays < 0 聽// Surely you change the date after the beginning of the system
聽 聽 return 0
endif
if nDays > 0
聽 nDays--
聽 return ( 86399 - nInitSecs ) + ( nDays * 86399 ) + nSeconds
endif
return nSeconds - nInitSecs
/* *************************** */
/* *************************** */
/* ************************************************
聽 聽Nos indica si se trata de una versi贸n DEMO o no.
聽 聽--------------------------------------------- */
FUNCTION lIsDemo( )
/* La obtenci贸n del n煤mero de serie del USB no funciona bajo W2000 si no se
聽 聽es administrador del sistema, de ah铆 que si se est谩 bajo W2000 se de por
聽 聽v谩lido a煤n sin tener el USB conectado.
*/
Return !( aUSB[1] .or. IsWin2000() )
/* *************************** */
/* *************************** */
/* *********************************************************************************
聽 聽Chequea si se encuentra instalado el USB correspondiente a uno de los n煤meros de
聽 聽serie recibidos.
聽 聽ATENCI脫N: de la array aUSB el primer elemento no es tenido en
聽 聽cuenta ya que se trata del indicador de DEMO (.T. o .F.).
聽 聽------------------------------------------------------------------------------ */
STATIC FUNCTION lIsOkUSB()
Local nContador := 0
/* El 1er. element 茅s l'indicador de DEMO.
聽 聽----------------------------------- */
For nContador := 2 to Len( aUSB )
聽 聽 If cNumUSB2LetUSB( aUSB[ nContador ][1] ) <> ""
聽 聽 聽 聽 Exit
聽 聽 EndIf
EndFor
Return !( nContador > Len( aUSB ) )
/* *************************** */
/* *************************** */
/* *****************************************************************
聽 聽A partir de un n煤mero de serie obtiene la letra del Pendrive USB.
聽 聽-------------------------------------------------------------- */
STATIC FUNCTION cNumUSB2LetUSB( cNumUSB )
Local cLetter 聽 聽:= ""
Local nBitDrives := GetLogicalDrives()
Local nContador 聽:= 0
Local cDrive 聽 聽 := ""
For nContador := 1 to 32
聽 聽 If lAnd( nBitDrives, 2 ^ ( nContador - 1 ) )
聽 聽 聽 聽 cDrive = Chr( Asc( "A" ) - 1 + nContador ) + ":\"
聽 聽 聽 聽 If nContador != 1 .and. GetDriveType( cDrive ) == DRIVE_REMOVABLE
聽 聽 聽 聽 聽 聽 If GetUSBSerial( cDrive ) == cNumUSB
聽 聽 聽 聽 聽 聽 聽 聽 cLetter := cDrive
聽 聽 聽 聽 聽 聽 聽 聽 Exit
聽 聽 聽 聽 聽 聽 EndIf
聽 聽 聽 聽 Endif
聽 聽 Endif
Next
Return cLetter
/* *************************** */
/* *************************** */
/* ***********************************************************************
聽 聽A partir d'una lletra d'un USB inicia la cerca del seu n煤mero de s猫rie.
聽 聽-------------------------------------------------------------------- */
STATIC FUNCTION GetUSBSerial( cDrive )
Local oLoc := CreateObject( "wbemScripting.SwbemLocator" )
Local oSrv := oLoc:ConnectServer()
Local oJobs := oSrv:ExecQuery( "SELECT * FROM Win32_LogicalDiskToPartition" )
Local oJob
Local cDriveNumber
cDrive = Upper( cDrive )
If Len( cDrive ) == 1
聽 聽 cDrive += ":"
Endif
If Len( cDrive ) > 2
聽 聽 cDrive = SubStr( cDrive, 1, 2 )
Endif
For each oJob in oJobs
聽 聽 If cDrive == StrToken( oJob:Dependent, 2, '"' )
聽 聽 聽 聽 cDriveNumber = SubStr( StrToken( StrToken( oJob:Antecedent, 2, '"' ), 1, "," ), 7 )
聽 聽 聽 聽 return GetSerial( oSrv, cDriveNumber )
聽 聽 Endif
Next
Return ""
/* *************************** */
/* *************************** */
/* *********************************************************
聽 聽Cerca a partir d'una lletra d'USB el seu n煤mero de s猫rie.
聽 聽------------------------------------------------------ */
STATIC FUNCTION GetSerial( oSrv, cDriveNumber )
Local aDrives := oSrv:ExecQuery( "SELECT * FROM Win32_DiskDrive" )
Local oDrive, cSerial := ""
For each oDrive in aDrives
聽 聽 If oDrive:Name == "\\.\PHYSICALDRIVE" + cDriveNumber .and. ;
聽 聽 聽 聽oDrive:InterfaceType == "USB"
聽 聽 聽 聽 cSerial = oDrive:PNPDeviceID
聽 聽 聽 聽 cSerial = SubStr( cSerial, 1, RAt( "&", cSerial ) - 1 )
聽 聽 聽 聽 cSerial = SubStr( cSerial, RAt( "&", cSerial ) + 1 )
聽 聽 聽 聽 If At( "\", cSerial ) != 0
聽 聽 聽 聽 聽 聽 cSerial = SubStr( cSerial, At( "\", cSerial ) + 1 )
聽 聽 聽 聽 Endif
聽 聽 聽 聽 Return cSerial
聽 聽 Endif
Next
Return cSerial
/* *************************** */
/* *************************** */
/* *****************************************************************************
聽 聽Torna el n煤mero de s猫rie de l'USB escollit per ser la 'motxila' del programa.
聽 聽-------------------------------------------------------------------------- */
FUNCTION cGetSerialProtect()
Local cDriveUSB 聽 聽:= ""
Local oClp 聽 聽 聽 聽 := Nil
If ( cDriveUSB := cSelectUSB( .T. ) ) <> ""
/*
聽 聽 MsgInfo( "Identificadors autoritzats: " + cNumUSB() + CRLF + CRLF + ;
聽 聽 聽 聽 聽 聽 聽 "L'identificador de " + cDriveUSB 聽+ " 茅s: " + GetUSBSerial( cDriveUSB ) + CRLF + CRLF + ;
聽 聽 聽 聽 聽 聽 聽 If( AT( GetUSBSerial( cDriveUSB ), cNumUSB() ) > 0, 聽"SI", 聽"NO") + " est脿 autoritzat.", ;
聽 聽 聽 聽 聽 聽 聽 "A T E N C I 脫 !" )
*/
聽 聽 If MsgYesNo( "Identificadors autoritzats: " + cNumUSB() + CRLF + CRLF + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽"L'identificador de " + cDriveUSB 聽+ " 茅s: " + GetUSBSerial( cDriveUSB ) + CRLF + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽"El vol copiar al ClipBoard?" + CRLF + CRLF + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽If( AT( GetUSBSerial( cDriveUSB ), cNumUSB() ) > 0, 聽"SI", 聽"NO") + " est脿 autoritzat.", ;
聽 聽 聽 聽 聽 聽 聽 聽 聽"ATENCI脫 !" )
聽 聽 聽 聽 DEFINE CLIPBOARD oClp FORMAT TEXT
聽 聽 聽 聽 //cClp = oClp:GetText()
聽 聽 聽 聽 oclp:Open()
聽 聽 聽 聽 Oclp:Empty()
聽 聽 聽 聽 SetClipboardData( 1, GetUSBSerial( cDriveUSB ) )
聽 聽 聽 聽 oClp:End()
聽 聽 EndIf
//If( AT( GetUSBSerial( cDriveUSB ), cNumUSB() ) > 0, 聽aUSB[1] := .T., Nil )
EndIf
Return Nil
/* *************************** */
/* *************************** */
FUNCTION cSelectUSB( lMensaje )
Local aDriveUSB := {}
Local nDriveUSB := 0
Local cDriveUSB := ""
Local nContador := 0
aDriveUSB := aUSBDrive()
If Len( aDriveUSB ) = 0
聽 聽 If lMensaje
聽 聽 聽 聽 MsgAlert( "No hi han unitats USB disponibles, insereixi una unitat USB.", "A T E N C I 脫 !" )
聽 聽 EndIf
聽 Else
聽 聽 //AEval( aDriveUSB, { |DriveUSB| DriveUSB := "&" + DriveUSB } )
聽 聽 For nContador := 1 To Len( aDriveUSB )
聽 聽 聽 聽 aDriveUSB[ nContador ] := "&" + aDriveUSB[ nContador ]
聽 聽 EndFor
聽 聽 nDriveUSB := Alert( "Unitats USB disponibles:", aDriveUSB, "Esculleixi una unitat USB" )
聽 聽 If nDriveUSB > 0
聽 聽 聽 聽 cDriveUSB := SubStr( aDriveUSB[ nDriveUSB ], 2 )
聽 聽 EndIf
EndIf
//Traza(1, "cDriveUSB=", cDriveUSB)
//Traza(1, "aDriveUSB[ nDriveUSB ]=", aDriveUSB[ nDriveUSB ])
Return cDriveUSB
/* *************************** */
/* *************************** */
/* ******************************************
聽 聽Devuelve un CADENA de los USB autorizados.
聽 聽--------------------------------------- */
STATIC FUNCTION cNumUSB()
Local nContador := 0
Local cUSB 聽 聽 聽:= ""
/* El 1er. element 茅s l'indicador de DEMO.
聽 聽----------------------------------- */
For nContador := 2 to Len( aUSB )
聽 聽 cUSB = cUSB + "," + aUSB[ nContador ][1]
EndFor
Return SubStr( cUSB, 2)
/* *************************** */
/* *************************** */
/* ************************************
聽 聽Torna una array dels USB connectats.
聽 聽--------------------------------- */
STATIC FUNCTION aUSBDrive()
Local nBitDrives := GetLogicalDrives()
Local nContador 聽:= 0
Local cDrive 聽 聽 := ""
Local aDrives 聽 聽:= {}
For nContador := 1 to 32
聽 聽 If lAnd( nBitDrives, 2 ^ ( nContador - 1 ) )
聽 聽 聽 聽 cDrive = Chr( Asc( "A" ) - 1 + nContador ) + ":\"
聽 聽 聽 聽 If nContador != 1 .and. GetDriveType( cDrive ) == DRIVE_REMOVABLE
聽 聽 聽 聽 聽 聽 AAdd( aDrives, cDrive )
聽 聽 聽 聽 Endif
聽 聽 Endif
Next
Return aDrives
/* *************************** */