Anoop
Anoop

Reputation: 41

Issues in Finding Windows Activation through SoftwareProductLicensing Class

I am using the below code to determine the activation of the Widnows7. I am getting 7 instances/product. I am not clear which product refers to original Windows 7.

I am unable to find documentation on to check which instance to determine whether Windows is activated or not

        //use a SelectQuery to tell what we're searching in
        SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct");

        //set the search up
        ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);

        //get the results into a collection
        using (ManagementObjectCollection obj = searcherObj.Get())
        {
            foreach (ManagementObject m in obj)
        {
            if (Convert.ToInt32(m["GracePeriodRemaining"].ToString()) == 0)
            {
                MessageBox.Show("Windows is active");
                break;
            }
            else
            {
                MessageBox.Show(" Windows is not active");
                break;
            }
        }
            //now loop through the collection looking for
            //an activation status

        }

Upvotes: 2

Views: 1643

Answers (2)

Anoop
Anoop

Reputation: 41

Windows® 7, VOLUME_KMSCLIENT channel refers to the actual product. this can found in description property

    string ComputerName = "localhost";
    ManagementScope Scope;                
    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

     Scope.Connect();
     SelectQuery searchQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct");

        //set the search up
        ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(Scope, searchQuery);

        //get the results into a collection
        using (ManagementObjectCollection obj = searcherObj.Get())
        {

            foreach (ManagementObject m in obj)
            {
                String s = m["Description"].ToString();
                if ((m["Description"].ToString()) .Contains("VOLUME_KMSCLIENT channel"))
                {
                    if (m["LicenseStatus"].ToString() == "1")
                    {
                        var gracePeriod = Convert.ToInt32(m["GracePeriodRemaining"]) / (60 * 24);
                        MessageBox.Show("Not Licensed " + " Grace Period Remaining--" + gracePeriod.ToString() + "days");

                    }
                    else
                    {
                        var gracePeriod = Convert.ToInt32(m["GracePeriodRemaining"]) / (60 * 24);
                        MessageBox.Show("Not Licensed " + " Grace Period Remaining--" + gracePeriod.ToString() + "days");
                    }
                }

            }
        }

Upvotes: 0

RRUZ
RRUZ

Reputation: 136391

When you use the SoftwareLicensingProduct WMI class, more than an instance is returned due to the Volume Licensing feature introduced in Windows Vista, so to return only the instance of you Windows version you must filter the WQL sentence using the PartialProductKey, ApplicationId and LicenseIsAddon properties.

Try this sample

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM SoftwareLicensingProduct Where PartialProductKey <> null AND ApplicationId='55c92734-d682-4d71-983e-d6ec3f16059f' AND LicenseIsAddon=False");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}", "Name", (String)WmiObject["Name"]);
                    Console.WriteLine("{0,-35} {1,-40}", "GracePeriodRemaining", (UInt32) WmiObject["GracePeriodRemaining"]);// Uint32
                    switch ((UInt32)WmiObject["LicenseStatus"])
                    {
                        case 0: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Unlicensed");
                                break;
                        case 1: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Licensed");
                                break;
                        case 2: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Out-Of-Box Grace Period");
                                break;
                        case 3: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Out-Of-Tolerance Grace Period");
                                break;
                        case 4: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "on-Genuine Grace Period");
                                break;
                        case 5: Console.WriteLine("{0,-35} {1,-40}", "LicenseStatus", "Notification");
                                break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

Another option is use the SLIsGenuineLocal function.

Try the answer to this question Determine Genuine Windows Installation in C# for a C# sample.

Upvotes: 2

Related Questions