Disable specific USB Storage Device C# - 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?

Related

MagneticStripeReader.GetDefaultAsync(); returns null

I have a usb connected MSR reader and i am trying to get it by using the sample codes proveded in here. This works fine but the problem is when i add the same code to my app it doesn't work. GetDefaultAsync returns null.
private static MagneticStripeReader _reader = null;
public static async void StartRead()
{
if (await CreateDefaultMagneticStripeReaderObject())
{
....
}
}
private static async Task<bool> CreateDefaultMagneticStripeReaderObject()
{
if (_reader == null)
{
_reader = await MagneticStripeReader.GetDefaultAsync();
if (_reader == null)
return false;
}
return true;
}
My code is like above, very similer to sample but it doesnt work. Also i've added the device capability of pointOfService. So that is not the case.
I was in the exact same situation and I spent the last 5 hours, finally I know what was going on. You are missing a capability in the Package.appxmanifest
'pointOfService' is the capability you want to include. This capability does not show in the UI and therefore I could not find any difference between my broken project and Microsoft's sample project. You can not add that capability using the UI. You have to manually add it by modifying the XML file.
The sample project by Microsoft have it too
https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/MagneticStripeReader/cs/Package.appxmanifest#L53
Make sure the card reader is in HID mode and not Keyboard emulation mode. That was one of my problems.
To do this is really wonky. MagTek has a ActiveX control on their website to assist us... because ActiveX is awful, you can only use it with InternetExplorer (it won't even work with Edge.)
go here in IE: https://www.magtek.com/changemode/
Enable active X when it pops up, and you can change from hid to keyboard and back.

Potential Issue With Getting Hard Drive Serial From a RAID configuration

I currently have a function that returns the hard drive serial of a virtual machine using calls via WMI, which works great when run on an actual physical hard drive. However, when I run the function on a virtual machine with a virtual disk, the hard drive serial always comes back as the same series of 1's and 0's. I am trying to use this technique to identify a specific machine. Is there a more reliable way to retrieve some sort of identifier which identifies the hardware used in a (virtual) machine that will likely not change?
As a note, I have had the MAC Address given to me as a suggestion, but I do not want my software to break if the NIC it is bound to has to be replaced.
I am also concerned with what might return on a system hard drive which is configured via RAID, as this serial needs to be consistent with every call. I do not have a RAID configured system to test this on, however, so I am unsure of what will even be returned in the first place.
EDIT I have figured out a reliable way to lock our software to a virtual machine even if the serial number might not be unique, so the VM portion is no longer an issue. However, I still am unsure of how this might return on with certain RAID configurations, and as stated above, I do not have the luxury of a RAID configured machine to test on, much less several configurations to test. Any assistance on this is very much appreciated.
Here is the HD serial function:
string Win32_Class = string.Empty;
string Win32_Property = string.Empty;
string systemDrive = null;
try
{
systemDrive = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System).Substring(0, 2);
Win32_Class = "Win32_LogicalDisk";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(string.Format("SELECT * FROM {1} WHERE DeviceID='{0}'", systemDrive, Win32_Class)))
{
foreach (ManagementObject logicalDisk in searcher.Get())
{
Win32_Class = "Win32_DiskPartition";
foreach (ManagementObject partition in logicalDisk.GetRelated(Win32_Class))
{
Win32_Class = "Win32_DiskDrive";
foreach (ManagementObject diskDrive in partition.GetRelated(Win32_Class))
{
Win32_Class = "Win32_PhysicalMedia";
foreach (ManagementObject diskMedia in diskDrive.GetRelated(Win32_Class))
{
Win32_Property = "SerialNumber";
mySystemDeviceSerial = diskMedia[Win32_Property].ToString().Trim();
}
}
}
}
}
}
If you want to identify the VM instance, you could use the UUID property of the Win32_ComputerSystemProduct instance. In the real world, this maps to an ID on the motherboard. In a VM, this returns a unique value for each VM configuration, regardless of the drives (but I'm not sure what happens if the VM is cloned or moved).
You could use the serial number of the 'logical' disk. This will change if the disk is repartitioned. If one drive of a redundant RAID setup is changed it won't change. This is something stored at the block level so it won't matter what the actual storage setup is.
You want the VolumeSerialNumber property of a Win32_LogicalDisk for the installation volume.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx

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)

How can I check for 3G, wifi, EDGE, Cellular Networks in Windows Phone 7?

How can I check for 3G, wifi, EDGE, Cellular Networks in Windows Phone 7 using C#?
If you can use the Mango (7.1) SDK, and if your scenario involves using sockets, there's a trivial way to get the NetworkInterfaceType/SubType information for the connection you just made:
NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface();
var type = netInterfaceInfo.InterfaceType;
var subType = netInterfaceInfo.InterfaceSubtype;
No need to use the NetworkInterface.NetworkInterfaceType property (which notoriously takes up to 30sec to return); no need to trigger a hostname resolution just to determine the network type; no need to listen to network change events.
Of course, this works best in conjunction with DeviceNetworkInformation.IsNetworkAvailable or NetworkInterface.GetIsNetworkAvailable() - those calls return immediately whether you're on a network or not. If you are, you connect the socket first and ask questions when it's connected :-)
A final note: beware of Mango's DeviceNetworkInformation.IsWiFiEnabled - I thought it would return whether I was on a wifi network, but instead it returns whether wifi is turned on or off in the phone settings... not super useful.
take a look at phoney tools, they have class PhoneNetworking for this:
http://wildermuth.com/2011/03/05/Phoney_Tools_Updated_(WP7_Open_Source_Library)
its open source you can check the source code
As of the Mango release (beta 2 and RC), this information is now available but it requires you to actually make a connection, presumably because it doesn't check until something needs it.
You can either perform a DNS resolution (see below) or use the GetCurrentNetworkInterface WebRequest extension method, which will throw an InvalidOperationException if the request hasn't connected yet.
There are also some events to follow in the Microsoft.Phone.Net.NetworkInformation namespace, but I wouldn't be surprised if those events didn't fire until a connection was made.
Interestingly, it seems you can also prefer or require on a per-connection basis using the SetNetworkPreference and SetNetworkRequirement extension methods, though it doesn't go beyond wifi vs cellular.
DeviceNetworkInformation.ResolveHostNameAsync(
new DnsEndPoint("microsoft.com", 80),
new NameResolutionCallback(nrr =>
{
var info = nrr.NetworkInterface;
var type = info.InterfaceType;
var subType = info.InterfaceSubtype;
}), null);
The enumeration values for NetworkInterfaceType (wifi/gsm) and NetworkInterfaceSubType (edge/3g) are available on MSDN.
Without socket:
var currentList = new NetworkInterfaceList().Where(i => i.InterfaceState == ConnectState.Connected).Select(i => i.InterfaceSubtype);
if (currentList.Contains(NetworkInterfaceSubType.WiFi))
Debug.WriteLine("WiFi");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
NetworkInterfaceSubType.Cellular_EVDO,
NetworkInterfaceSubType.Cellular_3G,
NetworkInterfaceSubType.Cellular_HSPA,
NetworkInterfaceSubType.Cellular_EVDV,
}).Any())
Debug.WriteLine("3G");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
NetworkInterfaceSubType.Cellular_GPRS,
NetworkInterfaceSubType.Cellular_1XRTT,
NetworkInterfaceSubType.Cellular_EDGE,
}).Any())
Debug.WriteLine("2G");
Unfortunately the api's don't provide very limited information about the kind of network connection you have. You can tell if you are on 3G, Cellular or Ethernet (i.e. USB connection to PC) but that is all the information you get.
Check out this for more info Better way to check for an network connection on WP7
To get Network Data for windows phone app i.e it is connected to a ethernet, wifi or cellular network also getting the subtype i.e 2G or 3g network following program can be used.
Using Microsoft.Phone.Net.NetworkInformation
Using Microsoft.Phone.net.NetworkInfromation
var Newlist = new NetworkInterfaceList();
foreach (NetworkInterfaceInfo x in Newlist)
{
if(x.InterfaceState==ConnectState.Connected)
{
if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.WiFi))
{
Interface = x.InterfaceType.ToString();
SubInterface = x.InterfaceSubtype.ToString();
break;
}
else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDO) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_3G) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_HSPA) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDV))
{
Interface = x.InterfaceType.ToString();
SubInterface= “3G Network”;
break;
}
else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_GPRS) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_1XRTT) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EDGE))
{
Interface = x.InterfaceType.ToString();
SubInterface= “2G Network”;
break;
}
else
{
Interface = “Ethernet”;
SubInterface= “Unknown” ;
break;
}
}
else
{
Interface=”not connected”;
SubInterface=”unknown”;
}
Here, Interface and SubInterface gives the network information.

How to check the machine type? laptop or desktop?

How to check current machine type? laptop or desktop ?
I got this from http://blog.csdn.net/antimatterworld/archive/2007/11/11/1878710.aspx ,it works well on my home machine(Win2003 on laptop), it returns "Portable", but failed on my work machine(Vista on laptop), it returns "Other".
here is the code:
public enum ChassisTypes
{
Other = 1,
Unknown,
Desktop,
LowProfileDesktop,
PizzaBox,
MiniTower,
Tower,
Portable,
Laptop,
Notebook,
Handheld,
DockingStation,
AllInOne,
SubNotebook,
SpaceSaving,
LunchBox,
MainSystemChassis,
ExpansionChassis,
SubChassis,
BusExpansionChassis,
PeripheralChassis,
StorageChassis,
RackMountChassis,
SealedCasePC
}
public static ChassisTypes GetCurrentChassisType()
{
ManagementClass systemEnclosures = new ManagementClass("Win32_SystemEnclosure");
foreach (ManagementObject obj in systemEnclosures.GetInstances())
{
foreach (int i in (UInt16[ ])(obj["ChassisTypes"]))
{
if (i > 0 && i < 25)
{
return (ChassisTypes)i;
}
}
}
return ChassisTypes.Unknown;
}
Here's a good Microsoft article that suggests looking at a few other WMI classes to get a better idea of whether the computer is a laptop or desktop:
http://technet.microsoft.com/en-us/library/cc180825.aspx
Win32_SystemEnclosure, ChassisTypes(1)=10
Win32_Battery or Win32_PortableBattery
Win32_PCMCIAController
Win32_DriverVXD.Name = "pccard"
Win32_ComputerSystem.Manufacturer
Win32_ComputerSystem.Model
And it also suggests to look in the registry for the Power scheme.
Well, I may be attempting to raise the dead here, but I would suggest that the most reliable method of determining a laptop would be the present of a lid status switch.
See GetPwrCapabilities and System_power_Capabilities
There is no need for both Other and Unknown check.
Change the condition to i > 1 && i < 25 and remember ChassisTypes is an array where OS returns what ever he thinks your system is.
It is possible to match more than single type. Your code only returns the first match.
See http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0921.mspx and http://msdn.microsoft.com/en-us/library/aa387204(VS.85).aspx which states that:
This documentation is derived from the CIM class descriptions published by the DMTF.
Presumably, that means the manufacturer had to provide some information for Windows to be able to discover it.
I don't think there is a definitive right answer for this and I've found WMI unreliable for it but I have been using the Win32 function GetSystemPowerStatus() to determine if there is a system battery, obviously system battery == portable computer.
I've never tried this on a desktop with a UPS though?!
I am pretty sure that this will depend on whether the manufacturer has bothered to provide that data on the current chipset. If they have not, then "Other" is the best you can do.
Check out this somewhat related article, which also suggests querying the BIOS directly.
In order to check if machine is laptop or desktop you can try to check battery status, using SystemPowerCapabilites.LidPresent or both of them.
Battery:
if(SystemInformation.PowerStatus.BatteryChargeStatus ==BatteryChargeStatus.NoSystemBattery){
//desktop
}
else{
//laptop
}
SystemPowerCapabilites.LidPresent:
public SYSTEM_POWER_CAPABILITIES getSystemPowerCapabilites(){
{
SYSTEM_POWER_CAPABILITIES systemPowerCapabilites;
GetPwrCapabilities(out systemPowerCapabilites);
return systemPowerCapabilites;
}
getSystemPowerCapabilites().LidPresent;
GetPwrCapabilities definition: http://www.pinvoke.net/default.aspx/powrprof/GetPwrCapabilities.html
read registry key from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\pcmcia, the ‘Start’ value, it's laptop if start =0, otherwise it's desktop machine if start doesn't exist or start != 0.

Categories

Resources