取CPU序列號,獲取網卡,取硬盤系列號,獲取目錄下的文件,強制刪除目錄

[delphi]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. unit Unit1;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, StdCtrls;  
  8.   
  9. type  
  10.   TForm1 = class(TForm)  
  11.     Button1: TButton;  
  12.     Button2: TButton;  
  13.     procedure Button1Click(Sender: TObject);  
  14.     procedure Button2Click(Sender: TObject);  
  15.   private  
  16.     { Private declarations }  
  17.   public  
  18.     { Public declarations }  
  19.   end;  
  20.   TFileItem = class(TCollectionItem)  
  21.   public  
  22.     FileName: WideString;  
  23.     FileSize: Int64;  
  24.     IsDirectory: Boolean;  
  25.   end;  
  26.   
  27. var  
  28.   Form1: TForm1;  
  29.   
  30. implementation  
  31.   
  32. {$R *.dfm}  
  33.   
  34. //------ 取CPU序列號 uses WinSock  
  35. function GetCPUID: string;  
  36.   procedure SetCPU(Handle: THandle; CPUNO: Integer);  
  37.   var  
  38.     ProcessAffinity: Cardinal;  
  39.     _SystemAffinity: Cardinal;  
  40.   begin  
  41.     GetProcessAffinityMask(handle, ProcessAffinity, _SystemAffinity);  
  42.     ProcessAffinity := CPUNO;  
  43.     SetProcessAffinityMask(handle, ProcessAffinity);  
  44.   end;  
  45.   
  46. const  
  47.   CPUINFO = '%.8x-%.8x-%.8x-%.8x';  
  48. var  
  49.   iEax: Integer;  
  50.   iEbx: Integer;  
  51.   iEcx: Integer;  
  52.   iEdx: Integer;  
  53. begin  
  54.   SetCPU(GetCurrentProcess, 1);  
  55.   asm  
  56.     push ebx  
  57.     push ecx  
  58.     push edx  
  59.     mov   eax, 1  
  60.     DW $A20F//cpuid  
  61.     mov   iEax, eax  
  62.     mov   iEbx, ebx  
  63.     mov   iEcx, ecx  
  64.     mov   iEdx, edx  
  65.     pop edx  
  66.     pop ecx  
  67.     pop ebx  
  68.   end;  
  69.   Result := Format(CPUINFO, [iEax, iEbx, iEcx, iEdx]);  
  70. end;  
  71. //獲取網卡  
  72. function MacAddress: string;  
  73. var  
  74.  Lib: Cardinal;  
  75.  Func : function(GUID: PGUID): Longint; stdcall;  
  76.  GUID1, GUID2: TGUID;  
  77. begin  
  78.  Result := '';  
  79.  Lib := LoadLibrary('rpcrt4.dll');  
  80.  if Lib <> then  
  81.  begin  
  82.    if Win32Platform <>VER_PLATFORM_WIN32_NT then  
  83.      @Func := GetProcAddress(Lib, 'UuidCreate')  
  84.      else @Func := GetProcAddress(Lib, 'UuidCreateSequential');  
  85.    if Assigned(Func) then  
  86.    begin  
  87.      if (Func(@GUID1) = 0) and  
  88.        (Func(@GUID2) = 0) and  
  89.        (GUID1.D4[2] = GUID2.D4[2]) and  
  90.        (GUID1.D4[3] = GUID2.D4[3]) and  
  91.        (GUID1.D4[4] = GUID2.D4[4]) and  
  92.        (GUID1.D4[5] = GUID2.D4[5]) and  
  93.        (GUID1.D4[6] = GUID2.D4[6]) and  
  94.        (GUID1.D4[7] = GUID2.D4[7]) then  
  95.      begin  
  96.        Result :=  
  97.         IntToHex(GUID1.D4[2], 2) + '-' +  
  98.         IntToHex(GUID1.D4[3], 2) + '-' +  
  99.         IntToHex(GUID1.D4[4], 2) + '-' +  
  100.         IntToHex(GUID1.D4[5], 2) + '-' +  
  101.         IntToHex(GUID1.D4[6], 2) + '-' +  
  102.         IntToHex(GUID1.D4[7], 2);  
  103.      end;  
  104.    end;  
  105.    FreeLibrary(Lib);  
  106.  end;  
  107. end;  
  108.   
  109. //取硬盤系列號:  
  110. function GetIdeSerialNumber: Pansichar; //獲取硬盤的出廠系列號;  
  111. const IDENTIFY_BUFFER_SIZE = 512;  
  112. type  
  113.   TIDERegs = packed record  
  114.   bFeaturesReg: BYTE;  
  115.   bSectorCountReg: BYTE;  
  116.   bSectorNumberReg: BYTE;  
  117.   bCylLowReg: BYTE;  
  118.   bCylHighReg: BYTE;  
  119.   bDriveHeadReg: BYTE;  
  120.   bCommandReg: BYTE;  
  121.   bReserved: BYTE;  
  122.   end;  
  123.   TSendCmdInParams = packed record  
  124.   cBufferSize: DWORD;  
  125.   irDriveRegs: TIDERegs;  
  126.   bDriveNumber: BYTE;  
  127.   bReserved: array[0..2] of Byte;  
  128.   dwReserved: array[0..3] of DWORD;  
  129.   bBuffer: array[0..0] of Byte;  
  130.   end;  
  131.   TIdSector = packed record  
  132.   wGenConfig: Word;  
  133.   wNumCyls: Word;  
  134.   wReserved: Word;  
  135.   wNumHeads: Word;  
  136.   wBytesPerTrack: Word;  
  137.   wBytesPerSector: Word;  
  138.   wSectorsPerTrack: Word;  
  139.   wVendorUnique: array[0..2] of Word;  
  140.   sSerialNumber: array[0..19] of CHAR;  
  141.   wBufferType: Word;  
  142.   wBufferSize: Word;  
  143.   wECCSize: Word;  
  144.   sFirmwareRev: array[0..7] of Char;  
  145.   sModelNumber: array[0..39] of Char;  
  146.   wMoreVendorUnique: Word;  
  147.   wDoubleWordIO: Word;  
  148.   wCapabilities: Word;  
  149.   wReserved1: Word;  
  150.   wPIOTiming: Word;  
  151.   wDMATiming: Word;  
  152.   wBS: Word;  
  153.   wNumCurrentCyls: Word;  
  154.   wNumCurrentHeads: Word;  
  155.   wNumCurrentSectorsPerTrack: Word;  
  156.   ulCurrentSectorCapacity: DWORD;  
  157.   wMultSectorStuff: Word;  
  158.   ulTotalAddressableSectors: DWORD;  
  159.   wSingleWordDMA: Word;  
  160.   wMultiWordDMA: Word;  
  161.   bReserved: array[0..127] of BYTE;  
  162.   end;  
  163.   PIdSector = ^TIdSector;  
  164.   TDriverStatus = packed record  
  165.   bDriverError: Byte;  
  166.   bIDEStatus: Byte;  
  167.   bReserved: array[0..1] of Byte;  
  168.   dwReserved: array[0..1] of DWORD;  
  169.   end;  
  170.   TSendCmdOutParams = packed record  
  171.   cBufferSize: DWORD;  
  172.   DriverStatus: TDriverStatus;  
  173.   bBuffer: array[0..0] of BYTE;  
  174.   end;  
  175. var  
  176.   hDevice: Thandle;  
  177.   cbBytesReturned: DWORD;  
  178.   SCIP: TSendCmdInParams;  
  179.   aIdOutCmd: array[0..(SizeOf(TSendCmdOutParams) + IDENTIFY_BUFFER_SIZE-1)-1] of Byte;  
  180.   IdOutCmd: TSendCmdOutParams absolute aIdOutCmd;  
  181. procedure ChangeByteOrder(var Data; Size: Integer);//函數中的過程  
  182. var  
  183.   ptr: Pchar;  
  184.   i: Integer;  
  185.   c: Char;  
  186. begin  
  187.   ptr := @Data;  
  188.   for I := to (Size shr 1) - do begin  
  189.   c := ptr^;  
  190.   ptr^ := (ptr + 1)^;  
  191.   (ptr + 1)^ := c;  
  192.   Inc(ptr, 2);  
  193.   end;  
  194. end;  
  195. begin //函數主體  
  196.   Result := '';  
  197.   if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then  
  198.   begin // Windows NT, Windows 2000  
  199.   hDevice := CreateFile('\\.\PhysicalDrive0', GENERIC_READ or GENERIC_WRITE,  
  200.   FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);  
  201.   end  
  202.   else // Version Windows 95 OSR2, Windows 98  
  203.   hDevice := CreateFile('\\.\SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0);  
  204.   if hDevice = INVALID_HANDLE_VALUE then Exit;  
  205.   try  
  206.   FillChar(SCIP, SizeOf(TSendCmdInParams) - 1, #0);  
  207.   FillChar(aIdOutCmd, SizeOf(aIdOutCmd), #0);  
  208.   cbBytesReturned := 0;  
  209.   with SCIP do  
  210.   begin  
  211.   cBufferSize := IDENTIFY_BUFFER_SIZE;  
  212.   with irDriveRegs do  
  213.   begin  
  214.   bSectorCountReg := 1;  
  215.   bSectorNumberReg := 1;  
  216.   bDriveHeadReg := $A0;  
  217.   bCommandReg := $EC;  
  218.   end;  
  219.   end;  
  220.   if not DeviceIoControl(hDevice, $0007C088, @SCIP, SizeOf(TSendCmdInParams) - 1,@aIdOutCmd, SizeOf(aIdOutCmd), cbBytesReturned, nil) then Exit;  
  221.   finally  
  222.   CloseHandle(hDevice);  
  223.   end;  
  224.   with PIdSector(@IdOutCmd.bBuffer)^ do  
  225.   begin  
  226.   ChangeByteOrder(sSerialNumber, SizeOf(sSerialNumber));  
  227.   (Pchar(@sSerialNumber) + SizeOf(sSerialNumber))^:= #0;  
  228.   Result := PAnsichar(@sSerialNumber);  
  229.   end;  
  230. end;  
  231. //獲取目錄下的文件  
  232. procedure FindAllFiles(APath: WideString; AFiles: TCollection;  
  233.   var AFileSize: Int64);  
  234. var  
  235.   strSearchPath: WideString;  
  236.   strSafePath: WideString;  
  237.   FindData: WIN32_FIND_DATAW;  
  238.   hFind: THandle;  
  239.   objItem: TFileItem;  
  240. begin  
  241.   strSafePath := Trim(APath);  
  242.   if strSafePath[Length(strSafePath)] <> '\' then strSafePath := strSafePath + '\';  
  243.   strSearchPath := strSafePath + '*.*';  
  244.   
  245.   hFind := FindFirstFileW(PWideChar(strSearchPath), FindData);  
  246.   if (INVALID_HANDLE_VALUE = hFind) then Exit;  
  247.   
  248.   while True do  
  249.   begin  
  250.     if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY then  
  251.     begin  
  252.       if(FindData.cFileName[0] <> '.') then  
  253.       begin  
  254.         objItem := TFileItem(AFiles.Add());  
  255.         objItem.FileName := strSafePath + FindData.cFileName;  
  256.         objItem.FileSize := 0;  
  257.         objItem.IsDirectory := True;  
  258.   
  259.         FindAllFiles(strSafePath + FindData.cFileName, AFiles, AFileSize);  
  260.       end;  
  261.     end  
  262.     else  
  263.     begin  
  264.       objItem := TFileItem(AFiles.Add());  
  265.       objItem.FileName := strSafePath + FindData.cFileName;  
  266.       objItem.FileSize := FindData.nFileSizeLow or FindData.nFileSizeHigh shl SizeOf(FindData.nFileSizeHigh);  
  267.       objItem.IsDirectory := False;  
  268.   
  269.       AFileSize := AFileSize + objItem.FileSize;  
  270.     end;  
  271.     if (not FindNextFileW(hFind, FindData)) then Break;  
  272.   end;  
  273.   Windows.FindClose(hFind);  
  274. end;  
  275.  //強制刪除目錄  
  276. function ForceToRemoveDir(ADir: string): Boolean;  
  277. var  
  278.   pDir: PChar;  
  279.   SR: TSearchRec;  
  280.   FR: Integer;  
  281. begin  
  282.   Result := False;  
  283.   pDir := PChar(ADir);  
  284.   if not DirectoryExists(pDir) then Exit;  
  285.   try  
  286.     if Copy(pDir, Length(pDir), 1) <> '\' then  
  287.       pDir := PChar(pDir + '\');  
  288.     FR := FindFirst(pDir + '*.*', FaAnyfile, SR);  
  289.     while FR = do  
  290.     begin  
  291.       if ((SR.Attr and FaDirectory) = FaDirectory) and  
  292.         (SR.Name <> '.') and (SR.Name <> '..') then  
  293.       begin  
  294.         if not ForceToRemoveDir(StrPas(pDir) + SR.Name) then Break;  
  295.       end;  
  296.       if ((SR.Attr and FaDirectory <> FaDirectory) and  
  297.         (SR.Attr and FaVolumeID <> FaVolumeID)) then  
  298.       begin  
  299.         SysUtils.FileSetAttr(pDir + SR.Name,  
  300.           SysUtils.FileGetAttr(pDir + SR.Name) and (not  
  301.           SysUtils.faReadOnly)); //取消文件的只讀屬性  
  302.         if not DeleteFile(PChar(pDir + SR.Name)) then  
  303.           Break;  
  304.       end;  
  305.       FR := FindNext(SR);  
  306.     end;  
  307.     SysUtils.FindClose(SR);  
  308.     RemoveDirectory(pDir);  
  309.     Result := True;  
  310.   except  
  311.   end;  
  312. end;  
  313. //獲取windows系統版本  
  314. function GetWindowsVersion: string;  
  315. var  
  316.   AWin32Version: Extended;  
  317.   os: string;  
  318. begin  
  319.   os := 'Windows ';  
  320.   AWin32Version := StrtoFloat(format('%d.%d' ,[Win32MajorVersion, Win32MinorVersion]));  
  321.   if Win32Platform = VER_PLATFORM_WIN32s then  
  322.     Result := os + '32'  
  323.   else if Win32Platform=VER_PLATFORM_WIN32_WINDOWS then  
  324.   begin  
  325.     if AWin32Version=4.0 then  
  326.       Result := os + '95'  
  327.     else if AWin32Version=4.1 then  
  328.       Result := os + '98'  
  329.     else if AWin32Version=4.9 then  
  330.       Result := os + 'Me'  
  331.     else  
  332.       Result := os + '9x'  
  333.   end  
  334.   else if Win32Platform = VER_PLATFORM_WIN32_NT then  
  335.   begin  
  336.     if AWin32Version=3.51 then  
  337.       Result := os + 'NT 3.51'  
  338.     else if AWin32Version=4.0 then  
  339.       Result := os + 'NT 4.0'  
  340.     else if AWin32Version=5.0 then  
  341.       Result := os + '2000'  
  342.     else if AWin32Version=5.1 then  
  343.       Result := os + 'XP'  
  344.     else if AWin32Version=5.2 then  
  345.       Result := os + '2003'  
  346.     else if AWin32Version=6.0 then  
  347.       Result := os + 'Vista'  
  348.     else if AWin32Version=6.1 then  
  349.       Result := os + '7'  
  350.     else  
  351.       Result := os ;  
  352.   end  
  353.   else  
  354.     Result := os + '??';  
  355. end;  
  356.   
  357. procedure TForm1.Button1Click(Sender: TObject);  
  358. var  
  359.   i:integer;  
  360. begin  
  361.   showmessage(MacAddress());  
  362.   showmessage(GetCPUID());  
  363.   showmessage(GetIdeSerialNumber());  
  364.   showmessage(GetWindowsVersion());  
  365. end;  
  366.   
  367. procedure TForm1.Button2Click(Sender: TObject);  
  368. var  
  369.   aFiles: TCollection;  
  370.   aFileSize: Int64;  
  371. begin  
  372.   //FindAllFiles('C:\\apache-tomcat-6.0.32',aFiles,aFileSize);  
  373.   //showmessage(inttostr(aFileSize));  
  374.   ForceToRemoveDir('C:\apache-tomcat-6.0.32');  
  375.   showmessage('刪除目錄成功!');  
  376. end;  
  377.   
  378. end.  

http://blog.csdn.net/earbao/article/details/19629579apache

相關文章
相關標籤/搜索