public bool AddLicense(License inLicense)
        {
            byte[] signaturebytes = null;
            byte[] clientbytes    = null;
            byte[] licensesbytes  = null;
            signaturebytes = new byte[4];
            clientbytes    = new byte[2];
            licensesbytes  = new byte[2];

            /*
             * // test quickly add license in dongle..
             * // fill the struct first
             * MESSAGE_LOG_HEADER_STRUCT2 temp2;
             * temp2.mClient = Convert.ToUInt16(inLicense.mClient);
             * temp2.mCRC = inLicense.mCRC;
             * temp2.mEnd = Convert.ToUInt16(inLicense.mEnd);
             * temp2.mKey = Convert.ToByte(inLicense.mKey);
             * temp2.mProduct = Convert.ToUInt16(inLicense.mProduct);
             * temp2.mStart = Convert.ToUInt16(inLicense.mStart);
             *
             * byte[] all = null;
             * byte[] licensesbytes = null;
             * licensesbytes = new byte[2];
             * all = new byte[12];
             *
             * all=RawSerialize(temp2);
             *
             * file.FilePos = 24 + (mLicenseCount*24);
             * // client id
             * status = file.Write(all, 0, 12);
             *
             * // and put license count on 1
             * file.FilePos = 6;
             * // and the number of licenses
             * licensesbytes = BitConverter.GetBytes((uint)mLicenseCount+1);
             * status = file.Write(licensesbytes, 0, 2);
             */


            if (!inLicense.mValid)
            {
                MessageBox.Show("This license is not valid.", "Problem", MessageBoxButtons.OK);
                return(false);
            }
            if (!(mKey != 0 && mHaspHandle != -1))
            {
                MessageBox.Show("Insert a valid dongle.", "Problem", MessageBoxButtons.OK);
                return(false);
            }
            if (mClient == 0 || mLicenseCount == 0)
            {
                //uint theSignature = 1400456047;
                signaturebytes = BitConverter.GetBytes(1400456047);
                //signaturebytes[0] = 0x6f;
                //signaturebytes[1] = 0x43;
                //signaturebytes[2] = 0x79;
                //signaturebytes[3] = 0x53;
                // mClient = inLicense.mClient;
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                //signaturebytes = encoding.GetBytes(theSignature.ToString());
                file.FilePos  = 0;
                status        = file.Write(signaturebytes, 0, 4);
                clientbytes   = BitConverter.GetBytes(inLicense.mClient);
                file.FilePos  = 4;
                status        = file.Write(clientbytes, 0, 2);
                licensesbytes = BitConverter.GetBytes(1);
                file.FilePos  = 6;
                status        = file.Write(licensesbytes, 0, 2);

                WriteLicense(inLicense, 0, false);
                return(true);
            }

            if (mClient != inLicense.mClient)
            {
                MessageBox.Show("This license has been prepared for a different dongle.", "Problem", MessageBoxButtons.OK);
                return(false);
            }

            for (int i = 0; i < mLicenses.Count; i++)
            {
                if ((mLicenses[i].mProduct == inLicense.mProduct) &&
                    (mLicenses[i].mClient == inLicense.mClient) &&
                    (mLicenses[i].mStart == inLicense.mStart) &&
                    (mLicenses[i].mEnd == inLicense.mEnd))
                {
                    MessageBox.Show("This license already exists on this dongle.", "Problem", MessageBoxButtons.OK);
                    return(false);
                }
            }

            for (int i = 0; i < mLicenses.Count; i++)
            {
                if ((mLicenses[i].mProduct == inLicense.mProduct) &&
                    (mLicenses[i].mClient == inLicense.mClient))
                {
                    // same product and client...
                    if (mLicenses[i].mEnd < inLicense.mEnd)
                    {
                        // write over expired license or update license!
                        WriteLicense(inLicense, i, true);
                        return(true);
                    }
                    else
                    {
                        MessageBox.Show("Newer license already exists on this dongle.", "Problem", MessageBoxButtons.OK);
                    }
                    return(false);

                    /*
                     *          mLicenses[i] = inLicense;
                     *          hasp_write (mHaspHandle, HASP_FILEID_MAIN, 24+i*12, 12, &inLicense);
                     * //			mLicenseCount++;
                     *          hasp_write (mHaspHandle, HASP_FILEID_MAIN, 6, 2, &mLicenseCount);
                     *          Beep (440, 200);
                     */
                }
            }

            //if (mLicenseCount < (mSize - 24)/12) {
            WriteLicense(inLicense, mLicenseCount, false);
            return(true);
            //}

            //MessageBox.Show("There is no room in this dongle for the license.", "Problem", MessageBoxButtons.OK);
            //return;
        }
        public bool CheckNewLicense(string inLicCode, int inProduct)
        {
            bool ok = true;

            License thenewlicense = new License();

            //uint theCRC = 0;
            if (inLicCode.Length == 24)
            {
                try
                {
                    thenewlicense.mClient  = Convert.ToUInt16((inLicCode[2] - 65) * 26 * 26 + (inLicCode[3] - 65) * 26 + (inLicCode[5] - 65));
                    thenewlicense.mProduct = Convert.ToUInt16((inLicCode[6] - 65) * 26 * 26 + (inLicCode[7] - 65) * 26 + (inLicCode[8] - 65));
                    uint temp = Convert.ToUInt16((inLicCode[13] - 65) * 26 * 26 + (inLicCode[15] - 65) * 26 + (inLicCode[16] - 65));
                    thenewlicense.mCRC   = Convert.ToUInt32((((inLicCode[10] - 65) * 26 * 26 + (inLicCode[11] - 65) * 26 + (inLicCode[12] - 65)) << 10) + (temp >> 4));
                    thenewlicense.mKey   = temp & 15;
                    thenewlicense.mStart = Convert.ToUInt32((inLicCode[17] - 65) * 26 * 26 + (inLicCode[18] - 65) * 26 + (inLicCode[20] - 65));
                    thenewlicense.mEnd   = Convert.ToUInt32((inLicCode[21] - 65) * 26 * 26 + (inLicCode[22] - 65) * 26 + (inLicCode[23] - 65));

                    string totalstring = thenewlicense.mClient.ToString() + thenewlicense.mProduct.ToString() + thenewlicense.mKey.ToString() + thenewlicense.mStart.ToString() + thenewlicense.mEnd.ToString();
                    Crc32  newCrc      = new Crc32();
                    uint   newcrc      = newCrc.ComputeChecksum(StrToByteArray(totalstring));
                    newcrc = newcrc & 0x00ffffff;
                    thenewlicense.mValid = newcrc == thenewlicense.mCRC;
                    //if (thenewlicense.mValid && inProduct == thenewlicense.mProduct)
                    if (AddLicense(thenewlicense))
                    {
                        ok = true;
                    }
                    else
                    {
                        ok = false;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("This license is not valid.", "Problem", MessageBoxButtons.OK);
                    ok = false;
                }

                //else
                //    ok = false;

                /*
                 *      mStart = (inLicCode[17]-'A')*26*26 + (inLicCode[18]-'A')*26 + (inLicCode[20]-'A');
                 *      mEnd = (inLicCode[21]-'A')*26*26 + (inLicCode[22]-'A')*26 + (inLicCode[23]-'A');
                 */
                //SetCRC ();
                //mValid = (theCRC == mCRC);
            }
            else
            {
                MessageBox.Show("This license is not valid.", "Problem", MessageBoxButtons.OK);
                ok = false;
            }



            //newCrc.ComputeChecksum(
            return(ok);
        }
        internal bool CheckNewLicense(string inLicCode)
        {
            bool ok = true;
            int  test;

            License thenewlicense = new License();

            uint theCRC = 0;

            if (inLicCode.Length == 24)
            {
                try
                {
                    thenewlicense.mClient  = Convert.ToUInt16((inLicCode[2] - 65) * 26 * 26 + (inLicCode[3] - 65) * 26 + (inLicCode[5] - 65));
                    thenewlicense.mProduct = Convert.ToUInt16((inLicCode[6] - 65) * 26 * 26 + (inLicCode[7] - 65) * 26 + (inLicCode[8] - 65));
                    uint temp = Convert.ToUInt16((inLicCode[13] - 65) * 26 * 26 + (inLicCode[15] - 65) * 26 + (inLicCode[16] - 65));
                    thenewlicense.mCRC   = Convert.ToUInt32((((inLicCode[10] - 65) * 26 * 26 + (inLicCode[11] - 65) * 26 + (inLicCode[12] - 65)) << 10) + (temp >> 4));
                    thenewlicense.mKey   = temp & 15;
                    thenewlicense.mStart = Convert.ToUInt32((inLicCode[17] - 65) * 26 * 26 + (inLicCode[18] - 65) * 26 + (inLicCode[20] - 65));
                    thenewlicense.mEnd   = Convert.ToUInt32((inLicCode[21] - 65) * 26 * 26 + (inLicCode[22] - 65) * 26 + (inLicCode[23] - 65));

                    string totalstring = thenewlicense.mClient.ToString() + thenewlicense.mProduct.ToString() + thenewlicense.mKey.ToString() + thenewlicense.mStart.ToString() + thenewlicense.mEnd.ToString();
                    Crc32  newCrc      = new Crc32();
                    uint   newcrc      = newCrc.ComputeChecksum(StrToByteArray(totalstring));
                    newcrc = newcrc & 0x00ffffff;
                    thenewlicense.mValid = newcrc == thenewlicense.mCRC;
                    if (!thenewlicense.mValid)
                    {
                        MessageBox.Show("This License is not valid!");
                        return(false);
                    }
                    int returnint = AddLicense(thenewlicense);
                    if (returnint == 0)
                    {
                        ok = true;
                    }
                    else
                    {
                        string messageboxstring = "";
                        switch (returnint)
                        {
                        case 1:
                            messageboxstring = "This license is not for this client!";
                            break;

                        case 2:
                            messageboxstring = "This license is not for this product!";
                            break;

                        // JPB This is a test change, removing redundant code...
                        //case 3:
                        //    messageboxstring = "This license end date is shorter then the current end date!";
                        //    break;
                        case 4:
                            messageboxstring = "This license end date is already expired!";
                            break;

                        case 5:
                            messageboxstring = "Unable to save new license!";
                            break;
                        }
                        MessageBox.Show(messageboxstring);
                        ok = false;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("This license is not valid.", "Problem", MessageBoxButtons.OK);
                    ok = false;
                }

                //else
                //    ok = false;

                /*
                 *      mStart = (inLicCode[17]-'A')*26*26 + (inLicCode[18]-'A')*26 + (inLicCode[20]-'A');
                 *      mEnd = (inLicCode[21]-'A')*26*26 + (inLicCode[22]-'A')*26 + (inLicCode[23]-'A');
                 */
                //SetCRC ();
                //mValid = (theCRC == mCRC);
            }
            else
            {
                MessageBox.Show("This license is not valid.", "Problem", MessageBoxButtons.OK);
                ok = false;
            }



            //newCrc.ComputeChecksum(
            return(ok);
        }