Files
Kanalsanierungsverwaltung/SanSystem/SanShared/Dongle.cs
2022-04-20 15:15:09 +02:00

171 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeMeter;
namespace SanShared
{
//4e 6a 41 77 4e 6a 51 7a 51 44 4d 78 4d 7a 6b 79 5a 54 4d 30 4d 6d 55 7a 4d 47 35 4c 61 32 55 7a 51 31 46 30 52 33 64 46 5a 45 68 35 56 57 74 47 61 7a 5a 30 61 48 70 55 57 69 39 58 64 6b 78 4a 54 32 78 43 62 6b 74 58 52 58 46 57 63 30 5a 73 64 7a 41 39
public class Dongle : IDisposable
{
uint FirmCode;
uint ProductCode;
public string SyncfusionKey = "";
Api cmApi;
CmCredential cmCred;
CmAccess2 cmAcc;
HCMSysEntry hcmse;
CmBoxInfo cmBoxInfo;
CmBoxEntry2 BoxContent;
public Dongle(uint ProductCode)
{
#if !DEBUG
this.FirmCode = 103086;
this.ProductCode = ProductCode;
#else
return;
this.FirmCode = 10;
this.ProductCode = 1;
#endif
cmApi = new Api();
cmCred = new CmCredential();
cmAcc = new CmAccess2();
cmAcc.Credential = cmCred;
cmAcc.Ctrl |= CmAccess.Option.UserLimit;
cmAcc.FirmCode = this.FirmCode;
cmAcc.ProductCode = this.ProductCode;
hcmse = cmApi.CmAccess2(CmAccessOption.Local, cmAcc);
if (hcmse == null)
{
ErrorCodes2 code = cmApi.CmGetLastErrorCode2();
string output = string.Format("{0}", code);
}
if (!CheckDongleVorhanden())
throw new Exception("Dongle not connected");
cmBoxInfo = new CmBoxInfo();
CmGetBoxContentsOption boxOptions = new CmGetBoxContentsOption();
boxOptions = CmGetBoxContentsOption.AllEntries;
CmBoxEntry2[] tmpBoxContent;
tmpBoxContent = cmApi.CmGetBoxContents2(hcmse, boxOptions, this.FirmCode, cmBoxInfo);
CmEntryData[] pCmBoxEntry = (CmEntryData[])cmApi.CmGetInfo(hcmse, CmGetInfoOption.EntryData);
for (int i = 0; i < pCmBoxEntry.Length; i++)
{
switch (pCmBoxEntry[i].Ctrl & 0x0ffff)
{
case (uint)CodeMeter.GlobalEntryOption.ProtectedData:
// Transfer to transformed byte
uint length = pCmBoxEntry[i].DataLen;
byte[] datas = new byte[length];
for(uint f = 0; f < length; f++)
{
datas[f] = pCmBoxEntry[i].Data[f];
}
SyncfusionKey = Encoding.ASCII.GetString(datas);
break;
}
}
foreach (CmBoxEntry2 boxes in tmpBoxContent)
{
if (boxes.ProductCode == this.ProductCode)
{
this.BoxContent = boxes;
}
}
}
~Dongle()
{
CleanDongle();
}
public void CleanDongle()
{
#if DEBUG
return;
#endif
cmApi.CmRelease(hcmse);
}
public bool CheckDongleVorhanden()
{
#if LAPTOP
return true;
#else
if (hcmse == null)
return false;
else
return true;
#endif
}
public string GetDongleSerial()
{
CmBoxInfo res = (CmBoxInfo)cmApi.CmGetInfo(hcmse, CmGetInfoOption.BoxInfo);
if (null != res)
{
return res.SerialNumber.ToString();
}
else
{
throw new Exception("Fehler beim aufrufen der Seriennummer");
}
}
public uint GetFeatureMap()
{
return BoxContent.FeatureMap;
}
public string GetName()
{
return "";
}
public bool IsLicensed(byte neededMask)
{
#if DEBUG
return true;
#else
uint DongleFeature = GetFeatureMap();
//Trace.WriteLine("DongleFeature: " + DongleFeature);
byte DongleFeatureB = (byte)DongleFeature;
if ((DongleFeatureB & neededMask) == neededMask)
return true;
return false;
#endif
}
public void Dispose()
{
CleanDongle();
}
}
}