DiUninstallDriverA
関数シグネチャ
// newdev.dll (ANSI / -A)
#include <windows.h>
BOOL DiUninstallDriverA(
HWND hwndParent, // optional
LPCSTR InfPath,
DIUNINSTALLDRIVER_FLAGS Flags,
BOOL* NeedReboot // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hwndParent | HWND | inoptional | DiUninstallDriver がドライバーのアンインストールに関連するユーザーインターフェイス要素を表示するために使用する、トップレベルウィンドウへのハンドル。このパラメーターは省略可能であり、NULL を設定できます。 |
| InfPath | LPCSTR | in | ドライバーパッケージの INF ファイルの完全修飾パスを指定する、NULL 終端文字列へのポインター。 |
| Flags | DIUNINSTALLDRIVER_FLAGS | in | DWORD 型の値で、次のフラグのうち 0 個または 1 個以上を指定します: DIURFLAG_NO_REMOVE_INF。通常、このフラグには 0 を設定します。 このフラグが 0 の場合、DiUninstallDriver は、ドライバーパッケージがインストールされているすべてのデバイスに対して、一致する別のドライバーパッケージ (利用可能な場合)、または他に一致するドライバーパッケージが無い場合は null ドライバーをインストールすることで、そのドライバーパッケージを削除します。ただし、このフラグに DIURFLAG_NO_REMOVE_INF が設定されている場合、DiUninstallDriver は、ドライバーパッケージがインストールされているすべてのデバイスからそのドライバーパッケージを削除しますが、ドライバーストアからは削除しません。 注意: ドライバーパッケージのアンインストールを強制すると、より互換性の高いドライバーパッケージや新しいドライバーパッケージが、互換性の低いドライバーや古いドライバーに置き換えられる可能性があります。
Windows がデバイスのドライバーパッケージを選択する方法については、How Windows Selects Drivers を参照してください。 |
| NeedReboot | BOOL* | outoptional | アンインストールを完了するためにシステムの再起動が必要かどうかを示すために DiUninstallDriver が設定する、BOOL 型の値へのポインター。このパラメーターは省略可能であり、NULL にできます。このパラメーターが指定されていて、アンインストールの完了にシステムの再起動が必要な場合、DiUninstallDriver はこの値を TRUE に設定します。この場合、呼び出し元はシステムを再起動するようユーザーに促す必要があります。このパラメーターが指定されていて、アンインストールの完了にシステムの再起動が不要な場合、DiUninstallDriver はこの値を FALSE に設定します。このパラメーターが NULL で、アンインストールの完了にシステムの再起動が必要な場合、DiUninstallDriver はシステムの再起動を求めるダイアログボックスを表示します。このパラメーターの詳細については、後述の「Remarks」セクションを参照してください。 |
戻り値の型: BOOL
公式ドキュメント
DiUninstallDriver 関数は、ドライバーパッケージがインストールされているすべてのデバイスに対して、一致する別のドライバーパッケージ (利用可能な場合)、または他に一致するドライバーパッケージが無い場合は null ドライバーをインストールすることで、そのドライバーパッケージを削除します。その後、指定されたドライバーパッケージがドライバーストアから削除されます。(ANSI)
戻り値
DiUninstallDriver は、ドライバーパッケージがインストールされているすべてのデバイスから正常に削除され、かつシステムのドライバーストアからも正常に削除された場合に TRUE を返します。ドライバーパッケージがドライバーストアから正常にアンインストールされなかった場合、DiUninstallDriver は FALSE を返し、記録されたエラーは GetLastError を呼び出すことで取得できます。GetLastError が返す可能性のある比較的一般的なエラー値は次のとおりです。
| 戻り値 | 説明 |
|---|---|
| 呼び出し元が Administrator 特権を持っていません。既定では、ドライバーストアからドライバーパッケージをアンインストールするには、呼び出し元が Administrator 特権を持っている必要があります。 | |
| 指定された INF ファイルのパスが存在しません。 | |
| Flags に指定された値が 0 でも DIURFLAG_NO_REMOVE_INF でもありません。 | |
| 呼び出し元のアプリケーションが 32 ビットアプリケーションであり、64 ビット環境で実行しようとしていますが、これは許可されていません。詳細については、Installing Devices on 64-Bit Systems を参照してください。 |
解説(Remarks)
一般に、アンインストール用アプリケーションでは NeedReboot に NULL を設定し、削除の完了に再起動が必要な場合に DiUninstallDriver がシステムの再起動をユーザーに促すようにするべきです。アプリケーションが NeedReboot ポインターを指定するのは、次の場合のみにするべきです。
- アンインストールを完了するために、アプリケーションが DiUninstallDriver を複数回呼び出す必要がある場合。この場合、アプリケーションは DiUninstallDriver の呼び出しのいずれかで NeedReboot の値として TRUE が返されたかどうかを記録し、返されていた場合は、最後の DiUninstallDriver の呼び出しが返った後にシステムを再起動するようユーザーに促すべきです。
- システムの再起動が行われる前に、アプリケーションが DiUninstallDriver の呼び出し以外の必要な処理を実行しなければならない場合。システムの再起動が必要な場合、アプリケーションは必要な処理を完了してから、システムを再起動するようユーザーに促すべきです。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// newdev.dll (ANSI / -A)
#include <windows.h>
BOOL DiUninstallDriverA(
HWND hwndParent, // optional
LPCSTR InfPath,
DIUNINSTALLDRIVER_FLAGS Flags,
BOOL* NeedReboot // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("newdev.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool DiUninstallDriverA(
IntPtr hwndParent, // HWND optional
[MarshalAs(UnmanagedType.LPStr)] string InfPath, // LPCSTR
uint Flags, // DIUNINSTALLDRIVER_FLAGS
IntPtr NeedReboot // BOOL* optional, out
);<DllImport("newdev.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function DiUninstallDriverA(
hwndParent As IntPtr, ' HWND optional
<MarshalAs(UnmanagedType.LPStr)> InfPath As String, ' LPCSTR
Flags As UInteger, ' DIUNINSTALLDRIVER_FLAGS
NeedReboot As IntPtr ' BOOL* optional, out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hwndParent : HWND optional
' InfPath : LPCSTR
' Flags : DIUNINSTALLDRIVER_FLAGS
' NeedReboot : BOOL* optional, out
Declare PtrSafe Function DiUninstallDriverA Lib "newdev" ( _
ByVal hwndParent As LongPtr, _
ByVal InfPath As String, _
ByVal Flags As Long, _
ByVal NeedReboot As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
DiUninstallDriverA = ctypes.windll.newdev.DiUninstallDriverA
DiUninstallDriverA.restype = wintypes.BOOL
DiUninstallDriverA.argtypes = [
wintypes.HANDLE, # hwndParent : HWND optional
wintypes.LPCSTR, # InfPath : LPCSTR
wintypes.DWORD, # Flags : DIUNINSTALLDRIVER_FLAGS
ctypes.c_void_p, # NeedReboot : BOOL* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('newdev.dll')
DiUninstallDriverA = Fiddle::Function.new(
lib['DiUninstallDriverA'],
[
Fiddle::TYPE_VOIDP, # hwndParent : HWND optional
Fiddle::TYPE_VOIDP, # InfPath : LPCSTR
-Fiddle::TYPE_INT, # Flags : DIUNINSTALLDRIVER_FLAGS
Fiddle::TYPE_VOIDP, # NeedReboot : BOOL* optional, out
],
Fiddle::TYPE_INT)#[link(name = "newdev")]
extern "system" {
fn DiUninstallDriverA(
hwndParent: *mut core::ffi::c_void, // HWND optional
InfPath: *const u8, // LPCSTR
Flags: u32, // DIUNINSTALLDRIVER_FLAGS
NeedReboot: *mut i32 // BOOL* optional, out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("newdev.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool DiUninstallDriverA(IntPtr hwndParent, [MarshalAs(UnmanagedType.LPStr)] string InfPath, uint Flags, IntPtr NeedReboot);
"@
$api = Add-Type -MemberDefinition $sig -Name 'newdev_DiUninstallDriverA' -Namespace Win32 -PassThru
# $api::DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot)#uselib "newdev.dll"
#func global DiUninstallDriverA "DiUninstallDriverA" sptr, sptr, sptr, sptr
; DiUninstallDriverA hwndParent, InfPath, Flags, varptr(NeedReboot) ; 戻り値は stat
; hwndParent : HWND optional -> "sptr"
; InfPath : LPCSTR -> "sptr"
; Flags : DIUNINSTALLDRIVER_FLAGS -> "sptr"
; NeedReboot : BOOL* optional, out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "newdev.dll" #cfunc global DiUninstallDriverA "DiUninstallDriverA" sptr, str, int, var ; res = DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot) ; hwndParent : HWND optional -> "sptr" ; InfPath : LPCSTR -> "str" ; Flags : DIUNINSTALLDRIVER_FLAGS -> "int" ; NeedReboot : BOOL* optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "newdev.dll" #cfunc global DiUninstallDriverA "DiUninstallDriverA" sptr, str, int, sptr ; res = DiUninstallDriverA(hwndParent, InfPath, Flags, varptr(NeedReboot)) ; hwndParent : HWND optional -> "sptr" ; InfPath : LPCSTR -> "str" ; Flags : DIUNINSTALLDRIVER_FLAGS -> "int" ; NeedReboot : BOOL* optional, out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; BOOL DiUninstallDriverA(HWND hwndParent, LPCSTR InfPath, DIUNINSTALLDRIVER_FLAGS Flags, BOOL* NeedReboot) #uselib "newdev.dll" #cfunc global DiUninstallDriverA "DiUninstallDriverA" intptr, str, int, var ; res = DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot) ; hwndParent : HWND optional -> "intptr" ; InfPath : LPCSTR -> "str" ; Flags : DIUNINSTALLDRIVER_FLAGS -> "int" ; NeedReboot : BOOL* optional, out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL DiUninstallDriverA(HWND hwndParent, LPCSTR InfPath, DIUNINSTALLDRIVER_FLAGS Flags, BOOL* NeedReboot) #uselib "newdev.dll" #cfunc global DiUninstallDriverA "DiUninstallDriverA" intptr, str, int, intptr ; res = DiUninstallDriverA(hwndParent, InfPath, Flags, varptr(NeedReboot)) ; hwndParent : HWND optional -> "intptr" ; InfPath : LPCSTR -> "str" ; Flags : DIUNINSTALLDRIVER_FLAGS -> "int" ; NeedReboot : BOOL* optional, out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
newdev = windows.NewLazySystemDLL("newdev.dll")
procDiUninstallDriverA = newdev.NewProc("DiUninstallDriverA")
)
// hwndParent (HWND optional), InfPath (LPCSTR), Flags (DIUNINSTALLDRIVER_FLAGS), NeedReboot (BOOL* optional, out)
r1, _, err := procDiUninstallDriverA.Call(
uintptr(hwndParent),
uintptr(unsafe.Pointer(windows.BytePtrFromString(InfPath))),
uintptr(Flags),
uintptr(NeedReboot),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction DiUninstallDriverA(
hwndParent: THandle; // HWND optional
InfPath: PAnsiChar; // LPCSTR
Flags: DWORD; // DIUNINSTALLDRIVER_FLAGS
NeedReboot: Pointer // BOOL* optional, out
): BOOL; stdcall;
external 'newdev.dll' name 'DiUninstallDriverA';result := DllCall("newdev\DiUninstallDriverA"
, "Ptr", hwndParent ; HWND optional
, "AStr", InfPath ; LPCSTR
, "UInt", Flags ; DIUNINSTALLDRIVER_FLAGS
, "Ptr", NeedReboot ; BOOL* optional, out
, "Int") ; return: BOOL●DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot) = DLL("newdev.dll", "bool DiUninstallDriverA(void*, char*, dword, void*)")
# 呼び出し: DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot)
# hwndParent : HWND optional -> "void*"
# InfPath : LPCSTR -> "char*"
# Flags : DIUNINSTALLDRIVER_FLAGS -> "dword"
# NeedReboot : BOOL* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "newdev" fn DiUninstallDriverA(
hwndParent: ?*anyopaque, // HWND optional
InfPath: [*c]const u8, // LPCSTR
Flags: u32, // DIUNINSTALLDRIVER_FLAGS
NeedReboot: [*c]i32 // BOOL* optional, out
) callconv(std.os.windows.WINAPI) i32;proc DiUninstallDriverA(
hwndParent: pointer, # HWND optional
InfPath: cstring, # LPCSTR
Flags: uint32, # DIUNINSTALLDRIVER_FLAGS
NeedReboot: ptr int32 # BOOL* optional, out
): int32 {.importc: "DiUninstallDriverA", stdcall, dynlib: "newdev.dll".}pragma(lib, "newdev");
extern(Windows)
int DiUninstallDriverA(
void* hwndParent, // HWND optional
const(char)* InfPath, // LPCSTR
uint Flags, // DIUNINSTALLDRIVER_FLAGS
int* NeedReboot // BOOL* optional, out
);ccall((:DiUninstallDriverA, "newdev.dll"), stdcall, Int32,
(Ptr{Cvoid}, Cstring, UInt32, Ptr{Int32}),
hwndParent, InfPath, Flags, NeedReboot)
# hwndParent : HWND optional -> Ptr{Cvoid}
# InfPath : LPCSTR -> Cstring
# Flags : DIUNINSTALLDRIVER_FLAGS -> UInt32
# NeedReboot : BOOL* optional, out -> Ptr{Int32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t DiUninstallDriverA(
void* hwndParent,
const char* InfPath,
uint32_t Flags,
int32_t* NeedReboot);
]]
local newdev = ffi.load("newdev")
-- newdev.DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot)
-- hwndParent : HWND optional
-- InfPath : LPCSTR
-- Flags : DIUNINSTALLDRIVER_FLAGS
-- NeedReboot : BOOL* optional, out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('newdev.dll');
const DiUninstallDriverA = lib.func('__stdcall', 'DiUninstallDriverA', 'int32_t', ['void *', 'str', 'uint32_t', 'int32_t *']);
// DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot)
// hwndParent : HWND optional -> 'void *'
// InfPath : LPCSTR -> 'str'
// Flags : DIUNINSTALLDRIVER_FLAGS -> 'uint32_t'
// NeedReboot : BOOL* optional, out -> 'int32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("newdev.dll", {
DiUninstallDriverA: { parameters: ["pointer", "buffer", "u32", "pointer"], result: "i32" },
});
// lib.symbols.DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot)
// hwndParent : HWND optional -> "pointer"
// InfPath : LPCSTR -> "buffer"
// Flags : DIUNINSTALLDRIVER_FLAGS -> "u32"
// NeedReboot : BOOL* optional, out -> "pointer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t DiUninstallDriverA(
void* hwndParent,
const char* InfPath,
uint32_t Flags,
int32_t* NeedReboot);
C, "newdev.dll");
// $ffi->DiUninstallDriverA(hwndParent, InfPath, Flags, NeedReboot);
// hwndParent : HWND optional
// InfPath : LPCSTR
// Flags : DIUNINSTALLDRIVER_FLAGS
// NeedReboot : BOOL* optional, out
// 構造体/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 Newdev extends StdCallLibrary {
Newdev INSTANCE = Native.load("newdev", Newdev.class, W32APIOptions.ASCII_OPTIONS);
boolean DiUninstallDriverA(
Pointer hwndParent, // HWND optional
String InfPath, // LPCSTR
int Flags, // DIUNINSTALLDRIVER_FLAGS
IntByReference NeedReboot // BOOL* optional, out
);
}@[Link("newdev")]
lib Libnewdev
fun DiUninstallDriverA = DiUninstallDriverA(
hwndParent : Void*, # HWND optional
InfPath : UInt8*, # LPCSTR
Flags : UInt32, # DIUNINSTALLDRIVER_FLAGS
NeedReboot : Int32* # BOOL* optional, out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef DiUninstallDriverANative = Int32 Function(Pointer<Void>, Pointer<Utf8>, Uint32, Pointer<Int32>);
typedef DiUninstallDriverADart = int Function(Pointer<Void>, Pointer<Utf8>, int, Pointer<Int32>);
final DiUninstallDriverA = DynamicLibrary.open('newdev.dll')
.lookupFunction<DiUninstallDriverANative, DiUninstallDriverADart>('DiUninstallDriverA');
// hwndParent : HWND optional -> Pointer<Void>
// InfPath : LPCSTR -> Pointer<Utf8>
// Flags : DIUNINSTALLDRIVER_FLAGS -> Uint32
// NeedReboot : BOOL* optional, out -> Pointer<Int32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function DiUninstallDriverA(
hwndParent: THandle; // HWND optional
InfPath: PAnsiChar; // LPCSTR
Flags: DWORD; // DIUNINSTALLDRIVER_FLAGS
NeedReboot: Pointer // BOOL* optional, out
): BOOL; stdcall;
external 'newdev.dll' name 'DiUninstallDriverA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "DiUninstallDriverA"
c_DiUninstallDriverA :: Ptr () -> CString -> Word32 -> Ptr CInt -> IO CInt
-- hwndParent : HWND optional -> Ptr ()
-- InfPath : LPCSTR -> CString
-- Flags : DIUNINSTALLDRIVER_FLAGS -> Word32
-- NeedReboot : BOOL* optional, out -> Ptr CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let diuninstalldrivera =
foreign "DiUninstallDriverA"
((ptr void) @-> string @-> uint32_t @-> (ptr int32_t) @-> returning int32_t)
(* hwndParent : HWND optional -> (ptr void) *)
(* InfPath : LPCSTR -> string *)
(* Flags : DIUNINSTALLDRIVER_FLAGS -> uint32_t *)
(* NeedReboot : BOOL* optional, out -> (ptr int32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library newdev (t "newdev.dll"))
(cffi:use-foreign-library newdev)
(cffi:defcfun ("DiUninstallDriverA" di-uninstall-driver-a :convention :stdcall) :int32
(hwnd-parent :pointer) ; HWND optional
(inf-path :string) ; LPCSTR
(flags :uint32) ; DIUNINSTALLDRIVER_FLAGS
(need-reboot :pointer)) ; BOOL* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $DiUninstallDriverA = Win32::API::More->new('newdev',
'BOOL DiUninstallDriverA(HANDLE hwndParent, LPCSTR InfPath, DWORD Flags, LPVOID NeedReboot)');
# my $ret = $DiUninstallDriverA->Call($hwndParent, $InfPath, $Flags, $NeedReboot);
# hwndParent : HWND optional -> HANDLE
# InfPath : LPCSTR -> LPCSTR
# Flags : DIUNINSTALLDRIVER_FLAGS -> DWORD
# NeedReboot : BOOL* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f DiUninstallDriverW (Unicode版) — INFで指定したドライバーパッケージをアンインストールする。
- f DiInstallDevice — デバイスをインストールしドライバーを適用する。