using System;
using COMTLI = System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;

/// This class implements the TLBINF32 methods used
/// by the ScanTool sample.  It was created by adding content
/// onto the stub class authored by gmStudio for 
/// ScanToolLib.
namespace TLI
{
   /// <summary>
   /// PInvoke methods.
   /// </summary>
   internal class NativeMethods
   {
      [DllImport("oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
      internal static extern COMTLI.ITypeLib LoadTypeLibEx(string szFile, REGKIND regkind);
   }

   // REGKIND enumeration used by LoadTypeLibEx
   internal enum REGKIND
   {
      REGKIND_DEFAULT = 0,
      REGKIND_REGISTER = 1,
      REGKIND_NONE = 2
   }
      
   public class TypeLibInfo
   {
   
      private COMTLI.ITypeLib typLib = null;
   
      public TypeLibInfo(COMTLI.ITypeLib newTypeLibInfo)
      {
         typLib = newTypeLibInfo;
      }
      
      public string Name
      {
         get
         {
            return Marshal.GetTypeLibName(typLib);
         }
      }
      public string GUID
      {
         get
         {
            string fmtGUID = "{" + Marshal.GetTypeLibGuid(typLib).ToString().ToUpper() + "}";
            return fmtGUID;
         }
      }
   }
   public class TLIApplication
   {

      public TLI.TypeLibInfo TypeLibInfoFromFile(string FileName)
      {
        // Load the old type library without registering it
        return new TLI.TypeLibInfo(NativeMethods.LoadTypeLibEx(FileName, REGKIND.REGKIND_NONE));
      }
      
   }
   public enum TliErrors
   {
      tliErrNoCurrentTypelib = -2147220991,
      tliErrCantLoadLibrary = unchecked((int)(0x80029C4A)), // COM was -2147220990, Win32 API returns a different exception number
      tliErrTypeLibNotRegistered = -2147220989,
      tliErrSearchResultsChanged = -2147220988,
      tliErrNotApplicable = -2147220987,
      tliErrIncompatibleData = -2147220986,
      tliErrIncompatibleSearchType = -2147220985,
      tliErrIncompatibleTypeKind = -2147220984,
      tliErrInaccessibleImportLib = -2147220983,
      tliErrNoDefaultValue = -2147220982,
      tliErrDataNotAvailable = -2147220981,
      tliErrNotAnEntryPoint = -2147220980,
      tliErrStopFiltering = -2147220979,
      tliErrArrayBoundsNotAvailable = -2147220978,
      tliErrSearchResultsNotSorted = -2147220977,
      tliErrTypeNotArray = -2147220976
   }
}
