使用SHFormatDrive格式化硬盘
来源:互联网 发布:网络彩票平台出租骗局 编辑:程序博客网 时间:2024/06/17 23:22
从实际应用的角度看,从Windows 9X到Windows NT都应该提供这个FormatDisk函数,否则怎么实现资源管理器窗口的磁盘格式化呢?笔者通过仔细查找发现,Windows 确实提供了这样一个函数,尽管文档资料没有注明,但是如果我们使用View Dependencies打开Shell2.dll,就会发现这个函数。它就是SHFormatDrive,编译连接到Shell32.lib。尽管通过平台的SDK文档和SHELLAPI.H文件并不能发现这个函数,但是Win32各个操作系统都能找到它的踪影。应用程序可以通过调用这个函数实现借用外壳的磁盘格式化对话框格式化磁盘。调用约定如下:
DWORD WINAPI SHFormatDrive(HWND hwnd,UINT drive,UINT fmtID,UINT options);
参数含义如下:
hwnd:拥有对话框的窗口句柄,注意hwnd为NULL时,并不会导致这个对话框生成一个上层的应用程序,也就是说,这个参数总是非NULL,这个对话框只能作为一个窗口的子窗口,而不能作为一个单独的应用程序;
drive:要格式化的驱动器号,以A: == 0为基准,依此类推;
fmtID:必须设定为 SHFMT_ID_DEFAULT,即0xFFFF;
options:可以有两个选择项:SHFMT_OPT_FULL 0x0001和SHFMT_OPT_SYSONLY 0x0002,如果这一项设置为零,系统就要使用快速格式化作为默认设置。
该函数的返回值要么为一个下列以SHFMT_开头的值,要么是一个成功格式化的磁盘标识。
//上次格式化出错,磁盘可能被格式化
#define SHFMT_ERROR 0xFFFFFFFFL
//格式化被取消
#define SHFMT_CANCEL 0xFFFFFFFEL
// 不能进行磁盘格式化
#define SHFMT_NOFORMAT 0xFFFFFFFDL
实现步骤
1.函数接口声明
typedef DWORD (WINAPI *PFNSHFORMATDRIVE)(HWND hwnd,UINT drive,UINT fmtID,UINT options);
2.加载Shell32.dll库
HINSTANCE hInstance=LoadLibrary(_T
(“Shell32.dll”));
If(hInstance==NULL) return;
3.获得函数指针
PFNSHFORMATDRIVE pFnSHFormatDrive=(
PFNSHFORMATDRIVE)GetProcessAddress(_T
(“SHFormatDrive”));
if(pFnSHFormatDrive==NULL)
{
FreeLibrary(hInstance);
return;
}
4.调用函数
(pFnSHFormatDrive)(hwnd,drive/*a:==0*/,fmtID/* SHFMT_ID_DEFAULT */,options);
5.释放句柄资源
FreeLibrary(hInstance);
Return;
本程序在Windows 2000环境下调试通过,使用Delphi、Visual Basic的用户通过调用上述Windows SDK 函数也可以实现磁盘格式化。
/*****************************************************************/
也可以直接调用
DWORD SHFormatDrive( HWND hwnd,
UINT drive,//0 for A:, 2 for C:,...
UINT fmtID,//SHFMT_ID_DEFAULT only
UINT options//SHFMT_OPT_FULL取消快格,进行全格
//SHFMT_OPT_SYSONLY 创建ms-dos启动盘
);
包含在头文件shlobj.h,for2k
尽管很多程序不需要此项操作,但WIN32API确实提供了一个函数用来格式化磁盘
即:
DWORD WINAPI SHFormatDrive(HWND hwnd,
UINT drive,
UINT fmtID,
UINT options);
函数原型可以在SHELLAPI.H文件里面找到,如果没有那么表明你没有更新这个头文件。
但你仍然可以用SHELL32.LIB库提供的函数。
如果SHELLAPI.H有这个函数的原型,你直接包含进来,调用SHFormatDrive即可。
如果没有则按照如下写一个头文件即可(也可以加到SHELLAPI.H文件中):
//Begin
#if !defined(SHFMT_OPT_FULL)
#if defined (__cplusplus)
extern "C " {
#endif
/*****************************************************************
The SHFormatDrive API provides access to the Shell 's format
dialog box. This allows applications that want to format disks to bring
up the same dialog box that the Shell uses for disk formatting.
PARAMETERS
hwnd = The window handle of the window that will own the
dialog. NOTE that hwnd == NULL does not cause this
dialog to come up as a "top level application "
window. This parameter should always be non-null,
this dialog box is only designed to be the child of
another window, not a stand-alone application.
drive = The 0 based (A: == 0) drive number of the drive
to format.
fmtID = Currently must be set to SHFMT_ID_DEFAULT.
options = There are currently only two option bits defined.
SHFMT_OPT_FULL
SHFMT_OPT_SYSONLY
SHFMT_OPT_FULL specifies that the "Quick Format "
setting should be cleared by default. If the user
leaves the "Quick Format " setting cleared, then a
full format will be applied (this is useful for
users that detect "unformatted " disks and want
to bring up the format dialog box).
If options is set to zero (0), then the "Quick Format "
setting is set by default. In addition, if the user leaves
it set, a quick format is performed. Under Windows NT 4.0,
this flag is ignored and the "Quick Format " box is always
checked when the dialog box first appears. The user can
still change it. This is by design.
The SHFMT_OPT_SYSONLY initializes the dialog to
default to just sys the disk.
All other bits are reserved for future expansion
and must be 0.
Please note that this is a bit field and not a
value, treat it accordingly.
RETURN
The return is either one of the SHFMT_* values, or if
the returned DWORD value is not == to one of these
values, then the return is the physical format ID of the
last successful format. The LOWORD of this value can be
passed on subsequent calls as the fmtID parameter to
"format the same type you did last time ".
*****************************************************************/
DWORD WINAPI SHFormatDrive(HWND hwnd,
UINT drive,
UINT fmtID,
UINT options);
//
// Special value of fmtID which means "use the defaultformat "
//
#define SHFMT_ID_DEFAULT 0xFFFF
//
// Option bits for options parameter
//
#define SHFMT_OPT_FULL 0x0001
#define SHFMT_OPT_SYSONLY 0x0002
//
// Special return values. PLEASE NOTE that these are DWORD values.
//
#define SHFMT_ERROR 0xFFFFFFFFL // Error on last format,
// drive may be formatable
#define SHFMT_CANCEL 0xFFFFFFFEL // Last format wascanceled
#define SHFMT_NOFORMAT 0xFFFFFFFDL // Drive is not formatable
#if defined (__cplusplus)
}
#endif
#endif
//End:Begin
调用方式如:
SHFormatDrive (m_hWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
注意:
如果你不希望系统弹出Abort, Retry, or Ignore的询问对话框,可以用SetErrorMode
为(!SEM_FAILCRITICALERRORS),如下:
UINT OldMode = SetErrorMode(0); //得到缺省设置
SetErrorMode(OldMode & !SEM_FAILCRITICALERRORS);//设置处理方式
SHFormatDrive (m_hWnd, 0 /* A: */, SHFMT_ID_DEFAULT, 0);
SetErrorMode(OldMode); // 恢复缺省设置
- 使用SHFormatDrive格式化硬盘
- LINUX使用未格式化的硬盘
- linux 大硬盘格式化挂载使用
- linux 硬盘格式化工具 的使用
- 硬盘格式化
- 格式化硬盘
- 格式化硬盘
- 使用希捷DiscWizard格式化3TB硬盘并分区
- CentOS使用mkfs.ext4快速格式化大容量硬盘
- 硬盘格式化教程
- 格式化硬盘 (.net C#)
- 强制格式化硬盘
- Ubuntu_Ubuntu硬盘格式化
- linux硬盘 格式化 分区
- Linux 硬盘,格式化,分区。
- 加硬盘,格式化,挂载
- Linux 硬盘,格式化,分区
- 硬盘格式化了怎么办
- 谷歌拼音输入法,无候选词时,仅显示输入字符界面的问题
- JSTL学习笔记3-----URL相关标签的使用
- hibernate3和4 usertype区别
- 请讲述heap与stack的差别
- MySQL----int(M)讲解 (int(3)与int(11)的区别)
- 使用SHFormatDrive格式化硬盘
- java反射列子
- StartMediaPadShow
- 内存中的字节对齐
- java flash 安全沙箱
- Android的Handler总结
- centos ssh配置无密码登录
- HttpWatch的指标说明
- 如何在oracle 使用 dump