Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to find out whether Reboot is needed after SetupDiRemoveDevice(...),...?

214 views
Skip to first unread message

Kerem Gümrükcü

unread,
Dec 29, 2008, 2:47:44 PM12/29/08
to
Hi,

how can i find out if a reboot/restart is needed after
calling SetupDiRemoveDevice(...) on a particular device?

TIA,...

Regards

Kerem

--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Jim Cavalaris [MS]

unread,
Jan 21, 2009, 12:59:36 AM1/21/09
to
as non-intuitive as it is, you should not call SetupDiRemoveDevice directly
to uninstall a device. that function is reserved for use by class
installers only. calling it from your application actually bypasses the
class-installer and any co-installers that are registered for the device.
if the installers do not get a chance to participate in the uninstall to
remove any device-specific state they might maintain, the uninstall may not
behave as expected.

to uninstall a device, call SetupDiCallClassInstaller with the DIF_REMOVE
code instead.

SetupDiCallClassInstaller
http://msdn.microsoft.com/en-us/library/ms792989.aspx

DIF_REMOVE
http://msdn.microsoft.com/en-us/library/ms793460.aspx

to check for a reboot, call SetupDiGetDeviceInstallParams after a successful
DIF_REMOVE call and check for if either of the DI_NEEDRESTART or
DI_NEEDREBOOT SP_DEVINSTALL_PARAMS.Flags bits are set.

SetupDiGetDeviceInstallParams
http://msdn.microsoft.com/en-us/library/ms791258.aspx

SP_DEVINSTALL_PARAMS
http://msdn.microsoft.com/en-us/library/ms793021.aspx

you can find an example of how to uses these functions to uninstall a device
and check for reboot in the "devcon" WDK sample:

Device Console (devcon)
http://msdn.microsoft.com/en-us/library/cc462983.aspx

hope this helps,
jim.

--
This posting is provided "AS IS" with no warranties, and confers no rights.


"Kerem Gümrükcü" <kare...@hotmail.com> wrote in message
news:e7gpS5ea...@TK2MSFTNGP05.phx.gbl...

Kerem Gümrükcü

unread,
Jan 21, 2009, 4:24:10 AM1/21/09
to
Hi Jim,

thanks for your reply. Well i didnt know that this is such a big
difference in calling the SetupDiCallClassInstaller and
SetupDiRemoveDevice. I will change this inside my application.
But at first i have to manage the huge amount of pinvoke stuff,
since i call this from a managed C# .NET Runtimed application.
Its sometimes hard to find the right .NET representation even
when you follow the runtime guidlines, you still have to work
sometimes with raw memory and lots of PtrToStruct and Vice
Versa,...

Thanks for the hint,...

Regards

Kerem

--
--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.pro-it-education.de/software/deviceremover
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

"Jim Cavalaris [MS]" <jam...@online.microsoft.com> schrieb im Newsbeitrag
news:2B4A53FB-6500-48EB...@microsoft.com...

Maxim S. Shatskih

unread,
Jan 21, 2009, 2:53:04 PM1/21/09
to
> thanks for your reply. Well i didnt know that this is such a big
> difference in calling the SetupDiCallClassInstaller and
> SetupDiRemoveDevice.

Look at DEVCON sample, it always does SetupDiCallClassInstaller

> Its sometimes hard to find the right .NET representation

Correct. The interface between managed and unmanaged code should be specially designed, just using pInvoke for all setupapi is a bad idea.

--
Maxim S. Shatskih
Windows DDK MVP
ma...@storagecraft.com
http://www.storagecraft.com

Kerem Gümrükcü

unread,
Jan 22, 2009, 1:46:03 AM1/22/09
to
Hi maxim,

>Correct. The interface between managed and unmanaged code
>should be specially designed, just using pInvoke for all setupapi
>is a bad idea.

and thats why i exported some functions into a C-Export Style Library
that i use to get what i want, but mostly it works on .NET Layer just
fine and i only use my external dll for api calls that are really diffcult
to map to .NET Unmannaged calls if you only need e.g. string for something
and you must translate 10 functions and tons of structs/pointers to .NET,
then and only then i use all ready SDK/DDK stuff and enclose it in one
single export and use it from my .NET code. Anyway, here are the
translations of
the Windows SetupAPI i use to the .NET representation if someone is
interessted in it:

[Flags]
public enum SpDevInstallParamsFlags: uint
{
DI_SHOWOEM = 0x00000001, // support Other... button
DI_SHOWCOMPAT = 0x00000002, // show compatibility list
DI_SHOWCLASS = 0x00000004, // show class list
DI_SHOWALL = 0x00000007, // both class & compat list shown
DI_NOVCP = 0x00000008, // don't create a new copy queue--use
// caller-supplied FileQueue
DI_DIDCOMPAT = 0x00000010, // Searched for compatible
devices
DI_DIDCLASS = 0x00000020, // Searched for class devices
DI_AUTOASSIGNRES = 0x00000040, // No UI for resources if
possible

// flags returned by DiInstallDevice to indicate need to
reboot/restart
DI_NEEDRESTART = 0x00000080, // Reboot required to take
effect
DI_NEEDREBOOT = 0x00000100, // ""

// flags for device installation
DI_NOBROWSE = 0x00000200, // no Browse... in InsertDisk

// Flags set by DiBuildDriverInfoList
DI_MULTMFGS = 0x00000400, // Set if multiple manufacturers
in
// class driver list

// Flag indicates that device is disabled
DI_DISABLED = 0x00000800, // Set if device disabled

// Flags for Device/Class Properties
DI_GENERALPAGE_ADDED = 0x00001000,
DI_RESOURCEPAGE_ADDED = 0x00002000,

// Flag to indicate the setting properties for this Device (or
class) caused a change
// so the Dev Mgr UI probably needs to be updatd.
DI_PROPERTIES_CHANGE = 0x00004000,

// Flag to indicate that the sorting from the INF file should be
used.
DI_INF_IS_SORTED = 0x00008000,

// Flag to indicate that only the the INF specified by
SP_DEVINSTALL_PARAMS.DriverPath
// should be searched.
DI_ENUMSINGLEINF = 0x00010000,

// Flag that prevents ConfigMgr from removing/re-enumerating
devices during device
// registration, installation, and deletion.
DI_DONOTCALLCONFIGMG = 0x00020000,

// The following flag can be used to install a device disabled
DI_INSTALLDISABLED = 0x00040000,

// Flag that causes SetupDiBuildDriverInfoList to build a
device's compatible driver
// list from its existing class driver list, instead of the
normal INF search.
DI_COMPAT_FROM_CLASS = 0x00080000,

// This flag is set if the Class Install params should be used.
DI_CLASSINSTALLPARAMS = 0x00100000,

// This flag is set if the caller of DiCallClassInstaller does
NOT
// want the internal default action performed if the Class
installer
// returns ERROR_DI_DO_DEFAULT.
DI_NODI_DEFAULTACTION = 0x00200000,

// The setupx flag, DI_NOSYNCPROCESSING (0x00400000) is not
support in the Setup APIs.

// flags for device installation
DI_QUIETINSTALL = 0x00800000, // don't confuse the user with
// questions or excess info
DI_NOFILECOPY = 0x01000000, // No file Copy necessary
DI_FORCECOPY = 0x02000000, // Force files to be copied from
install path
DI_DRIVERPAGE_ADDED = 0x04000000, // Prop provider added
Driver page.
DI_USECI_SELECTSTRINGS = 0x08000000, // Use Class Installer
Provided strings in the Select Device Dlg
DI_OVERRIDE_INFFLAGS = 0x10000000, // Override INF flags
DI_PROPS_NOCHANGEUSAGE = 0x20000000, // No Enable/Disable in
General Props

DI_NOSELECTICONS = 0x40000000, // No small icons in select
device dialogs

DI_NOWRITE_IDS = 0x80000000, // Don't write HW & Compat IDs
on install

}

[Flags]
public enum SpDevInstallParamsFlagsEx : uint
{
DI_FLAGSEX_RESERVED2 = 0x00000001, //
DI_FLAGSEX_USEOLDINFSEARCH is obsolete
DI_FLAGSEX_RESERVED3 = 0x00000002, //
DI_FLAGSEX_AUTOSELECTRANK0 is obsolete
DI_FLAGSEX_CI_FAILED = 0x00000004, // Failed to Load/Call class
installer
DI_FLAGSEX_FINISHINSTALL_ACTION = 0x00000008, //
Class/co-installer wants to get a DIF_FINISH_INSTALL action in client
context.
DI_FLAGSEX_DIDINFOLIST = 0x00000010, // Did the Class Info List
DI_FLAGSEX_DIDCOMPATINFO = 0x00000020, // Did the Compat Info
List
DI_FLAGSEX_FILTERCLASSES = 0x00000040,
DI_FLAGSEX_SETFAILEDINSTALL = 0x00000080,
DI_FLAGSEX_DEVICECHANGE = 0x00000100,
DI_FLAGSEX_ALWAYSWRITEIDS = 0x00000200,
DI_FLAGSEX_PROPCHANGE_PENDING = 0x00000400, // One or more
device property sheets have had changes made
// to them, and need to have a DIF_PROPERTYCHANGE occur.
DI_FLAGSEX_ALLOWEXCLUDEDDRVS = 0x00000800,
DI_FLAGSEX_NOUIONQUERYREMOVE = 0x00001000,
DI_FLAGSEX_USECLASSFORCOMPAT = 0x00002000, // Use the device's
class when building compat drv list.
// (Ignored if DI_COMPAT_FROM_CLASS flag is specified.)

DI_FLAGSEX_RESERVED4 = 0x00004000, //
DI_FLAGSEX_OLDINF_IN_CLASSLIST is obsolete
DI_FLAGSEX_NO_DRVREG_MODIFY = 0x00008000, // Don't run AddReg
and DelReg for device's software (driver) key.
DI_FLAGSEX_IN_SYSTEM_SETUP = 0x00010000, // Installation is
occurring during initial system setup.
DI_FLAGSEX_INET_DRIVER = 0x00020000, // Driver came from
Windows Update
DI_FLAGSEX_APPENDDRIVERLIST = 0x00040000, // Cause
SetupDiBuildDriverInfoList to append
// a new driver list to an existing list.
DI_FLAGSEX_PREINSTALLBACKUP = 0x00080000, // not used
DI_FLAGSEX_BACKUPONREPLACE = 0x00100000, // not used
DI_FLAGSEX_DRIVERLIST_FROM_URL = 0x00200000, // build driver
list from INF(s) retrieved from URL specified
// in SP_DEVINSTALL_PARAMS.DriverPath (empty string means
// Windows Update website)
DI_FLAGSEX_RESERVED1 = 0x00400000,
DI_FLAGSEX_EXCLUDE_OLD_INET_DRIVERS = 0x00800000, // Don't
include old Internet drivers when building
// a driver list.
// Ignored on Windows Vista and later.
DI_FLAGSEX_POWERPAGE_ADDED = 0x01000000, // class installer
added their own power page

DI_FLAGSEX_FILTERSIMILARDRIVERS = 0x02000000, // only include
similar drivers in class list
DI_FLAGSEX_INSTALLEDDRIVER = 0x04000000, // only add the
installed driver to the class or compat
// driver list. Used in calls to SetupDiBuildDriverInfoList
DI_FLAGSEX_NO_CLASSLIST_NODE_MERGE = 0x08000000, // Don't
remove identical driver nodes from the class list
DI_FLAGSEX_ALTPLATFORM_DRVSEARCH = 0x10000000, // Build driver
list based on alternate platform information
// specified in associated file queue
DI_FLAGSEX_RESTART_DEVICE_ONLY = 0x20000000, // only restart
the device drivers are being installed on as
// opposed to restarting all devices using those drivers.
DI_FLAGSEX_RECURSIVESEARCH = 0x40000000, // Tell
SetupDiBuildDriverInfoList to do a recursive search
DI_FLAGSEX_SEARCH_PUBLISHED_INFS = 0x80000000, // Tell
SetupDiBuildDriverInfoList to do a "published INF" search

}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SP_DEVINSTALL_PARAMS
{
public int cbSize;
public SpDevinstallParamsFlags Flags;
public SpDevinstallParamsFlagsEx FlagsEx;
public IntPtr HwndParent;
public IntPtr InstallMsgHandler;
public IntPtr InstallMsgHandlerContext;
public IntPtr FileQueue;
public IntPtr CallInstallReserved;
public int Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string DriverPath;
}

Call it with these then (whatever you like):

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiGetDeviceInstallParams(IntPtr
DeviceInfoSet,
IntPtr DeviceInfoData,
ref SP_DEVINSTALL_PARAMS DeviceInstallParams);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiGetDeviceInstallParams(IntPtr
DeviceInfoSet,
SP_DEVINFO_DATA DeviceInfoData,
ref SP_DEVINSTALL_PARAMS DeviceInstallParams);

[DllImport("setupapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetupDiGetDeviceInstallParams(IntPtr
DeviceInfoSet,
IntPtr DeviceInfoData,
IntPtr DeviceInstallParams);

Additionall there can be [In] and [Out] Attributes but the Runtime handles
them
just fine and as expected,....

Regards

Kerem

0 new messages