|
Hi friend.
Sorry, I never developed Windows drivers.
But I saw a lot articles about it in codeproject.
Put string "Driver" in Search box of codeproject main page and
you find these articles.
Best Regards.
Vladimir.
|
|
|
|
|
I found all three of the articles to be very informative. I am working on a little project that requires listing all the drivers for a certain device (much like the device manager in windows does) but I have been unable to accomplish it. Can you point me to a resource that explains this or give a brief explanation of how you could do it ?
Thanks,
Jon
|
|
|
|
|
jonnyboy9999 wrote:
I found all three of the articles to be very informative.
Thank you.
jonnyboy9999 wrote:
I am working on a little project that requires listing all the drivers for a certain device (much like the device manager in windows does) but I have been unable to accomplish it. Can you point me to a resource that explains this or give a brief explanation of how you could do it ?
Try to study follow resources:
Using Device Installation Functions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/install/hh/install/setupapi_071eca3a-0cc3-4962-b3de-65dcf056c761.xml.asp
SetupDiBuildDriverInfoList
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/install/hh/install/di-rtns_dd9aa1be-1a67-4cc6-8a06-5db71eecd322.xml.asp
SetupDiEnumDriverInfo
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/install/hh/install/di-rtns_8d84a225-9dac-4ab3-8c9a-5048284d82be.xml.asp
SetupDiGetDriverInfoDetail
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/install/hh/install/di-rtns_5a2fb98d-54ee-4290-9969-f5e12d77cbcf.xml.asp
SP_DRVINFO_DATA
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/install/hh/install/di-struct_738a1fa5-729a-4464-af75-05591d68eef7.xml.asp
SP_DRVINFO_DETAIL_DATA
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/install/hh/install/di-struct_74ef2af7-e982-4041-9c39-605ca316359c.xml.asp
Vladimir.
|
|
|
|
|
It seems I have read all of those articles countless times, but I still can't seem to get it. Here is a layout of what I am doing.
CM_Enumerate_Classes //Enumerate all the classes
SetupDiClassNameFromGuid //For Each Class, get its name
SetupDiGetClassDescription //For Each Class, get its desc
SetupDiGetClassDevs //For Each Class, get its device info set
SetupDiEnumDeviceInfo //Get Each device info data for the device info set
SetupDiGetDeviceRegistryProperty //For Each info data get the device f. name or desc
SetupDiBuildDriverInfoList //For Each info data build a driver list.
SetupDiEnumDriverInfo //For each info data enum a driver list.
This all works just fine until the last step (SetupDiEnumDriverInfo). Can you verify that the following two things are correct:
1.
private static extern bool SetupDiEnumDriverInfo(IntPtr inpDeviceInfoSet, SP_DEVINFO_DATA inpDeviceInfoData, int inpDriverType, int inpMemberIndex, ref SP_DRVINFO_DATA outDriverInfoData);
2.
[StructLayout(LayoutKind.Sequential)]
private class SP_DRVINFO_DATA
{
public int cbSize;
public int DriverType;
public long Reserved;
public string Description;
public string MfgName;
public string ProviderName;
DateTime DriverDate;
string DriverVersion;
}
I am also having trouble figuring out what to set cbsize to. Since Marshal.SizeOf does not work for SP_DEVINFO_DATA I assume it will also not work for the above structure.
Thanks you very much,
Jon.
|
|
|
|
|
I have not time now. May be during a week.
Send here your declaration for SetupDiBuildDriverInfoList.
|
|
|
|
|
Here are my two driver declarations.
[DllImport("setupapi.dll")]
private static extern bool SetupDiBuildDriverInfoList(IntPtr inpDeviceInfoSet, SP_DEVINFO_DATA outDeviceInfoData, int inpDriverType);
[DllImport("setupapi.dll")]
private static extern bool SetupDiEnumDriverInfo(IntPtr inpDeviceInfoSet, SP_DEVINFO_DATA inpDeviceInfoData, int inpDriverType, int inpMemberIndex, SP_DRVINFO_DATA outDriverInfoData);
And here are my two structures.
[StructLayout(LayoutKind.Sequential)]
private class SP_DEVINFO_DATA
{
public int cbSize = 28;
public Guid ClassGuid;
public int DevInst;
public ulong Reserved;
};
[StructLayout(LayoutKind.Sequential)]
private class SP_DRVINFO_DATA
{
public int cbSize = ?????;
public int DriverType;
public ulong Reserved;
public string Description;
public string MfgName;
public string ProviderName;
DateTime DriverDate;
string DriverVersion;
}
|
|
|
|
|
Hello,
i ant to remove / eject plug/play devices
with some builtin windows os functions instead of
the taskbar notification area and the default
windows form...
Do you know some functions which can be
accessed from a normal user mode program ?
Thanks a lot.
Gabi
|
|
|
|
|
I know it is possible for some multimedia devices.
The mciSendString function (from winmm.dll) can open and close CD for example.
I suggest you read -
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_device_control.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_mcisendstring.asp
Vladimir.
|
|
|
|
|
Hallo!
This article was very interesting for me but I miss how I can manipulate the status (enable/disable) of the divice. I should to this for a usb-device but I couldn't found any solution.
Have someone a solution for this problem?
Thank you for help,
Thomas.
|
|
|
|
|
Hi,
mrt2003 wrote:
This article was very interesting for me...
Thank you...
mrt2003 wrote:
...but I miss how I can manipulate the status (enable/disable) of the divice.
You can use CM_Set_HW_Prof_Flags function from cfgmgr32.dll with flag CSCONFIGFLAG_DISABLE or CSCONFIGFLAG_DO_NOT_CREATE.
I want to write "Part 4" just about it but still have no time. May be on the next week...
|
|
|
|
|
thanks for your answer!
I could not found the function CM_Set_HW_Prof_Flags ... I found only following function:
CM_Set_HW_Prof_FlagsA
CM_Set_HW_Prof_FlagsW
CM_Set_HW_Prof_Flags_ExA
CM_Set_HW_Prof_Flags_ExW
Which one should I use and you know the definition of the function?
Thanks for help
Tom.
|
|
|
|
|
This information present in DDK. Two files - doc: cfgmgr32.doc and .h: cfgmgr32.h.
You can try download it.
In two words:
CM_Set_HW_Prof_Flags_ExA, CM_Set_HW_Prof_Flags_ExW - extended functions. I never tried it but as I know they has
addition parameter - hMachine. So it is for working with remote PC.
More exactly from cfgmgr32.doc:
"The Windows NT implementation of these routines is remotable to remote machines via a set of extended configuration manager routines (CM_xxx_Ex). A caller can connect to a remote machine by calling CM_Connect_Machine. The handle returned in that call can then be passed to other extended Configuration manager routines. This feature is only implemented on Windows NT."
About ...A and ...W from the same doc:
"The 32-bit Configuration Manager routines are defined to support both the Unicode and ANSI string set (as with other Win32 routines, the corresponding Unicode or ANSI version of the routine is called based on whether UNICODE is defined or not). The Unicode versions will return CR_CALL_NOT_IMPLEMENTED on Windows 95."
So you can use CM_Set_HW_Prof_FlagsA for all system or
CM_Set_HW_Prof_FlagsW for WinNT/2000/XP.
CMAPI
CONFIGRET
WINAPI
CM_Set_HW_Prof_FlagsA(
IN DEVINSTID_A szDevInstName,
IN ULONG ulConfig,
IN ULONG ulValue,
IN ULONG ulFlags
);
CMAPI
CONFIGRET
WINAPI
CM_Set_HW_Prof_FlagsW(
IN DEVINSTID_W szDevInstName,
IN ULONG ulConfig,
IN ULONG ulValue,
IN ULONG ulFlags
);
|
|
|
|
|
Hi,
I'm also interested in disabling devices.
I've DDK 2003 with cfgmgr32.h found.
But I can't find cfgmgr32.doc anywhere.
Can you tell me where I can find it?
Thank you very much.
|
|
|
|
|
Hi,
I'd got it from Win 98 and/or Win2000 DDK.
You can also read about it in MS site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/install/hh/install/cfgmgrfn_7bf5dade-d4e6-4460-8b3c-a4b99458ed28.xml.asp
|
|
|
|
|
Hi
I serarch this function in MSDN but MSDN's comment is "Do not use this function". Is there any other function can do the same thing?
|
|
|
|
|
In Devinfo.Cs add the following code:
<br />
private const int DICS_ENABLE = 0x00000001;<br />
private const int DICS_DISABLE = 0x00000002;<br />
private const int DICS_PROPCHANGE = 0x00000003;<br />
private const int DICS_START = 0x00000004;<br />
private const int DICS_STOP = 0x00000005;<br />
<br />
private const int DICS_FLAG_GLOBAL = 0x00000001;
private const int DICS_FLAG_CONFIGSPECIFIC= 0x00000002;
private const int DICS_FLAG_CONFIGGENERAL = 0x00000004;
private const int DIF_PROPERTYCHANGE = 0x00000012;<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct SP_CLASSINSTALL_HEADER<br />
{<br />
public int cbSize;<br />
public uint InstallFunction;<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct SP_PROPCHANGE_PARAMS<br />
{<br />
public SP_CLASSINSTALL_HEADER ClassInstallHeader;<br />
public uint StateChange;<br />
public uint Scope;
public uint HwProfile;<br />
};<br />
<br />
[DllImport("setupapi.dll")]<br />
private static extern bool<br />
SetupDiCallClassInstaller(<br />
int InstallFunction,<br />
IntPtr DeviceInfoSet,<br />
SP_DEVINFO_DATA DeviceInfoData);<br />
<br />
[DllImport("setupapi.dll", SetLastError=true, CharSet=CharSet.Auto)]<br />
public static extern bool SetupDiSetClassInstallParamsA(<br />
IntPtr DeviceInfoSet,<br />
IntPtr DeviceInfoData, <br />
SP_DEVINFO_DATA ClassInstallParams, <br />
int ClassInstallParamsSize);<br />
<br />
public static bool enableDevice(IntPtr HwDevInfo, SP_DEVINFO_DATA DeviceInfoData)<br />
{<br />
return changeDeviceState(HwDevInfo, DeviceInfoData, DICS_ENABLE);<br />
}<br />
public static bool disableDevice(IntPtr HwDevInfo, SP_DEVINFO_DATA DeviceInfoData)<br />
{<br />
return changeDeviceState(HwDevInfo, DeviceInfoData, DICS_DISABLE);<br />
}<br />
public static bool changeDeviceState(IntPtr HwDevInfo, SP_DEVINFO_DATA DeviceInfoData, uint state)<br />
{<br />
SP_PROPCHANGE_PARAMS propertyChangeParams = new SP_PROPCHANGE_PARAMS();<br />
propertyChangeParams.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER));<br />
propertyChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;<br />
propertyChangeParams.StateChange = state;<br />
propertyChangeParams.Scope = DICS_FLAG_GLOBAL;<br />
int propertyChangeParamsSize = Marshal.SizeOf(propertyChangeParams);<br />
<br />
IntPtr PSP_PROPCHANGE_PARAMS = Marshal.AllocHGlobal(propertyChangeParamsSize);<br />
Marshal.StructureToPtr(propertyChangeParams, PSP_PROPCHANGE_PARAMS, true);<br />
<br />
bool ok = SetupDiSetClassInstallParamsA(HwDevInfo, DeviceInfoData, PSP_PROPCHANGE_PARAMS, (UInt32) propertyChangeParamsSize);<br />
if (ok)<br />
{<br />
ok = SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, HwDevInfo, DeviceInfoData);<br />
if (!ok)<br />
{<br />
int errorCode = Marshal.GetLastWin32Error();<br />
Console.WriteLine("Error (" + errorCode.ToString() + ") + SetupDiSetClassInstaller");<br />
return false;<br />
}<br />
}<br />
else<br />
{<br />
int errorCode = Marshal.GetLastWin32Error();<br />
Console.WriteLine("Error (" + errorCode.ToString() + ") + SetupDiSetClassInstallParams");<br />
return false;<br />
}<br />
return true;<br />
}<br />
To see it in action e.g. to disable the printer port, add some code such as this to the end of the EnumerateDevices method:
<br />
if (DeviceName.ToString() == "ECP Printer Port (LPT1)") <br />
{<br />
disableDevice(NewDeviceInfoSet,DeviceInfoData);<br />
}
|
|
|
|
|
I would like to use enable/disable.
There seems to be a compiler error in the supplied code the call to SetupDiSetClassInstallParamsA(HwDevInfo, DeviceInfoData, PSP_PROPCHANGE_PARAMS...
the compiler reports parameters 2 and 3 are wrong. I have swapped these two parameters round but when trying out enable/disable I get an error 87 (wrong parameters).
Any ideas?
Nigel
|
|
|
|
|
You must change
<br />
[DllImport("setupapi.dll", SetLastError=true, CharSet=CharSet.Auto)]<br />
public static extern bool SetupDiSetClassInstallParamsA(<br />
IntPtr DeviceInfoSet,<br />
IntPtr DeviceInfoData,<br />
SP_DEVINFO_DATA ClassInstallParams,<br />
int ClassInstallParamsSize);<br />
to
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]<br />
public static extern bool SetupDiSetClassInstallParamsA(<br />
IntPtr DeviceInfoSet,<br />
SP_DEVINFO_DATA DeviceInfoData,<br />
IntPtr ClassInstallParams,<br />
UInt32 ClassInstallParamsSize);<br />
By the way, Vladimir, very nice work! Thanks a lot!
Alex
|
|
|
|
|
Hi
I can't get it to work ! Can you please help me ?
After I insert the code I get This error:
DevInfo.cs(56,21): error CS0051: Inconsistent accessibility: parameter type 'DevInfo.DeviceInfo.SP_DEVINFO_DATA' is less accessible than method 'DevInfo.DeviceInfo.SetupDiSetClassInstallParamsA(System.IntPtr, DevInfo.DeviceInfo.SP_DEVINFO_DATA, System.IntPtr, uint)'
I also get errors on public static bool enableDevice public static bool disableDevice and public static bool changeDeviceState (Location of symbol related to previous error)
I'm using SharpDevelop.
Thank you ,and by the way Very good article
GG.
|
|
|
|
|
Compilation error is removed by changing the parameter to public and re arranging the paramater according to the method prototype.
The error 87 was removed by modifying the changeDeviceState()as follows:
work for me on windows XP. now able to disable and enable the device.The code and information
provided by this thread has helped me and is awesome. I am using this to enable and disable a USB based touch screen to prevent
accidental user input. Thank you all and the to the owner of the article
//------ Code Starts Here -----------
<br />
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]<br />
public static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet,<br />
IntPtr DeviceInfoData, IntPtr ClassInstallParams, int<br />
ClassInstallParamsSize);<br />
<br />
<br />
<br />
public static bool changeDeviceState(IntPtr HwDevInfo, SP_DEVINFO_DATA DeviceInfoData, uint state)<br />
{<br />
<br />
SP_PROPCHANGE_PARAMS propertyChangeParams = new SP_PROPCHANGE_PARAMS();<br />
<br />
propertyChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;<br />
<br />
propertyChangeParams.ClassInstallHeader.cbSize =<br />
Marshal.SizeOf(typeof(SP_CLASSINSTALL_HEADER));<br />
<br />
propertyChangeParams.StateChange = state;<br />
<br />
propertyChangeParams.Scope = DICS_FLAG_GLOBAL;<br />
<br />
propertyChangeParams.HwProfile = 0;<br />
<br />
int propertyChangeParamsSize = Marshal.SizeOf(propertyChangeParams);<br />
<br />
IntPtr PSP_PROPCHANGE_PARAMS =<br />
Marshal.AllocHGlobal(propertyChangeParamsSize);<br />
<br />
Marshal.StructureToPtr(propertyChangeParams, PSP_PROPCHANGE_PARAMS, true);<br />
<br />
int devInfoSetSize = Marshal.SizeOf(DeviceInfoData);<br />
<br />
IntPtr PSP_DEVINFO_DATA = Marshal.AllocHGlobal(devInfoSetSize);<br />
<br />
Marshal.StructureToPtr(DeviceInfoData, PSP_DEVINFO_DATA, true);<br />
<br />
<br />
bool ok = SetupDiSetClassInstallParams(HwDevInfo, PSP_DEVINFO_DATA,<br />
PSP_PROPCHANGE_PARAMS, propertyChangeParamsSize);<br />
if (ok)<br />
{<br />
ok = SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, HwDevInfo, DeviceInfoData);<br />
if (!ok)<br />
{<br />
int errorCode = Marshal.GetLastWin32Error();<br />
Console.WriteLine("Error (" + errorCode.ToString() + ") + SetupDiSetClassInstaller");<br />
return false;<br />
}<br />
}<br />
else<br />
{<br />
int errorCode = Marshal.GetLastWin32Error();<br />
Console.WriteLine("Error (" + errorCode.ToString() + ") + SetupDiSetClassInstallParams");<br />
return false;<br />
}<br />
return true;<br />
<br />
}<br />
//------ Code Ends Here -----------
|
|
|
|
|
Change the declaration for the function. It should look like this:
public static extern bool SetupDiSetClassInstallParamsA(
IntPtr DeviceInfoSet,
SP_DEVINFO_DATA DeviceInfoData,
IntPtr ClassInstallParams,
int ClassInstallParamsSize);
|
|
|
|
|
Hi Ian,
does this code also run from within a windows service? I.e. can a device be enabled/disabled from within a windows service?
Thanks
fx3000se
|
|
|
|
|
I have not tried this code from within a service, but I cannot see why it should not work, except perhaps for security reasons. It should be a quick thing to verify.
|
|
|
|
|
How can i enable or Disable USB Device in C#
The Code below, use to give me the Details regarding USB Devices, now how can i disable it ??
=====================================================================================
oq = new System.Management.ObjectQuery("select * from Win32_PnPEntity where Name='USB Root Hub'");
query = new ManagementObjectSearcher(ms, oq);
queryCollection = query.Get();
//create parent
nodeCollection = createParentNode("USB Ports");
foreach (ManagementObject mo in queryCollection)
{
createChildNode(nodeCollection, "Name: " + mo["Caption"]);
createChildNode(nodeCollection, "DeviceID: " + mo["DeviceID"]);
createChildNode(nodeCollection, "Status: " + mo["Status"]);
}
|
|
|
|
|
Hi,
I managed to enable/disable a device from C#. I must say that I did not find any articles explaining how to do it so I found the solution by trial and error. However it works for me.
I have a SerialPort Multiplexer that creates 3 virtual serial ports that will send/receive data to/from a single physical COM port.
Steps:
0. using System.Runtime.InteropServices;
1. Just above the class definition define the following type:
using DEVINSTID_A = System.String;
public class SerialDevice<br />
{<br />
....
2. Inside the class definition import "cfgmgr32.dll" and "CM_Set_HW_Prof_FlagsA" method as below:
[DllImport("cfgmgr32.dll", EntryPoint = "CM_Set_HW_Prof_FlagsA")]<br />
public static extern void CM_Set_HW_Prof_FlagsA(<br />
DEVINSTID_A szDevInstName,<br />
uint ulConfig,<br />
uint ulValue,<br />
uint ulFlags<br />
);
3. Call the method to enable disable:
- DISABLE
TG.Devices.Enquirer.SerialEnquirer.CM_Set_HW_Prof_FlagsA(@"ROOT\WINMUX\0000", 0, 0x00000001, 0);
- ENABLE
TG.Devices.Enquirer.SerialEnquirer.CM_Set_HW_Prof_FlagsA(@"ROOT\WINMUX\0000", 0, 0x00000000, 0);
The Device Instance ID can be found by goind to Device Manager -> Properties (0f the COM port) -> Details (and select from the combo the Device Instance ID string). It will display the value that you must use when calling the method.
|
|
|
|
|