473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Disabling a device in c# (Help please)

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.InstallFunct ion = 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.GetLa stWin32Error()));
}
System.Diagnostics.Debug.WriteLine(GetErrorMessage (Marshal.GetLastWin32Error()), "StateChange(" + ui_NewState.ToString() + "):");

}

Jul 21 '05 #1
0 2816

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: FloydPW | last post by:
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...
3
by: Russell McDade | last post by:
I have a html page that I don't want anyone to print. Is there a way using JavaScript to send an alert to the user to warn them from trying to print the html page? Is there an onPrint event...
0
by: ChrisC | last post by:
Im having some trouble disabling hardware using WMI. I got a handle on the device, but when I call SetPowerState I get a type mismatch. The definition of the method says it takes an int (power...
8
by: nek | last post by:
Greetings, Has anyone got the latest progress on this? It hit me this time on W2K running DB2 V8.1.5. It returned the following warning: D:\Temp>db2 backup db sample to \\localhost\tape0...
8
by: Tony Liu | last post by:
I am having a "Null Device is Missing" compile error when compiling a c++ project. The documentation from MSDN said it could be caused by low system resource or the user account does not have...
2
by: jwgoerlich | last post by:
Hello group, My C# app needs to disable and re-enable devices. I did some research and found that I could do exactly what I wanted by using Devcon from the DDK. Devcon disable =net...
1
by: =?Utf-8?B?15DXldeo158=?= | last post by:
I have recently installed my windows XP, the process went smoothly, but I had encounterd some drivers issues after the installation. I managed to solve the ethernet adapter driver issue, but I...
0
by: =?Utf-8?B?QmV2eV9KZXRlcg==?= | last post by:
Using XP Pro as an administrator. Experiencing 3 problems: 1) Device Manager is empty in both normal & safe modes, 2) F8 will not take me into Safe Mode but MSConfig will, & 3) Computer folder...
7
by: SAL | last post by:
Hopefully someone will have a clue as to what is happening here. In our office, we have both ColdFusion and ASP.NET applications. One of our new developers has been installing ColdFusion 8 on the...
2
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am trying to disable the power key in windows mobile version 6.0 while my application is running. The user should not be able to turn off or turn on the device. Those functionalities should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.