Win32 API 日本語リファレンス
ホームUI.Shell › SHFormatDrive

SHFormatDrive

関数
ドライブのフォーマットダイアログを表示し実行する。
DLLSHELL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

// SHELL32.dll
#include <windows.h>

DWORD SHFormatDrive(
    HWND hwnd,
    DWORD drive,
    SHFMT_ID fmtID,
    DWORD options
);

パラメーター

名前方向説明
hwndHWNDinダイアログ ボックスの親ウィンドウのハンドル。Format ダイアログ ボックスには親ウィンドウが必要です。そのため、このパラメーターを NULL にすることはできません。
driveDWORDinフォーマットするドライブ。このパラメーターの値は、A: ドライブを 0 として始まるドライブ文字を表します。たとえば、値 2 は C: ドライブを表します。
fmtIDSHFMT_IDin

物理フォーマットの ID。現在のところ、次のフラグのみが定義されています。

SHFMT_ID_DEFAULT (0xFFFF)

既定のフォーマット ID。

optionsDWORDin

この値は 0、またはダイアログ ボックスの既定のフォーマット オプションを変更する次の値のいずれかである必要があります。この値はビットフィールドとして扱われるため、それに応じて処理する必要があります。

SHFMT_OPT_FULL (0x0001)

0x001。このフラグが設定されている場合、クイック フォーマット オプションが選択されます。

この関数は、Windows XP with SP1 以降の Shlobj.h にのみ含まれています。

Windows XP: Windows XP with SP1 より前では、この関数は Shell32.lib を通じてアクセスできます。

SHFMT_OPT_SYSONLY (0x0002)

0x002。MS-DOS の起動ディスクを作成する オプションを選択し、システム起動ディスクを作成します。

戻り値の型: DWORD

公式ドキュメント

SHFormatDrive は変更されるか、利用できなくなる可能性があります。

戻り値

型: DWORD

最後に成功したフォーマットのフォーマット ID、または次の値のいずれかを返します。この値の LOWORD は、最後のフォーマットを繰り返すために、後続の呼び出しで fmtID パラメーターとして渡すことができます。

戻り値 説明
SHFMT_ERROR
最後のフォーマット中にエラーが発生しました。これは、ドライブがフォーマット不可能であることを示すものではありません。
SHFMT_CANCEL
最後のフォーマットがキャンセルされました。
SHFMT_NOFORMAT
ドライブをフォーマットできません。

解説(Remarks)

フォーマットはダイアログ ボックスのインターフェイスによって制御されます。つまり、実際にフォーマットを開始するにはユーザーが OK ボタンをクリックする必要があり、プログラムからフォーマットを開始することはできません。

次の SHFormatDrive の呼び出しは、ドライブ A 内のディスクに対して既定のフォーマット オプションが選択された状態で、シェルの Format ダイアログ ボックスを表示します。

SHFormatDrive(hMainWnd, 0, SHFMT_ID_DEFAULT, 0);
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// SHELL32.dll
#include <windows.h>

DWORD SHFormatDrive(
    HWND hwnd,
    DWORD drive,
    SHFMT_ID fmtID,
    DWORD options
);
[DllImport("SHELL32.dll", ExactSpelling = true)]
static extern uint SHFormatDrive(
    IntPtr hwnd,   // HWND
    uint drive,   // DWORD
    uint fmtID,   // SHFMT_ID
    uint options   // DWORD
);
<DllImport("SHELL32.dll", ExactSpelling:=True)>
Public Shared Function SHFormatDrive(
    hwnd As IntPtr,   ' HWND
    drive As UInteger,   ' DWORD
    fmtID As UInteger,   ' SHFMT_ID
    options As UInteger   ' DWORD
) As UInteger
End Function
' hwnd : HWND
' drive : DWORD
' fmtID : SHFMT_ID
' options : DWORD
Declare PtrSafe Function SHFormatDrive Lib "shell32" ( _
    ByVal hwnd As LongPtr, _
    ByVal drive As Long, _
    ByVal fmtID As Long, _
    ByVal options As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SHFormatDrive = ctypes.windll.shell32.SHFormatDrive
SHFormatDrive.restype = wintypes.DWORD
SHFormatDrive.argtypes = [
    wintypes.HANDLE,  # hwnd : HWND
    wintypes.DWORD,  # drive : DWORD
    wintypes.DWORD,  # fmtID : SHFMT_ID
    wintypes.DWORD,  # options : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHELL32.dll')
SHFormatDrive = Fiddle::Function.new(
  lib['SHFormatDrive'],
  [
    Fiddle::TYPE_VOIDP,  # hwnd : HWND
    -Fiddle::TYPE_INT,  # drive : DWORD
    -Fiddle::TYPE_INT,  # fmtID : SHFMT_ID
    -Fiddle::TYPE_INT,  # options : DWORD
  ],
  -Fiddle::TYPE_INT)
#[link(name = "shell32")]
extern "system" {
    fn SHFormatDrive(
        hwnd: *mut core::ffi::c_void,  // HWND
        drive: u32,  // DWORD
        fmtID: u32,  // SHFMT_ID
        options: u32  // DWORD
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHELL32.dll")]
public static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint options);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHELL32_SHFormatDrive' -Namespace Win32 -PassThru
# $api::SHFormatDrive(hwnd, drive, fmtID, options)
#uselib "SHELL32.dll"
#func global SHFormatDrive "SHFormatDrive" sptr, sptr, sptr, sptr
; SHFormatDrive hwnd, drive, fmtID, options   ; 戻り値は stat
; hwnd : HWND -> "sptr"
; drive : DWORD -> "sptr"
; fmtID : SHFMT_ID -> "sptr"
; options : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "SHELL32.dll"
#cfunc global SHFormatDrive "SHFormatDrive" sptr, int, int, int
; res = SHFormatDrive(hwnd, drive, fmtID, options)
; hwnd : HWND -> "sptr"
; drive : DWORD -> "int"
; fmtID : SHFMT_ID -> "int"
; options : DWORD -> "int"
; DWORD SHFormatDrive(HWND hwnd, DWORD drive, SHFMT_ID fmtID, DWORD options)
#uselib "SHELL32.dll"
#cfunc global SHFormatDrive "SHFormatDrive" intptr, int, int, int
; res = SHFormatDrive(hwnd, drive, fmtID, options)
; hwnd : HWND -> "intptr"
; drive : DWORD -> "int"
; fmtID : SHFMT_ID -> "int"
; options : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shell32 = windows.NewLazySystemDLL("SHELL32.dll")
	procSHFormatDrive = shell32.NewProc("SHFormatDrive")
)

// hwnd (HWND), drive (DWORD), fmtID (SHFMT_ID), options (DWORD)
r1, _, err := procSHFormatDrive.Call(
	uintptr(hwnd),
	uintptr(drive),
	uintptr(fmtID),
	uintptr(options),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function SHFormatDrive(
  hwnd: THandle;   // HWND
  drive: DWORD;   // DWORD
  fmtID: DWORD;   // SHFMT_ID
  options: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHELL32.dll' name 'SHFormatDrive';
result := DllCall("SHELL32\SHFormatDrive"
    , "Ptr", hwnd   ; HWND
    , "UInt", drive   ; DWORD
    , "UInt", fmtID   ; SHFMT_ID
    , "UInt", options   ; DWORD
    , "UInt")   ; return: DWORD
●SHFormatDrive(hwnd, drive, fmtID, options) = DLL("SHELL32.dll", "dword SHFormatDrive(void*, dword, dword, dword)")
# 呼び出し: SHFormatDrive(hwnd, drive, fmtID, options)
# hwnd : HWND -> "void*"
# drive : DWORD -> "dword"
# fmtID : SHFMT_ID -> "dword"
# options : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shell32" fn SHFormatDrive(
    hwnd: ?*anyopaque, // HWND
    drive: u32, // DWORD
    fmtID: u32, // SHFMT_ID
    options: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc SHFormatDrive(
    hwnd: pointer,  # HWND
    drive: uint32,  # DWORD
    fmtID: uint32,  # SHFMT_ID
    options: uint32  # DWORD
): uint32 {.importc: "SHFormatDrive", stdcall, dynlib: "SHELL32.dll".}
pragma(lib, "shell32");
extern(Windows)
uint SHFormatDrive(
    void* hwnd,   // HWND
    uint drive,   // DWORD
    uint fmtID,   // SHFMT_ID
    uint options   // DWORD
);
ccall((:SHFormatDrive, "SHELL32.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, UInt32, UInt32, UInt32),
      hwnd, drive, fmtID, options)
# hwnd : HWND -> Ptr{Cvoid}
# drive : DWORD -> UInt32
# fmtID : SHFMT_ID -> UInt32
# options : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t SHFormatDrive(
    void* hwnd,
    uint32_t drive,
    uint32_t fmtID,
    uint32_t options);
]]
local shell32 = ffi.load("shell32")
-- shell32.SHFormatDrive(hwnd, drive, fmtID, options)
-- hwnd : HWND
-- drive : DWORD
-- fmtID : SHFMT_ID
-- options : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHELL32.dll');
const SHFormatDrive = lib.func('__stdcall', 'SHFormatDrive', 'uint32_t', ['void *', 'uint32_t', 'uint32_t', 'uint32_t']);
// SHFormatDrive(hwnd, drive, fmtID, options)
// hwnd : HWND -> 'void *'
// drive : DWORD -> 'uint32_t'
// fmtID : SHFMT_ID -> 'uint32_t'
// options : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHELL32.dll", {
  SHFormatDrive: { parameters: ["pointer", "u32", "u32", "u32"], result: "u32" },
});
// lib.symbols.SHFormatDrive(hwnd, drive, fmtID, options)
// hwnd : HWND -> "pointer"
// drive : DWORD -> "u32"
// fmtID : SHFMT_ID -> "u32"
// options : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t SHFormatDrive(
    void* hwnd,
    uint32_t drive,
    uint32_t fmtID,
    uint32_t options);
C, "SHELL32.dll");
// $ffi->SHFormatDrive(hwnd, drive, fmtID, options);
// hwnd : HWND
// drive : DWORD
// fmtID : SHFMT_ID
// options : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Shell32 extends StdCallLibrary {
    Shell32 INSTANCE = Native.load("shell32", Shell32.class);
    int SHFormatDrive(
        Pointer hwnd,   // HWND
        int drive,   // DWORD
        int fmtID,   // SHFMT_ID
        int options   // DWORD
    );
}
@[Link("shell32")]
lib LibSHELL32
  fun SHFormatDrive = SHFormatDrive(
    hwnd : Void*,   # HWND
    drive : UInt32,   # DWORD
    fmtID : UInt32,   # SHFMT_ID
    options : UInt32   # DWORD
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SHFormatDriveNative = Uint32 Function(Pointer<Void>, Uint32, Uint32, Uint32);
typedef SHFormatDriveDart = int Function(Pointer<Void>, int, int, int);
final SHFormatDrive = DynamicLibrary.open('SHELL32.dll')
    .lookupFunction<SHFormatDriveNative, SHFormatDriveDart>('SHFormatDrive');
// hwnd : HWND -> Pointer<Void>
// drive : DWORD -> Uint32
// fmtID : SHFMT_ID -> Uint32
// options : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SHFormatDrive(
  hwnd: THandle;   // HWND
  drive: DWORD;   // DWORD
  fmtID: DWORD;   // SHFMT_ID
  options: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHELL32.dll' name 'SHFormatDrive';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SHFormatDrive"
  c_SHFormatDrive :: Ptr () -> Word32 -> Word32 -> Word32 -> IO Word32
-- hwnd : HWND -> Ptr ()
-- drive : DWORD -> Word32
-- fmtID : SHFMT_ID -> Word32
-- options : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shformatdrive =
  foreign "SHFormatDrive"
    ((ptr void) @-> uint32_t @-> uint32_t @-> uint32_t @-> returning uint32_t)
(* hwnd : HWND -> (ptr void) *)
(* drive : DWORD -> uint32_t *)
(* fmtID : SHFMT_ID -> uint32_t *)
(* options : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shell32 (t "SHELL32.dll"))
(cffi:use-foreign-library shell32)

(cffi:defcfun ("SHFormatDrive" shformat-drive :convention :stdcall) :uint32
  (hwnd :pointer)   ; HWND
  (drive :uint32)   ; DWORD
  (fmt-id :uint32)   ; SHFMT_ID
  (options :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHFormatDrive = Win32::API::More->new('SHELL32',
    'DWORD SHFormatDrive(HANDLE hwnd, DWORD drive, DWORD fmtID, DWORD options)');
# my $ret = $SHFormatDrive->Call($hwnd, $drive, $fmtID, $options);
# hwnd : HWND -> HANDLE
# drive : DWORD -> DWORD
# fmtID : SHFMT_ID -> DWORD
# options : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型