Detect number of display monitor - c#

I already tried Screen.AllScreen, SystemInformation.MonitorCount, and WMI but all of them failed.
My application runs as a windows service, hence no visual Form or UI. Both Screen.AllScreen and SystemInformation.MonitorCount returns 1 even when I have 2 monitors. If I run my application in console, it returns the correct count of display but my requirement is that my application to run as a windows service (no UI).
Thanks!

Found answer to my own question.
Still end up using WMI.
I was initially using Win32_DesktopMonitor giving a non-reliable answer.
Using this query:
"SELECT * FROM Win32_PnPEntity WHERE Service = 'monitor'"
WMI returns the correct monitor instance connected to my PC.

I went with Win32_PnPEntity because it represents the information for Plug and Play Devices in your Device Manager, which will show you when you have a monitors plugged in. The way the query works in "searcher" is more likely to be accurate than others since it uses the Like Operator. I do this because on 3 different computers the monitor entries in Device manager appeared differently. E.g. (Pnp-Monitor, Pnp Monitor (Standard), Generic Pnp Monitor).
private int CountMonitorsInstalled()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "select * from Win32_PnPEntity WHERE Name LIKE '%PnP%Monitor%'");
return searcher.Get().Count;
}
catch(Exception ex)
{
return 0;
}
}

Related

SMBIOS - Get SPD (Serial Presence Detect) Modules Information C#

I searched a lot but did not find any working codes getting SPD tables information via C#. Out there there are lots of softwares which get this info but HOW?
as shown in the image, for RAM devices, you can see Manufacture's name which can not be retrieve at all by WMI etc
If there is a DLL for using in C# will be perfect also
After some Research found this:
https://github.com/sapozhnikovay/SMBIOS
but it can not read table 17 to get memory device information.
Once I was researching about this, you need to get this information through SMBUS (not SMBIOS). But you need to create a driver (WDM in C/C++) to access this information.
Make sure you have added System.Management as a reference.
Here is a string that will return almost any information you want from the component :
private string getComponent(string hwClass, string syntax)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwClass);
foreach (ManagementObject mj in mos.Get())
{
return Convert.ToString(mj[syntax]);
}
return null;
}
Using the string would look like this, say on a button click :
label1.Text = getComponent("Win32_PhysicalMemory", "SerialNumber");
I tested it and it returned a serial number, you can also look at the list of things you can put in like manufacturer, name, capacity etc.
I got all of this information from this YouTube video.
You can find all of the devices and their properties here (CPU, GPU, etc.)

How to identify monitor or projector connect to pc?

I am writing a small C# application to identify which type of display connect to pc
A Monitor
A TV screen
A projector
I try to do with
Screen.AllScreens
EnumDisplayDevices()
but it does not help anything.
When I use
var mbs = new ManagementObjectSearcher("Select * From CIM_LogicalDevice");
ManagementObjectCollection mbsList = mbs.Get();
I can get device id:
DISPLAY\OTMFB0E\4&9C24ACE&0&UID16843008
DISPLAY\DELD058\4&9C24ACE&0&UID50725632
But there is not any value which is used to identify between a normal monitor and a projector.
Is there any way to do?
Or which information will be different of these display types?

Disable specific USB Storage Device C#

I am working on a project that needs to enable/disable specific USB storage devices.
My app gets all storage USB DeviceIDs, and depending on saved settings then needs to allow the device or not. I have used the code from this previous question for the enable/disable, and call it like so:
DisableHardware.DisableDevice(n => n.ToUpperInvariant().Contains(VidPid), true);
with string VidPid = "VID_8564&PID_1000";.
I have stepped through the code, it all works perfectly (as far as I can tell) until the SetupDIChangeState call, which then returns -536870389 (E000020B as hex) as error code (from Marshall.GetLastWin32Error()).
Apparently this refers to either 1) the device not being present (which as far as I understand is not the case here, as all other calls in this class work fine, and I get `VidPid' from
private static List<WMUBClasses.USBDeviceInfo> GetUSBDevices()
{
try
{
List<WMUBClasses.USBDeviceInfo> tList = new List<WMUBClasses.USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(#"SELECT * FROM Win32_USBHub"))
{
collection = searcher.Get();
}
foreach (var device in collection)
{
if (!device.GetPropertyValue("Description").ToString().Contains("Storage"))
{
continue;
}
tList.Add(new WMUBClasses.USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
return tList;
}
catch (Exception ex)
{
return null;
}
}
or 2) an incorrect build platform, but I have tried all the different combinations (Any CPU, Mixed Platforms, x86 and x64), they all return the same result.
I have also looked at this which is another approach to my problem (by creating and using a kernel mode filter driver), it just seems like killing a fly with a wrecking ball. To be honest I have no clue of how to go about using this (for someone that hasn't done any driver development it looks super intimidating, especially after having read some of the available documentation.)
Should I (A) keep using the SetupDi API calls to achieve my goal and if so, can anyone see what is wrong with the code or how I am using it? If not (A), should I (B) use the filter driver approach instead and if so, any pointers?
As stated in the header, I want to disable specific USB storage devices, so as far as I understand, this precludes using the Registry to disable ALL USB storage devices. So if neither of the above, does anyone have any other direction I should be looking at instead?

List all USB audio headsets connected to a Windows PC

I want to use C# to retrieve the USB headset devices connected to PC. I tried the below solutions but didn't work:
Solution 1:
How to enumerate audio out devices in c#
I tried this but the device name appears as "(Generic USB Audio)" and not the actual name.
Solution 2:
How to get the default audio device?
Solution 3:
Detecting Audio Input & output devices connected to system
Solution 2 and Solution 3 gave me the below result:
The device name is truncated to 31 characters.
Eg: "Microphone (Sennheiser VOICE 689"
****Question: Is there any way I can get the complete name of the device?****
If you know it's an USB audio device, and assuming the driver is correctly written for the device, you could do:
foreach (ManagementObject drive in
new ManagementObjectSearcher(
"select Name from Win32_USBDevice where Service='usbaudio'").Get())
{
{
string s = drive["Name"].ToString();
// Continue
}
}
Addition
You're only getting 31 characters (technically 32) because the PInvoke to the native .DLLs use a char[32], so it can't return more than that; you won't get what you need from solution 1 & 2.
Also, I don't know why you can't use Win32_USBDevice, as I'm also using Win7 x64 and I'm having no problems. This link might help you.
Possible Alternate
You might be able to use the Win32_PnPEntity class:
foreach (ManagementObject drive in
new ManagementObjectSearcher(
"select Name from Win32_PnPEntity where Service='usbaudio'").Get())
{
{
string s = drive["Name"].ToString();
// Continue. Can look at Description, Caption, etc. too
}
}

Is there really any way to uniquely identify any computer at all

I know there are a number of similar questions in stackoverflow such as the followings:
What's a good way to uniquely identify a computer?
What is a good unique PC identifier?
Unique computer id C#
WIN32_Processor::Is ProcessorId Unique for all computers
How to uniquely identify computer using C#?
... and dozens more and I have studied them all.
The problem is that some of the accepted answers have suggested MAC address as an unique identifier which is entirely incorrect. Some other answers have suggested to use a combination of various components which seems more logical. However, in case of using a combination it should be considered which component is naturally unlikely to be changed frequently. A few days ago we developed a key generator for a software licensing issue where we used the combination of CPUID and MAC to identify a windows pc uniquely and till practical testing we thought our approach was good enough. Ironically when we went testing it we found three computers returning the same id with our key generator!
So, is there really any way to uniquely identify any computer at all? Right now we just need to make our key generator to work on windows pc. Some way (if possible at all) using c# would be great as our system is developed on .net.
Update:
Sorry for creating some confusions and an apparently false alarm. We found out some incorrectness in our method of retrieving HW info. Primarily I thought of deleting this question as now my own confusion has gone and I do believe that a combination of two or more components is good enough to identify a computer. However, then I decided to keep it because I think I should clarify what was causing the problem as the same thing might hurt some other guy in future.
This is what we were doing (excluding other codes):
We were using a getManagementInfo function to retrieve MAC and Processor ID
private String getManagementInfo(String StrKey_String, String strIndex)
{
String strHwInfo = null;
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + StrKey_String);
foreach (ManagementObject share in searcher.Get())
{
strHwInfo += share[strIndex];
}
}
catch (Exception ex)
{
// show some error message
}
return strHwInfo;
}
Then where needed we used that function to retrieve MAC Address
string strMAC = getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress");
and to retrieve ProcessorID
string strProcessorId = getManagementInfo("Win32_Processor", "ProcessorId");
At this point, strMAC would contain more than one MAC address if there are more than one. To take only one we just took the first 17 characters (12 MAC digits and 5 colons in between).
strMAC = strMAC.Length > 17 ? strMAC.Remove(17) : strMAC;
This is where we made the mistake. Because getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress") was returning a number of extra MAC addresses that were really in use. For example, when we searched for MAC addresses in the command prompt by getmac command then it showed one or two MAC addresses for each pc which were all different. But getManagementInfo("Win32_NetworkAdapterConfiguration", "MacAddress") returned four to five MAC addresses some of which were identical for all computers. As we just took the first MAC address that our function returned instead of checking anything else, the identical MAC addresses were taken in strMAC incidently.
The following code by Sowkot Osman does the trick by returning only the first active/ enabled MAC address:
private static string macId()
{
return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
}
private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
if (mo[wmiMustBeTrue].ToString() == "True")
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
}
return result;
}
//Return a hardware identifier
private static string identifier(string wmiClass, string wmiProperty)
{
string result = "";
System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
return result;
}
However, I was absolutely right about the identical Processor ID issue. All three returned the same Processor ID when we put wmic cpu get ProcessorId command in their command prompts.
Now we have decided to use Motherboard serial number instead of Processor ID to make a combination with MAC address. I think our purpose will be served with this way and if it doesn't in some cases then we should let it go in those few cases.
How about adding motherboard serial number as well e.g.:
using System.management;
//Code for retrieving motherboard's serial number
ManagementObjectSearcher MOS = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
foreach (ManagementObject getserial in MOS.Get())
{
textBox1.Text = getserial["SerialNumber"].ToString();
}
//Code for retrieving Processor's Identity
MOS = new ManagementObjectSearcher("Select * From Win32_processor");
foreach (ManagementObject getPID in MOS.Get())
{
textBox2.Text = getPID["ProcessorID"].ToString();
}
//Code for retrieving Network Adapter Configuration
MOS = new ManagementObjectSearcher("Select * From Win32_NetworkAdapterConfiguration");
foreach (ManagementObject mac in MOS.Get())
{
textBox3.Text = mac["MACAddress"].ToString();
}
The fact in getting a globally unique ID is, only MAC address is the ID that will not change if you set up your system all over. IF you are generating a key for a specific product, the best way to do it is assigning unique IDs for products and combining the product ID with MAC address. Hope it helps.
I Completely agree with just the above comment.
For Software licensening, you can use:
Computer MAC Address (Take all if multiple NIC Card) + Your software Product Code
Most of the renowned telecom vendor is using this technique.
However, I was absolutely right about the identical Processor ID
issue. All three returned the same Processor ID when we put wmic cpu
get ProcessorId command in their command prompts.
Processor ID will be same if all the systems are running as virtual machines on the same hypervisor.
MAC ID seems fine. Only thing is users must be provided the option to reset the application, in case the MAC changes.
It looks like custom kitchen is the way for that.
SMBIOS UUID (motherboard serial) is not robust, but works fine in 99% cases. However some brands will set the same UUID for multiple computers (same production batch maybe). Getting it requires WMI access for the user (if he's not administrator), you can solve that by starting an external process asking administrator priviledges (check codeproject.com/Articles/15848/WMI-Namespace-Security)
Windows Product ID might be good, but I read it could be identical in some circumstances (https://www.nextofwindows.com/the-best-way-to-uniquely-identify-a-windows-machine)
Could someone clarify if the same Product ID (not product key) might be present on multiple computers ?
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid seems interesting. It's generated when installing Windows and if changed, it requires to reactivate Windows.
Mac Addresses are interresting but you can only take the first one or your unique ID will change when the interface is disabled, or when another network interface is added and appears first etc.
Hard Drive serial number is nice but when installing a ghost, it might also override the serial number from the original drive... And the HD serial is very easy to change.
The best might be to generate an ID with a combination of those machine identifiers and decide if the machine is the same by comparing those identifiers (ie if at least one Mac address + either SMBIOS UUID or Product ID is ok, accept)

Categories

Resources