9

If I wanted to determine if a user had a genuine copy of windows, how could I do that in C#? Can I integrate with Windows Genuine Advantage?

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Michael
  • 199
  • 1
  • 8

2 Answers2

20

You can use SLIsGenuineLocal (Checks whether the specified application is a genuine Windows installation), The Minimum supported client is Windows Vista.

The Software Licensing API , The Software Licensing API (SLAPI) can be used to determine a genuine Microsoft Windows installation, install and log an asset management license, and retrieve information about the licensing policy of a software component.

UPDATE, I Wrote this basic C# implementation

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication11
{
  using SLID = Guid; //SLID id declarated as typedef GUID SLID; in slpublic.h 

  class Program
  {

    public enum SL_GENUINE_STATE
    {
      SL_GEN_STATE_IS_GENUINE       = 0,
      SL_GEN_STATE_INVALID_LICENSE  = 1,
      SL_GEN_STATE_TAMPERED         = 2,
      SL_GEN_STATE_OFFLINE          = 3,
      SL_GEN_STATE_LAST             = 4
    }

    [DllImportAttribute("Slwga.dll", EntryPoint = "SLIsGenuineLocal", CharSet = CharSet.None, ExactSpelling = false, SetLastError = false, PreserveSig = true, CallingConvention = CallingConvention.Winapi, BestFitMapping = false, ThrowOnUnmappableChar = false)]
    [PreserveSigAttribute()]
    internal static extern uint SLIsGenuineLocal(ref SLID slid, [In, Out] ref SL_GENUINE_STATE genuineState, IntPtr val3);


    public static bool IsGenuineWindows()
    {
        bool _IsGenuineWindows   = false;
        Guid ApplicationID       = new Guid("55c92734-d682-4d71-983e-d6ec3f16059f"); //Application ID GUID http://technet.microsoft.com/en-us/library/dd772270.aspx
        SLID windowsSlid = (Guid)ApplicationID;  
        try
        {
          SL_GENUINE_STATE genuineState = SL_GENUINE_STATE.SL_GEN_STATE_LAST;
          uint ResultInt                = SLIsGenuineLocal(ref windowsSlid, ref genuineState, IntPtr.Zero);
          if (ResultInt == 0)
          {
            _IsGenuineWindows = (genuineState == SL_GENUINE_STATE.SL_GEN_STATE_IS_GENUINE);
          }
          else
          {
            Console.WriteLine("Error getting information {0}", ResultInt.ToString());          
          }

        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.Message);
        }
        return _IsGenuineWindows;
    }    

    static void Main(string[] args)
    {
      if (Environment.OSVersion.Version.Major >= 6) //Version 6 can be Windows Vista, Windows Server 2008, or Windows 7
      {
        if (IsGenuineWindows())
        {
          Console.WriteLine("Original Windows");        
        }
        else
        {
          Console.WriteLine("Not Original Windows");                
        }
      }
      else
      {
        Console.WriteLine("OS Not supoprted");     
      }
      Console.ReadLine();
    }
  }
}
volody
  • 6,946
  • 3
  • 42
  • 54
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • +1. Where did `55c92734-d682-4d71-983e-d6ec3f16059f` come from? – Billy ONeal Nov 13 '12 at 21:53
  • @BillyONeal this Windows APPID is part of the Volume Licensing feature introduced in Windows Vista, you can find more information here http://technet.microsoft.com/en-us/library/dd772269.aspx – RRUZ Nov 13 '12 at 22:08
  • @RRUZ: That GUID doesn't appear to exist in that document. (I checked before asking) – Billy ONeal Nov 14 '12 at 02:36
  • 1
    @BillyONeal, this value is present in the [`Slmgr.vbs`](http://technet.microsoft.com/en-us/library/ff793433.aspx) file which is the `Windows Software Licensing Management Tool` , this script is located in the `c:\windows\system32` folder, now inside of that file check the `WindowsAppId` constant :) – RRUZ Nov 14 '12 at 03:54
  • Thank you very much for your answer. – Sajeeb Chandan Saha Mar 24 '20 at 19:04
1

Update

When I answered this question, I was answering in the context of the asker's language (C#). As another answerer pointed out, there is a way to do this in other languages, but it's platform dependent (Vista and above, from what I can see).

Old answer (Clarified)

As far as I know, there is no API documentation available for WGA (still true), and since hackers could conceivably (and have) break WGA by knowing its internals, I doubt Microsoft is going to open up an API for WGA.

Microsoft has a separate API for C++ and COM developement as this answer points out, but nothing available in the .NET Framework that I can see.

Realistically, there's no good reason to find this out. It's not the business of the program to know if the OS is legally installed.

Edit

Leaving my answer up since I cannot delete it; but if you want to use Win32 API and don't have to stay inside the .NET Framework, I suggest using this answer.

Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • Thank you George, I didnt think of it that way... Although it would be nice to know that some pirate isnt using your software on a pirated system, and who knows, they may be pirating your software as well... – Michael Oct 12 '09 at 02:25
  • -1, RRUZ's response shows that there is in fact a documented API for checking the validity status of a Windows installation. – nobody Oct 12 '09 at 03:07
  • For C#, it appears there is no way to; that was his question. I didn't mean a blanket 'for all languages'; and I ought to edit my answer to reflect that. – George Stocker Oct 12 '09 at 03:47
  • George see the C# implementation wich added – RRUZ Oct 12 '09 at 04:54