2

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

        }
RRUZ
  • 134,889
  • 20
  • 356
  • 483
Anoop
  • 41
  • 1
  • 4

2 Answers2

2

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.

Community
  • 1
  • 1
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • My Problem is that there are 7 entries... but even one it not licensed it turns out to be not license when I test it on my local PC. But my local PC is licensed as per Computer info. I am trying to locate which of the 7 instances is the correct reference for the OS regarding license information – Anoop Oct 20 '12 at 15:03
  • Are you tried the code of my answer? because this code filter the WMI instances to retrieve only the local windows instance. – RRUZ Oct 23 '12 at 03:27
0

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");
                    }
                }

            }
        }
Anoop
  • 41
  • 1
  • 4