毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> 开发语言 >> 正文

delphi如何实现程序中安全删除U盘源代码

更新时间:2013-2-17:  来源:毕业论文

delphi如何实现程序中安全删除U盘源代码
就像手动安全移除删除U盘的效果一样,如何在代码中实现呢?

不好做,这个八成得用驱动,
WDK当中的CM_Request_Device_Eject这个函数
不过据说Setupapi.dll里面也有,可以考虑动态导出

开始运行

%windir%\system32\rundll32.exe shell32.dll,Control_RunDLL hotplug.dll

function OpenVolume(ADrive: char): THandle; var  RootName, VolumeName: string;   AccessFlags: DWORD; begin  RootName := ADrive + ':' + #134; // ADrive + ':\' kills the syntax highlighting   case GetDriveType(PChar(RootName)) of    DRIVE_REMOVABLE:       AccessFlags := GENERIC_READ or GENERIC_WRITE;     DRIVE_CDROM:       AccessFlags := GENERIC_READ;   else    Result := INVALID_HANDLE_VALUE;     exit;   end;   VolumeName := Format('\\.\%s:', [ADrive]);   Result := CreateFile(PChar(VolumeName), AccessFlags,     FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);   if Result = INVALID_HANDLE_VALUE then    RaiseLastWin32Error; end;   function LockVolume(AVolumeHandle: THandle): boolean; const  LOCK_TIMEOUT = 10 * 1000; // 10 Seconds   LOCK_RETRIES = 20;   LOCK_SLEEP = LOCK_TIMEOUT div LOCK_RETRIES;   // #define FSCTL_LOCK_VOLUME CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 6, METHOD_BUFFERED, FILE_ANY_ACCESS)   FSCTL_LOCK_VOLUME = (9 shl 16) or (0 shl 14) or (6 shl 2) or 0; var  Retries: integer;   BytesReturned: Cardinal; begin  for Retries := 1 to LOCK_RETRIES do begin    Result := DeviceIoControl(AVolumeHandle, FSCTL_LOCK_VOLUME, nil, 0,       nil, 0, BytesReturned, nil);     if Result then      break;     Sleep(LOCK_SLEEP);   end; end;   function DismountVolume(AVolumeHandle: THandle): boolean; const// #define FSCTL_DISMOUNT_VOLUME CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 8, METHOD_BUFFERED, FILE_ANY_ACCESS)   FSCTL_DISMOUNT_VOLUME = (9 shl 16) or (0 shl 14) or (8 shl 2) or 0; var  BytesReturned: Cardinal; begin  Result := DeviceIoControl(AVolumeHandle, FSCTL_DISMOUNT_VOLUME, nil, 0,     nil, 0, BytesReturned, nil);   if not Result then    RaiseLastWin32Error; end;   function PreventRemovalOfVolume(AVolumeHandle: THandle;   APreventRemoval: boolean): boolean; const// #define IOCTL_STORAGE_MEDIA_REMOVAL CTL_CODE(IOCTL_STORAGE_BASE, 0x0201, METHOD_BUFFERED, FILE_READ_ACCESS)   IOCTL_STORAGE_MEDIA_REMOVAL = ($2d shl 16) or (1 shl 14) or ($201 shl 2) or 0; type  TPreventMediaRemoval = record    PreventMediaRemoval: BOOL;   end; var  BytesReturned: Cardinal;   PMRBuffer: TPreventMediaRemoval; begin  PMRBuffer.PreventMediaRemoval := APreventRemoval;   Result := DeviceIoControl(AVolumeHandle, IOCTL_STORAGE_MEDIA_REMOVAL,     @PMRBuffer, SizeOf(TPreventMediaRemoval), nil, 0, BytesReturned, nil);   if not Result then    RaiseLastWin32Error; end;   function AutoEjectVolume(AVolumeHandle: THandle): boolean; const// #define IOCTL_STORAGE_EJECT_MEDIA CTL_CODE(IOCTL_STORAGE_BASE, 0x0202, METHOD_BUFFERED, FILE_READ_ACCESS)   IOCTL_STORAGE_EJECT_MEDIA = ($2d shl 16) or (1 shl 14) or ($202 shl 2) or 0; var  BytesReturned: Cardinal; begin  Result := DeviceIoControl(AVolumeHandle, IOCTL_STORAGE_EJECT_MEDIA, nil, 0,     nil, 0, BytesReturned, nil);   if not Result then    RaiseLastWin32Error; end;   function EjectVolume(ADrive: char): boolean; var  VolumeHandle: THandle; begin  Result := FALSE;   // Open the volume   VolumeHandle := OpenVolume(ADrive);   if VolumeHandle = INVALID_HANDLE_VALUE then    exit;   try    // Lock and dismount the volume     if LockVolume(VolumeHandle) and DismountVolume(VolumeHandle) then begin      // Set prevent removal to false and eject the volume       if PreventRemovalOfVolume(VolumeHandle, FALSE) then        AutoEjectVolume(VolumeHandle);     end;   finally    // Close the volume so other processes can use the drive     CloseHandle(VolumeHandle);   end; end;

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。