using BaseFunction; using Microsoft.Win32; using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace NSAnalysis { internal static class Program { #region CLMS 授权 /// /// 功能:使用MORCODE.DLL前验证使用软件是否正确,否则其他函数都不可正常调用 /// /// 验证内容为软件指纹文件中字符串 /// TRUE :该软件可正常调用 FALSE :该软件不可正常调用 [DllImport("MORCODE.dll", EntryPoint = "CLM_Login", CallingConvention = CallingConvention.Cdecl)] public static extern bool CLM_Login(StringBuilder str); /// /// 退出前执行一次,释放内存,记录使用信息 /// /// [DllImport("MORCODE.dll", EntryPoint = "CLM_Logout", CallingConvention = CallingConvention.Cdecl)] public static extern bool CLM_Logout(); /// /// 自动检查电脑中是否有当前软件的许可信息,过期/非本机电脑/其他软件 的许可证书都不会识别 /// /// TRUE :有该软件许可证书 FALSE :没有该软件许可证书 [DllImport("MORCODE.dll", EntryPoint = "CLM_Login_Scope", CallingConvention = CallingConvention.Cdecl)] public static extern bool CLM_Login_Scope(); /// /// 输入模块ID验证模块可用性 /// /// 模块ID /// 暂无定义 /// TRUE :该模块可用 FALSE :该模块不可用 [DllImport("MORCODE.dll", EntryPoint = "CLM_ModuleIsLicensed", CallingConvention = CallingConvention.Cdecl)] public static extern bool CLM_ModuleIsLicensed(ref UInt16 mod, ref UInt16 type); /// /// 获取到期日期 /// /// /// /// /// [DllImport("MORCODE.dll", EntryPoint = "CLM_GetWarrantyExpiration", CallingConvention = CallingConvention.Cdecl)] public static extern bool CLM_GetWarrantyExpiration(ref int mon, ref int day, ref int year); private static int gCLMSModeID = 0; private static int gAuthorizationMode = 0; #endregion CLMS 授权 /// /// The main entry point for the application. /// [STAThread] private static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); #region 创建Log文件 LogDebugDfn.strStartTime = DateTime.Now.ToString("yyyy.MM.dd HH-mm-ss"); System.IO.TextWriter log = new System.IO.StreamWriter(LogDebugDfn.strDebugFile); //Debug.txt TextWriterTraceListener logger = new TextWriterTraceListener(log); Trace.Listeners.Add(logger); MyBase.TraceWriteLine("--软件Program Main函数开始执行--"); //写入log文件 ConfigDfn.LoadConfigFile(); //加载配置参数 gCLMSModeID = FileIni.ReadInt(ConfigDfn.strConfigFile, "Chery", "CLMSModeID", 1); gAuthorizationMode = FileIni.ReadInt(ConfigDfn.strConfigFile, "Chery", "AuthorizationMode", 1); #endregion 创建Log文件 //FileSorter fileSorter = new FileSorter(); //fileSorter.test(); if (gAuthorizationMode == 1) { MyBase.TraceWriteLine("Log in using the CLMS authorization."); #region 检测CLMS /////1登陆验证 //验证内容为软件指纹文件中字符串,功能:使用MORCODE.DLL前验证使用软件是否正确,否则其他函数都不可正常调用 StringBuilder password = new StringBuilder("vravnhNYybkmDkHDPKhVIwbHMoqHktgqWHFkBTOUsZLLVLwaAOkPIFFoVffUnSqUkCApSxrWQmsKvYCdVUpZLOhuHshKFbUPezYYbyQCgUoHrFOaaLVSosAapdVagrfFSqDUzxCvVuSJodxkjBiCJdRUfhCIfyRzjLdTfkxtUHPYUHyQbzOaJFvdSbzLihYmuhLHFNUSbERcAFbnrvNQtZOCUSUnHFcySXzTbfqTgzeaWQrjoSTzEUYDkEUmqusV"); bool res = CLM_Login(password); if (!res) { MyBase.TraceWriteLine("CLMS Login Failed."); MessageBox.Show("CLMS Login fails. Please authorize the user first! ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } /////2检查许可 //该函数自动检查电脑中是否有当前软件的许可信息,过期/非本机电脑/其他软件 的许可证书都不会识别 res = CLM_Login_Scope(); if (!res) { //检查许可失败 MyBase.TraceWriteLine("CLMS check license failed! Please authorize the operation."); MessageBox.Show("The CLMS failed to check for permissions. Please authorize first! ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //3获取许可模块是否可用 //输入模块ID验证模块可用性 UInt16 mod = (UInt16)gCLMSModeID;//模块ID UInt16 type = 0;//暂无定义 res = CLM_ModuleIsLicensed(ref mod, ref type);//res true:可用 false:不可用 if (!res) { MyBase.TraceWriteLine("The CLMS module is unavailable!"); MessageBox.Show(mod.ToString() + " The CLMS module is unavailable!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //4获取到期日期 int Year = 0, Month = 0, Day = 0; res = CLM_GetWarrantyExpiration(ref Month, ref Day, ref Year); if (!res) { //登陆验证失败 MyBase.TraceWriteLine("Login verification failed, authorization time is up, please contact Hexagon company for authorization! The maturity time is:" + Year.ToString() + "." + Month.ToString() + "." + Day.ToString()); MessageBox.Show("Login verification failed, authorization time is up, please contact Hexagon company for authorization! The maturity time is:" + Year.ToString() + "." + Month.ToString() + "." + Day.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (res) { ConfigDfn.strExpiredTime = Year.ToString() + "-" + Month.ToString() + "-" + Day.ToString(); } #endregion 检测CLMS Application.Run(new FormMain()); } else { MyBase.TraceWriteLine("Log in using the original authorization mode."); Licence.GetRegisterInfo(); Licence.Encryption_DLL(); } } } public class Licence { //=============================================================== #region 1、加密动态库加密 public static void Encryption_DLL() { Covert.Program Mycheck = new Covert.Program(); if (Mycheck.CheckLicence(Application.StartupPath + "\\Key\\reg.key", "PCControl")) { MyBase.TraceWriteLine("软件授权成功"); ConfigDfn.strExpiredTime = Covert.Parameter.ExpiredTime.ToString("yyyy-MM-dd HH:mm:ss"); if (IsSoftwareRunning()) { MessageBox.Show("库位管理软件已经存在,请关闭后重新尝试!", "警告"); System.Environment.Exit(0); return; } Application.Run(new FormMain()); } else { MessageBox.Show("Did not pass the certification!\r\n" + Covert.Parameter.ExpiredTime.ToString()); BaseFunction.MyBase.TraceWriteLine("软件授权失败"); } } #endregion 1、加密动态库加密 private static bool IsSoftwareRunning() { int Num = 0; Process[] processes = Process.GetProcesses(); foreach (Process instance in processes) { string pro = instance.ProcessName; if (pro.ToLower() == "NSAnalysis") { Num++; } } if (Num > 1) return true; else return false; } //=============================================================== #region 读取注册表信息,准备各种时间 private static string PassWord = ""; private static string PassData = ""; private static Int64 PassNow = 0; private static Int64 LtOk = 20160101080000; //开始日期 private static Int64 LtOk2now; public static void GetRegisterInfo() { LtOk2now = Convert.ToInt64(System.DateTime.Now.ToString("yyyyMMddHHmmss")); RegistryKey pregkey; RegistryKey pregkey1; RegistryKey pregkey2; bool RegKey = false; try { do { pregkey = Registry.CurrentUser.OpenSubKey("software", true); pregkey1 = Registry.CurrentUser.OpenSubKey("software\\sunpike", true); pregkey2 = Registry.CurrentUser.OpenSubKey("software\\sunpike\\PCControl", true); if (pregkey1 != null) { if (pregkey2 != null) { if (pregkey2.GetValue("Pass") == null) { pregkey2.SetValue("Pass", ""); } if (pregkey2.GetValue("Data") == null) { pregkey2.SetValue("Data", ""); } if (pregkey2.GetValue("Now") == null) { pregkey2.SetValue("Now", LtOk.ToString()); } PassWord = pregkey2.GetValue("Pass").ToString(); PassData = pregkey2.GetValue("Data").ToString(); PassNow = Convert.ToInt64(pregkey2.GetValue("Now").ToString());//上次结束APP的时间 RegKey = true; } else { pregkey1.CreateSubKey("PCControl"); } } else { pregkey.CreateSubKey("sunpike"); } } while (!RegKey); if (LtOk2now > PassNow) pregkey2.SetValue("Data", LtOk.ToString()); else pregkey2.SetValue("Now", LtOk.ToString()); pregkey.Close(); pregkey1.Close(); pregkey2.Close(); } catch { MyBase.TraceWriteLine("--读取注册信息时遭到破坏。"); MessageBox.Show("读取注册信息时遭到破坏。"); // //有一个复位的方法,就是客户打电话给我,我给他一串数据(时间加校验码),填入后,通过校验是合适的时间,就恢复这串码中的时间, //举例: //目前是2014年03月19日11点30分15秒: // 20140319113015 --加密--> 3分十位2年千位4年个位1秒十位3校验位(前4位相加之和保证个位是3)1年十位1时个位1时十位5秒个位3校验位(前4位相加之和保证个位是3)0月十位0年百位1日十位0分个位2校验位(前4位相加之和保证个位是3)3月个位9日个位0校验位(前2位相加之和保证个位是3) // 3 2 4 1 3 1 1 1 5 5 0 0 1 0 2 3 0 0 // 324131115500102300 return; } } #endregion 读取注册表信息,准备各种时间 } }