Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

WMIGatherer - WMI wrapper and system information gathering library

In a lot of my projects I work with WMI, usually to get informations about something. For me it was always time consuming and nerve wrecking to add a proper WMI implementation. That's basically why I made WMIGatherer and thought sharing it might be a good idea. It's a library that makes working with WMI in C# easier. I don't like to call it WMI wrapper, but I could not find a proper word to describe it. It also contains a "Gathering"-namespace that provides methods for information gathering purposes through WMI.

Just to give you a basic understanding what you can do with it:
(How things work and how to use it exactly is explained in the README file on GitHub.)


Querying Part

Query a single property:
Spoiler (Click to View)

Query multiple properties / multiple classes:
Spoiler (Click to View)


Information Gathering Part

Get installed antivirus, antispyware, firewall:
Spoiler (Click to View)

Create a HWID according to your desires:
Spoiler (Click to View)


This is just a brief introduction, you can do a lot more. I would like this little project to become bigger and bigger. So, if somebody wants to have something added (any awesome WMI class or whatever), TELL ME!
Also, if unwanted exceptions or issues occur, please tell me. I tested it, but I'm sure that somewhere I forgot something, so let me know.
Before I forget, don't blame me for not using the 'var' keyword inside my code. I simply hate it, though some say it's wonderful.

GitHub: github.com/chrizator/WMIGatherer
(I can't post links :/)
wmi is good to do hard things easy the only problem is speed
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jan 31, 2018 02:03 PM)HFDone Wrote:  wmi is good to do hard things easy the only problem is speed

That's true. WMI should be used when there is no way around it or speed doesn't matter.
Does this work for .NET 2.0?
I know that .NET 2.0 doesn't support querying WMI, or at least I've heard.
(Jan 31, 2018 02:11 PM)Hoodie+ Wrote:  Does this work for .NET 2.0?
I know that .NET 2.0 doesn't support querying WMI, or at least I've heard.

Well, WMI (System.Management) works under .NET 2.0, but this project requires at least .NET 3.5.
Its been a while since someone released code that I enjoyed reading through. This is some good code. Thanks for the release, buddy!

(Jan 31, 2018 02:11 PM)Hoodie+ Wrote:  Does this work for .NET 2.0?
I know that .NET 2.0 doesn't support querying WMI, or at least I've heard.

Pretty sure WMI itself works under 2.0, but not sure if OPs project works under 2.0.
(Jan 31, 2018 09:49 PM)Ninja Wrote:  Its been a while since someone released code that I enjoyed reading through. This is some good code. Thanks for the release, buddy!


Pretty sure WMI itself works under 2.0, but not sure if OPs project works under 2.0.

Cheers, Ninja. 🤝
Thank you for releasing this. I'll be putting it to use.
Just FYI you can do expression bodied properties:
Code
public bool IsReadOnly => false;

Instead of this in C#:
Code
public bool IsReadOnly
{
    get => false;
}

And your WmiClass could make use of some ref returns (C#7)
Code
public WmiProperty[] Properties { get; }

public ref WmiProperty this[string propertyName]
{
    get
    {
        for (int i = 0; i < Properties.Length; i++)
        {
            if (Properties[i].Name.Equals(propertyName, StringComparison.Ordinal))
            {
                return ref Properties[i];
            }
        }
        
        string ex_msg = $"The specified property '{propertyName}' does not exist.";
        throw new IndexOutOfRangeException(ex_msg);
    }
}

Also much better to use Equals() for string comparison and define your comparison.

You've overcomplicated some of your util functions though such as GetHexString().

Not terrible code overall though. For something like this though you could probably make more use of abstract classes and interfaces for something which definitely has a hierarchy.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Interested seeing your own approach to this tomorrow. Can I assume that this your own implementation of Microsoft's Scriptomatic tool, WMI Code Creator?

I enjoy working with WMI when speed is not of importance. It's rich in information although most examples out there are poorly written and need updating. Which is costly in time for me. In a small number of cases, it can be known to be extremely iffy.
(Feb 3, 2018 12:41 PM)0xDEAD10CC Wrote:  Just FYI you can do expression bodied properties:
Code
public bool IsReadOnly => false;

Instead of this in C#:
Code
public bool IsReadOnly
{
   get => false;
}

And your WmiClass could make use of some ref returns (C#7)
Code
public WmiProperty[] Properties { get; }

public ref WmiProperty this[string propertyName]
{
   get
   {
       for (int i = 0; i < Properties.Length; i++)
       {
           if (Properties[i].Name.Equals(propertyName, StringComparison.Ordinal))
           {
               return ref Properties[i];
           }
       }
       
       string ex_msg = $"The specified property '{propertyName}' does not exist.";
       throw new IndexOutOfRangeException(ex_msg);
   }
}

Also much better to use Equals() for string comparison and define your comparison.

You've overcomplicated some of your util functions though such as GetHexString().

Not terrible code overall though. For something like this though you could probably make more use of abstract classes and interfaces for something which definitely has a hierarchy.

Thanks for the advice. It's great to have people on hear that actually read code and share their experience. I'll try to adapt my code ASAP.
Btw, I'm wondering how much of a difference Equals() makes instead of comparing the object references.
(Feb 4, 2018 05:08 AM)chrizator Wrote:  Thanks for the advice. It's great to have people on hear that actually read code and share their experience. I'll try to adapt my code ASAP.
Btw, I'm wondering how much of a difference Equals() makes instead of comparing the object references.

You're not comparing references when you use == on strings because it's overloaded.

Increased speed and recommended by Microsoft since 2005: https://msdn.microsoft.com/en-us/library...2147217396
Avoid problems like this from time to time with other reference types: https://blogs.msdn.microsoft.com/csharpf...se-equals/

It's more of a common habit for developers as well and makes it explicit as to what kind of string comparison you're doing. Also, if you want to compare references there's already a ReferenceEquals function too so you don't have to worry about this overloading. It's okay to use == and others on value types though as they don't overload these operators by default.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Feb 4, 2018 08:49 AM)0xDEAD10CC Wrote:  You're not comparing references when you use == on strings because it's overloaded.

Increased speed and recommended by Microsoft since 2005: https://msdn.microsoft.com/en-us/library...2147217396
Avoid problems like this from time to time with other reference types: https://blogs.msdn.microsoft.com/csharpf...se-equals/

It's more of a common habit for developers as well and makes it explicit as to what kind of string comparison you're doing. Also, if you want to compare references there's already a ReferenceEquals function too so you don't have to worry about this overloading. It's okay to use == and others on value types though as they don't overload these operators by default.

Oh, that's interesting to know. Thanks for the clarification!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by phisnNov 11, 2020 08:44 AM 1 165 Nov 11, 2020 08:44 AM
Last Post: HFDone
[C#] by LaTortueJun 11, 2020 10:05 AM 3 245 Jun 11, 2020 10:05 AM
Last Post: LaTortue
[Java] by PDGLOBALJan 11, 2020 12:01 PM 2 231 Jan 11, 2020 12:01 PM
Last Post: PDGLOBAL