[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetupDiGetClassDevs( // 1st form using a ClassGUID
ref Guid ClassGuid,
int Enumerator,
IntPtr hwndParent,
int Flags
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] // 2nd form uses an Enumerator
static extern IntPtr SetupDiGetClassDevs(
int ClassGuid,
string Enumerator,
IntPtr hwndParent,
int Flags
);
private const int DIGCF_PRESENT = (0x00000002);
private const int DN_DISABLEABLE = (0x00002000); // Can be rebalance
private const int CM_PROB_HARDWARE_DISABLED = (0x0000001D); // device disabled
private const int DN_HAS_PROBLEM = (0x00000400); // Need device installe
private const int CM_PROB_DISABLED = (0x00000016); // devinst is disable
private const int DIF_PROPERTYCHANGE = (0x00000012);
private const int DICS_FLAG_GLOBAL = (0x00000001); // make change in all hardware profile
private const int DICS_ENABLE = (0x00000001);
private const int DICS_DISABLE = (0x00000002);
private const int DICS_PROPCHANGE = (0x00000003);
private const int DICS_START = (0x00000004);
private const int DICS_STOP = (0x00000005);
[DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);
[DllImport("setupapi.dll")]
private static extern bool
SetupDiEnumDeviceInfo(IntPtr p_DeviceInfoSet, UInt32 ui_MemberIndex,
SP_DEVINFO_DATA o_DeviceInfoData);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetupDiSetClassInstallParams(
IntPtr DeviceInfoSet, IntPtr DeviceInfoData,
IntPtr ClassInstallParams, int ClassInstallParamsSize);
[StructLayout(LayoutKind.Sequential)]
private class SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst; // DEVINST handl
public ulong Reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_CLASSINSTALL_HEADER
{
public int cbSize;
public uint InstallFunction;
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_PROPCHANGE_PARAMS
{
public SP_CLASSINSTALL_HEADER ClassInstallHeader;
public uint StateChange;
public uint Scope;
public uint HwProfile;
}
//..
private static void StateChange(uint ui_NewState, uint ui_SelectedItem, IntPtr p_DeviceInfoSet)
{
SP_DEVINFO_DATA o_DeviceInfoData = new SP_DEVINFO_DATA();
bool b_Res = false;
SP_PROPCHANGE_PARAMS o_PropChangeParams = new SP_PROPCHANGE_PARAMS();
int i_PCPSize = 0;
IntPtr p_PropChangeParams;
int i_DIDSize = 0;
IntPtr p_DeviceInfoData;
o_DeviceInfoData.cbSize = 28;
//is devices exist for clas
o_DeviceInfoData.DevInst = 0;
o_DeviceInfoData.ClassGuid = Guid.Empty;
o_DeviceInfoData.Reserved = 0;
Guid dgg = new Guid("{4D36E967-E325-11CE-BFC1-08002BE10318}");
IntPtr hDevInfo1 = SetupDiGetClassDevs(ref dgg, 0, IntPtr.Zero, DIGCF_PRESENT);
b_Res = SetupDiEnumDeviceInfo(hDevInfo1,
ui_SelectedItem, o_DeviceInfoData);
if (b_Res == false)
{ //no such device
SetupDiDestroyDeviceInfoList(p_DeviceInfoSet);
throw new ApplicationException("Device Does Not Exist.");
o_PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
o_PropChangeParams.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER));
o_PropChangeParams.StateChange = ui_NewState;
o_PropChangeParams.Scope = Convert.ToUInt32(DICS_FLAG_GLOBAL);
o_PropChangeParams.HwProfile = 0;
i_PCPSize = Marshal.SizeOf(o_PropChangeParams);
p_PropChangeParams = Marshal.AllocHGlobal(i_PCPSize);
Marshal.StructureToPtr(o_PropChangeParams, p_PropChangeParams, true);
i_DIDSize = Marshal.SizeOf(o_DeviceInfoData);
p_DeviceInfoData = Marshal.AllocHGlobal(i_DIDSize);
Marshal.StructureToPtr(o_DeviceInfoData, p_DeviceInfoData, true);
b_Res = SetupDiSetClassInstallParams(p_DeviceInfoSet, p_DeviceInfoData,
p_PropChangeParams, i_PCPSize);
Marshal.FreeHGlobal(p_PropChangeParams);
Marshal.FreeHGlobal(p_DeviceInfoData);
if (b_Res == false)
{
SetupDiDestroyDeviceInfoList(p_DeviceInfoSet);
}
}
}
Нашел код блокировки флешек. Хелп разобрать
http://www.programmersforum.ru/showthread.php?t=5932&page=2&highlight=%F1%E8%EC%EF%EB%E8 - на делфи
и мой. Можно код, где я получаю список флешок и вырубаю их. я покка не совсем прошарил. буду ковырять. оч надо для общего развития и для приколов.
Код:
и мой. Можно код, где я получаю список флешок и вырубаю их. я покка не совсем прошарил. буду ковырять. оч надо для общего развития и для приколов.