Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "WinAPI";
Текущий архив: 2007.10.21;
Скачать: [xml.tar.bz2];

Вниз

Как отключить диск в системе.   Найти похожие ветки 

 
Still Swamp   (2007-04-16 17:11) [0]

Как отключить (размонтировать) диск в системе. Например мне надо отключить флэш накопитель.


 
SLoW.AlfaMoon.Com   (2007-04-17 10:02) [1]

FSCTL_DISMOUNT_VOLUME

The FSCTL_DISMOUNT_VOLUME control code dismounts a volume.

To perform this operation, call the DeviceIoControl function with the following parameters.

BOOL DeviceIoControl(
 (HANDLE) hDevice,            // handle to volume
 FSCTL_DISMOUNT_VOLUME,       // dwIoControlCode
 NULL,                        // lpInBuffer
 0,                           // nInBufferSize
 NULL,                        // lpOutBuffer
 0,                           // nOutBufferSize
 (LPDWORD) lpBytesReturned,   // number of bytes returned
 (LPOVERLAPPED) lpOverlapped  // OVERLAPPED structure
);

Parameters

hDevice
   [in] A handle to the volume to be dismounted.

   To retrieve a handle, call the CreateFile function.
dwIoControlCode
   [in] The control code for the operation.

   Use FSCTL_DISMOUNT_VOLUME for this operation.
lpInBuffer
   Not used with this operation.

   Set to NULL.
nInBufferSize
   Not used with this operation.

   Set to 0 (zero).
lpOutBuffer
   Not used with this operation.

   Set to NULL.
nOutBufferSize
   Not used with this operation.

   Set to 0 (zero).
lpBytesReturned
   [out] A pointer to a variable that receives the size of the data that is stored in the output buffer, in bytes.

   If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an operation returns no output data and lpOutBuffer is NULL, DeviceIoControl uses lpBytesReturned, which makes the value of lpBytesReturned meaningless.

   If lpOverlapped is not NULL, lpBytesReturned can be NULL. If this parameter is not NULL and the operation returns data, lpBytesReturned is meaningless until the overlapped operation is complete. To retrieve the number of bytes returned, call GetOverlappedResult.

   If hDevice is associated with an I/O completion port, you can retrieve the number of bytes returned by calling GetQueuedCompletionStatus.
lpOverlapped
   [in] A pointer to an OVERLAPPED structure.

   If hDevice is opened without specifying FILE_FLAG_OVERLAPPED, lpOverlapped is ignored.

   If hDevice is opened with the FILE_FLAG_OVERLAPPED flag, the operation is performed as an overlapped (asynchronous) operation, and lpOverlapped must point to a valid OVERLAPPED structure that contains a handle to an event object. Otherwise, the function fails in unpredictable ways.

   For overlapped operations, DeviceIoControl returns immediately, and the event object is signaled when the operation has been completed. Otherwise, the function does not return until the operation has been completed or an error occurs.

Return Value

If the operation completes successfully, DeviceIoControl returns a nonzero value.

If the operation fails or is pending, DeviceIoControl returns 0 (zero). To get extended error information, call GetLastError.
Remarks

The hDevice handle passed to DeviceIoControl must be a handle to a volume, opened for direct access. To retrieve a volume handle, call CreateFile with the lpFileName parameter set to a string of the following form:

\\.\X:

In the preceding string, X is a hard-drive partition letter, floppy disk drive, or CD-ROM drive. The application must also specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags in the dwShareMode parameter of CreateFile.

If the specified volume is locked by another process, the operation fails. To prevent another process from locking the volume, lock it as soon as you open it.

A dismounted volume has the following properties:

   * There are no open files.
   * The operating system does detect the volume.

The operating system tries to mount an unmounted volume as soon as an attempt is made to access it. For example, a call to GetLogicalDrives triggers the operating system to mount unmounted volumes.

Dismounting a volume is useful when a volume needs to disappear for a while. For example, an application that changes a volume file system from the FAT file system to the NTFS file system might use the following procedure.

To change a volume file system

  1. Open a volume.
  2. Lock the volume.
  3. Format the volume.
  4. Dismount the volume.
  5. Unlock the volume.
  6. Close the volume handle.

A dismounting operation removes the volume from the FAT file system awareness. When the operating system mounts the volume, it appears as an NTFS file system volume.
Requirements
Client  Requires Windows Vista, Windows XP, Windows 2000 Professional, or Windows NT Workstation 3.5 and later.
Server  Requires Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, or Windows NT Server 3.5 and later.
Header  

Declared in Winioctl.h.


 
SLoW.AlfaMoon.Com   (2007-04-17 10:04) [2]

А вообще, поиск в заголовочных файлах и MSDN в большинстве случаев дает хотя бы подсказку, в каком направлении двигаться :)


 
Still Swamp   (2007-04-17 10:47) [3]

Спасибо.


 
Still Swamp   (2007-04-18 11:27) [4]

Грабли следующие.
Для размонтирвания диска нужно его сначала залочить. Нечто вроде вот такого

DeviceName:="\\.\F:";
DeviceHandle:=CreateFile(PChar(DeviceName), 0, FILE_SHARE_READ and FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

По идее получил хэндл устройства. Он не нулевой. Далее лочу.

Result:=(DeviceIoControl(AHandle, FSCTL_LOCK_VOLUME, nil, 0, nil, 0, dwBytesReturned, nil));

В хэлпе написано что должны быть все файлы закрыты, диск не занят. Вроде так и есть, однако, тут получаю ошибки разнообразные.

Что может быть не так.


 
umbra ©   (2007-04-18 11:53) [5]


> нужно его сначала залочить.


LockFile


 
Still Swamp   (2007-04-18 12:13) [6]

FSCTL_LOCK_VOLUME - ну типа вот я это и пытаюс сделать


 
SLoW.AlfaMoon.Com   (2007-04-18 16:31) [7]

Получи список хэндлов и посмотри, нет ли среди них хэндлов на твой девайс и файлы на нем. На моем сайте (см. ник) есть сэмпл



Страницы: 1 вся ветка

Форум: "WinAPI";
Текущий архив: 2007.10.21;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.47 MB
Время: 0.047 c
2-1190635457
Alex.Rus
2007-09-24 16:04
2007.10.21
Процессы


1-1186570029
DmitrichJ
2007-08-08 14:47
2007.10.21
Excel: перечисление всех страничек. Как?


1-1186488804
tytus
2007-08-07 16:13
2007.10.21
FastReport 4.1.8 как выбрать кисть для MemoView?


1-1186674079
Lexa11_2002
2007-08-09 19:41
2007.10.21
Как передать формулу в Excel.


2-1191124887
Alex8
2007-09-30 08:01
2007.10.21
свойство "Custom.Constrstraint"





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский