You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
5.1 KiB
C#
162 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.ExceptionServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace FlexenabledLic
|
|
{
|
|
public delegate void LoginResultEventHandler(LoginResult loginResult);
|
|
public struct LoginResult
|
|
{
|
|
public int Status;
|
|
public string Message;
|
|
public string AppName;
|
|
}
|
|
/// <summary>
|
|
/// 登录管理的类.
|
|
/// </summary>
|
|
public class LicHelp
|
|
{
|
|
private LoginResult result;
|
|
public LoginResult Result
|
|
{
|
|
get { return result; }
|
|
private set { result = value; }
|
|
}
|
|
public LoginResultEventHandler LoginResultEvent;
|
|
private BackgroundWorker backgroundWorker1;
|
|
private string AppName;
|
|
/// <summary>
|
|
/// Prevents a default instance of the <see cref="LicHelp"/> class from being created.
|
|
/// </summary>
|
|
private LicHelp()
|
|
{
|
|
backgroundWorker1 = new BackgroundWorker();
|
|
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
|
|
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(loginWorkerCompleted);
|
|
}
|
|
/// <summary>
|
|
/// 登录结果事件.
|
|
/// </summary>
|
|
/// <param name="sender">The sender.</param>
|
|
/// <param name="e">The e.</param>
|
|
private void loginWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
{
|
|
result = (LoginResult)e.Result;
|
|
//if (result.Status != 2)
|
|
//{
|
|
LoginResultEvent?.Invoke(result);
|
|
//}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行登录.
|
|
/// </summary>
|
|
/// <param name="sender">The sender.</param>
|
|
/// <param name="e">The e.</param>
|
|
[HandleProcessCorruptedStateExceptions]
|
|
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
LoginResult loginResult = new LoginResult();
|
|
loginResult.Status = 1;
|
|
loginResult.AppName = AppName;
|
|
BackgroundWorker worker = sender as BackgroundWorker;
|
|
try
|
|
{
|
|
if (!File.Exists("FlexDll.dll"))
|
|
{
|
|
loginResult.Status = 2; // 该状态表示不需要使用FlexDll.dll进行登录
|
|
loginResult.Message = "FlexDll.dll未找到";
|
|
e.Result = loginResult;
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
LicHelp.Initialize();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string strMsg = ex.Message;
|
|
loginResult.Status = 1;
|
|
if (strMsg.Contains("FlexDll.dll"))
|
|
{
|
|
loginResult.Status = 2;// 该状态表示不需要使用FlexDll.dll进行登录
|
|
}
|
|
loginResult.Message = ex.Message;
|
|
e.Result = loginResult;
|
|
return;
|
|
}
|
|
StringBuilder strLogResult = new StringBuilder(200);
|
|
int nStat = 2;
|
|
try
|
|
{
|
|
nStat = LicHelp.CheckOut(AppName, strLogResult);
|
|
if (nStat != 0)
|
|
{
|
|
loginResult.Message = "登录失败";
|
|
}
|
|
else
|
|
{
|
|
loginResult.Message = "登录成功";
|
|
}
|
|
}
|
|
catch { }
|
|
loginResult.Status = nStat;
|
|
// 将结果传递给 RunWorkerCompleted 事件
|
|
e.Result = loginResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="LicHelp"/> class.
|
|
/// </summary>
|
|
/// <param name="appName">The app name.</param>
|
|
public LicHelp(string appName):this()
|
|
{
|
|
AppName = appName;
|
|
}
|
|
/// <summary>
|
|
/// 执行登录
|
|
/// </summary>
|
|
public void DoLogin()
|
|
{
|
|
if (!backgroundWorker1.IsBusy)
|
|
{
|
|
backgroundWorker1.RunWorkerAsync(AppName);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 执行登出.
|
|
/// </summary>
|
|
/// <returns>An int.</returns>
|
|
public int LogOut()
|
|
{
|
|
int nStat = -1;
|
|
try
|
|
{
|
|
nStat = CheckIn(AppName);
|
|
CleanUp();
|
|
}
|
|
catch { }
|
|
return nStat;
|
|
}
|
|
|
|
/// prototypes from DLL
|
|
[DllImport("FlexDll.dll", CharSet = CharSet.Ansi)]
|
|
public static extern int Initialize();
|
|
[DllImport("FlexDll.dll", CharSet = CharSet.Ansi)]
|
|
public static extern int CheckOut(String FeatureName, StringBuilder OutMsg);
|
|
[DllImport("FlexDll.dll", CharSet = CharSet.Ansi)]
|
|
public static extern int CheckIn(String FeatureName);
|
|
[DllImport("FlexDll.dll", CharSet = CharSet.Ansi)]
|
|
public static extern int CleanUp();
|
|
}
|
|
}
|