Exemple #1
0
        public List <string> GetAppModuleList(string licenseFilePath, string isUsed)
        {
            if (isUsed == "false")
            {
                return(null);
            }
            Console.WriteLine(string.Format("{0}[{1}]...", Utils.Utils.Translate("正在获取许可功能列表"), licenseFilePath));

            List <string> appModuleList = new List <string>();
            // 获取App和功能列表,用于根据ModuleName反查AppName

            int         nModuleCount     = 0;
            int         nLicenseAppCount = 0; // 授权许可在处理许可数时,只是每个App只有一条记录,所以此处需要AppCount;
            LicenseInfo oLicenseInfo     = null;

            StringBuilder licFilePath = new StringBuilder();

            licFilePath.Append(licenseFilePath);

            IntPtr intPtr = GetLicenseProperty(licFilePath);

            if (intPtr != null)
            {
                string strLicenseProperty = Marshal.PtrToStringAnsi(intPtr);
                if (strLicenseProperty != null && strLicenseProperty != "")
                {
                    oLicenseInfo = new LicenseInfo();
                    bool ret = oLicenseInfo.LicensePropertyFromString(strLicenseProperty, ref nModuleCount, ref nLicenseAppCount);

                    if (nModuleCount > 0)
                    {
                        int      itemMaxLength = 1024;
                        string[] strAppModules = new string[nModuleCount];
                        for (int i = 0; i < nModuleCount; i++)
                        {
                            strAppModules[i] = new string(' ', itemMaxLength);
                        }

                        GetLicenseAppModuleList(licFilePath, strAppModules, nModuleCount, itemMaxLength);

                        for (int i = 0; i < nModuleCount; i++)
                        {
                            appModuleList.Add(strAppModules[i]);
                        }
                    }
                }
            }
            Console.WriteLine(Utils.Utils.Translate("许可功能列表获取完成."));

            return(appModuleList);
        }
Exemple #2
0
        public bool Init(string configFile)
        {
            Console.WriteLine(Utils.Utils.Translate("正在初始化服务..."));

            bInit      = true;
            ConfigFile = configFile;

            List <string> LogoffLicenseIDs = new List <string>();
            // 读取许可注销信息
            string      logoffFile        = @"C:\Windows\logoff.dat";
            XmlNodeList logoffLicenseList = XML.GetNodeList(logoffFile, "logoffLicenseFile");

            if (logoffLicenseList != null)
            {
                foreach (XmlNode logoffLicense in logoffLicenseList)
                {
                    string logoffID = XML.Read(logoffLicense, "fileID", "");
                    if (string.IsNullOrEmpty(logoffID))
                    {
                        continue;
                    }
                    if (LogoffLicenseIDs.IndexOf(logoffID) >= 0)
                    {
                        continue;
                    }

                    LogoffLicenseIDs.Add(logoffID);
                }
            }

            // 读取配置文件,获取许可文件
            string xmlFile = configFile;

            LicenseInfos.Clear();
            htLicenseAppModule.Clear();

            int    nIndex          = 1;
            string licenseFilePath = string.Empty;

            while (true)
            {
                licenseFilePath = XML.Read(xmlFile, "LicenseFile" + nIndex, "file");
                if (licenseFilePath == "" || licenseFilePath == string.Empty)
                {
                    break;
                }
                else
                {
                    LicenseFile oLicenseFile = new LicenseFile();

                    string      isUsed       = XML.Read(xmlFile, "LicenseFile" + nIndex, "isUsed", "true");
                    LicenseInfo oLicenseInfo = oLicenseFile.GetLicenseInfo(licenseFilePath, isUsed);

                    bool bFind = false;
                    if (oLicenseInfo != null)
                    {
                        if (string.IsNullOrEmpty(oLicenseInfo.uuid))
                        {
                            Console.WriteLine(Utils.Utils.Translate("许可文件uuid错误."));
                            continue;
                        }

                        // 检查是否已经被注销
                        foreach (string logoffLicenseID in LogoffLicenseIDs)
                        {
                            if (String.Equals(oLicenseInfo.uuid, logoffLicenseID, StringComparison.CurrentCultureIgnoreCase))
                            {
                                oLicenseInfo.ErrorInfo = string.Format("{0}\n[{1}]", Utils.Utils.Translate("许可已被注销,请与软件销售商联系!"), licenseFilePath);
                                Console.WriteLine(oLicenseInfo.ErrorInfo);
                                break;
                            }
                        }

                        // 检查是否已经加载License
                        for (int i = 0; i < LicenseInfos.Count; i++)
                        {
                            LicenseInfo licenseInfo = LicenseInfos[i];
                            if (licenseInfo.IsUsed.ToLower() != "true")
                            {
                                continue;
                            }
                            if (string.IsNullOrEmpty(licenseInfo.ErrorInfo) == false)
                            {
                                continue;
                            }

                            if (licenseInfo.uuid == oLicenseInfo.uuid)
                            {
                                bFind = true;
                            }
                        }
                    }
                    else
                    {
                        nIndex++;
                        continue;
                    }

                    DateTime licCreateDate = DateTime.Parse(oLicenseInfo.CreateDate);
                    if (SystemDateVerify.CheckSystemDateWithLicenseFile(licCreateDate) == false)
                    {
                        Console.WriteLine(SystemDateVerify.ErrorInfo);
                        nIndex++;
                        continue;
                    }

                    if (bFind == false)
                    {
                        LicenseInfos.Add(oLicenseInfo);
                    }

                    // 添加当前许可文件中的注销ID到LogoffLicenseIDs
                    foreach (string logoffID in oLicenseInfo.LogoffLicenseIDs)
                    {
                        if (string.IsNullOrEmpty(logoffID))
                        {
                            continue;
                        }
                        if (LogoffLicenseIDs.IndexOf(logoffID) >= 0)
                        {
                            continue;
                        }

                        LogoffLicenseIDs.Add(logoffID);
                    }

                    // 获取许可配置中的所有模块信息,用于反查出厂时定义的module归属AppName
                    List <string> appModuleList = oLicenseFile.GetAppModuleList(licenseFilePath, isUsed);
                    if (appModuleList != null)
                    {
                        int    nSepIndex     = 0;
                        string strSeperator  = "\\&msp";
                        string strAppName    = string.Empty;
                        string strModuleName = string.Empty;
                        foreach (string appModule in appModuleList)
                        {
                            nSepIndex = 0;
                            string strAppModule = appModule;
                            int    nPos         = strAppModule.IndexOf(strSeperator);
                            while (nPos >= 0)
                            {
                                if (nSepIndex == 0)
                                {
                                    strAppName = strAppModule.Substring(0, nPos);
                                }
                                if (nSepIndex == 1)
                                {
                                    strModuleName = strAppModule.Substring(0, nPos);
                                    break;
                                }
                                strAppModule = strAppModule.Substring(nPos + strSeperator.Length, strAppModule.Length - nPos - strSeperator.Length);

                                nPos = strAppModule.IndexOf(strSeperator);
                                nSepIndex++;
                            }

                            try
                            {
                                htLicenseAppModule.Add(strModuleName, strAppName);
                            }
                            catch (System.Exception ex)
                            {
                            }
                            finally
                            {
                            }
                        }
                    }
                }
                nIndex++;
            }

            // 重新检查所有许可是否被注销
            foreach (LicenseInfo licenseInfo in LicenseInfos)
            {
                if (string.IsNullOrEmpty(licenseInfo.uuid))
                {
                    continue;
                }
                if (string.IsNullOrEmpty(licenseInfo.ErrorInfo) == false)
                {
                    continue;
                }

                // 检查是否已经被注销
                foreach (string logoffLicenseID in LogoffLicenseIDs)
                {
                    if (String.Equals(licenseInfo.uuid, logoffLicenseID, StringComparison.CurrentCultureIgnoreCase))
                    {
                        licenseInfo.ErrorInfo = string.Format("{0}, [{1}].", Utils.Utils.Translate("发现被注销许可"), licenseFilePath);
                        Console.WriteLine(licenseInfo.ErrorInfo);
                        break;
                    }
                }
            }


            bInit = false;
            Console.WriteLine(Utils.Utils.Translate("服务初始化完成."));

            return(true);
        }
Exemple #3
0
        public LicenseInfo GetLicenseInfo(string licenseFilePath, string isUsed)
        {
            Console.WriteLine(string.Format("{0}[{1}]...", Utils.Utils.Translate("正在获取许可信息"), licenseFilePath));

            int         nModuleCount     = 0;
            int         nLicenseAppCount = 0; // 授权许可在处理许可数时,只是每个App只有一条记录,所以此处需要AppCount;
            LicenseInfo oLicenseInfo     = null;

            StringBuilder licFilePath = new StringBuilder();

            licFilePath.Append(licenseFilePath);

            IntPtr intPtr = GetLicenseProperty(licFilePath);

            if (intPtr != null)
            {
                string strLicenseProperty = Marshal.PtrToStringAnsi(intPtr);
                if (strLicenseProperty != null && strLicenseProperty != "")
                {
                    oLicenseInfo                 = new LicenseInfo();
                    oLicenseInfo.IsUsed          = isUsed;
                    oLicenseInfo.ErrorInfo       = string.Empty;
                    oLicenseInfo.LicenseFilePath = licenseFilePath;

                    bool ret = oLicenseInfo.LicensePropertyFromString(strLicenseProperty, ref nModuleCount, ref nLicenseAppCount);

                    if (nLicenseAppCount > 0)
                    {
                        StringBuilder hostKey = new StringBuilder();
                        hostKey.Append(oLicenseInfo.HostKey);
                        IntPtr intPtrRet = CheckHostKey(hostKey);
                        string strRet    = Marshal.PtrToStringAnsi(intPtrRet);

                        if (strRet != "success")
                        {
                            oLicenseInfo.ErrorInfo = Utils.Utils.Translate("许可文件KEY信息在服务器校验失败!");
                            Console.WriteLine(oLicenseInfo.ErrorInfo);
                        }
                    }
                    else
                    {
                        oLicenseInfo.ErrorInfo = Utils.Utils.Translate("许可文件读取许可数信息失败!");
                        Console.WriteLine(oLicenseInfo.ErrorInfo);
                    }
                }
            }

            if (oLicenseInfo != null && oLicenseInfo.IsUsed.ToLower() == "true" && string.IsNullOrEmpty(oLicenseInfo.ErrorInfo))
            {
                int      itemMaxLength  = 1024;
                string[] strModuleInfos = new string[nLicenseAppCount];
                for (int i = 0; i < nLicenseAppCount; i++)
                {
                    strModuleInfos[i] = new string(' ', itemMaxLength);
                }

                GetLicenseModuleInfo(licFilePath, strModuleInfos, nLicenseAppCount, itemMaxLength);

                oLicenseInfo.ModuleInfos.Clear();

                for (int i = 0; i < nLicenseAppCount; i++)
                {
                    ModuleInfo oModuleInfo = new ModuleInfo();
                    oModuleInfo.ModuleFromString(strModuleInfos[i]);
                    oLicenseInfo.ModuleInfos.Add(oModuleInfo);
                }
            }

            Console.WriteLine(Utils.Utils.Translate("许可信息解析完成."));

            return(oLicenseInfo);
        }