应该清楚的是,在安装卷时获取文件句柄是微不足道的。
当卷被强制卸载时会发生什么?所有文件句柄都变为无效。尝试使用它们会返回错误。
这些代码片段强制卸载卷,以便后续代码可以对其进行直接 i/o。这是我几年前写的一个磁盘清理实用程序(一种大众消费产品)的摘录。
char fn [20];
sprintf (fn, "\\\\.\\%s:", vol -> GetVolName ());
vol_lock_handle = CreateFile (fn, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS,
NULL);
fprintf (stderr,
"Warning: volume dismount will disrupt or kill all processes with open files!\n"
"Before confirming, verify no critical processes have open files on volume %s:\n"
" Are you sure you want to dismount this volume? ('YES' to proceed)? ",
g_vol -> GetVolName ());
char buf [30];
if (!fgets (buf, sizeof buf, stdin) || stricmp (buf, "yes\n"))
{
fprintf (stderr, " Volume dismount not confirmed--canceled.\n");
continue;
}
DWORD status;
if (!DeviceIoControl (vol_lock_handle, FSCTL_DISMOUNT_VOLUME,
NULL, 0, NULL, 0, &status, NULL))
{
DWORD err = GetLastError ();
fprintf (stderr, "Error %d attempting to dismount volume: %s\n",
err, w32errtxt (err));
}
我完全知道这段代码看起来有多么错误。获取 GENERIC_READ 句柄用于卸载,然后获取卷锁,然后写入。有用!