Disabling a device in c# (Help please)

G

Guest

I am attmempting to enable/disable a device in C#. I am able to locate it and tell it to disable, but the device will not disable even upon reboot. I am hoping someone can lead me in the right direction or tell me what I am doing wrong. I have been testing on my local machine's LPT port

Here is the code in question. TI

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")
private static extern Boolea
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 = System.Guid.Empty
o_DeviceInfoData.Reserved = 0

b_Res = SetupDiEnumDeviceInfo(p_DeviceInfoSet
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);
System.Diagnostics.Debug.WriteLine(GetErrorMessage(Marshal.GetLastWin32Error()), "StateChange(" + ui_NewState.ToString() + "):");
throw new ApplicationException(GetErrorMessage(Marshal.GetLastWin32Error()));
}
System.Diagnostics.Debug.WriteLine(GetErrorMessage(Marshal.GetLastWin32Error()), "StateChange(" + ui_NewState.ToString() + "):");

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top