//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "test_unit.h"
#include "include/enigma_keygen.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

  EGenerateRegistrationKey fnGenerateRegistrationKey = NULL;
  EGenerateRegistrationKeyFromProject fnGenerateRegistrationKeyFromProject = NULL;
  HINSTANCE LibHandle;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  LibHandle = LoadLibrary("keygen.dll");
  fnGenerateRegistrationKey = (EGenerateRegistrationKey)GetProcAddress(LibHandle, "GenerateRegistrationKey");
  fnGenerateRegistrationKeyFromProject = (EGenerateRegistrationKeyFromProject)GetProcAddress(LibHandle, "GenerateRegistrationKeyFromProject");
  if (fnGenerateRegistrationKey == NULL || fnGenerateRegistrationKeyFromProject == NULL)
  {
    btnGK->Enabled = false;
    btnGKFPF->Enabled = false;
  }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::btnCloseClick(TObject *Sender)
{
  ExitProcess(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnGKClick(TObject *Sender)
{
 PKeyParams kp;
 _KeyParams tempkp;

 char keybuf[255];

 kp = &tempkp;
 memset(kp, 0 , sizeof(_KeyParams));

 memset(&keybuf, 0 , 255);

 kp->KeyLen = 255;  // Key buffer length
 kp->Key = keybuf;  // Key buffer

 kp->UserInfo = eUserInfo->Text.c_str(); // Offset to UserInfo
 kp->UserInfoLen = strlen(kp->UserInfo); // UserInfo length

 kp->HardwareID = eHardware->Text.c_str(); // Offset to HardwareID

 kp->ExrirationYear = 2006; // Expiration year
 kp->ExpirationMonth = 1;   // Expiration month
 kp->ExpirationDay = 1;     // Expiration day

 // Get it from project file --------------------------------------------------
 kp->D = "HNZ7RNW3YU2M6NTS4NUU64GYT778E8E737GQ6EDQRFH67WP44VJDRLSSTJNHUHEHHNFZ98J65KRKYGZNQ8TKP624BD8RCWKTMLVBX";
 kp->N = "4TWMD8B6B726PQAK2WCJSMADEECTEJ2BJKTQCAD23EHNUW74XW8JX3Y6SHFTQ7Y2JZFM7NZARDMP8GNXJ4ESWHLPJQZZAFRU6CU4KB";
 kp->A = "CECB532D507556AA9FA9ABA0F5411508";
 // ---------------------------------------------------------------------------

 fnGenerateRegistrationKey(kp); // Key generation function

 eKey->Text = kp->Key;
}

//---------------------------------------------------------------------------
void __fastcall TForm1::btnGKFPFClick(TObject *Sender)
{
 PKeyParams kp;
 _KeyParams tempkp;

 char* ProjectFileName;

 char* GenResult;
 char keybuf[255];

 kp = &tempkp;
 memset(kp, 0 , sizeof(_KeyParams));

 memset(&keybuf, 0 , 255);

 kp->KeyLen = 255;  // Key buffer length
 kp->Key = keybuf;  // Key buffer

 kp->UserInfo = eUserInfo->Text.c_str(); // Offset to UserInfo
 kp->UserInfoLen = strlen(kp->UserInfo); // UserInfo length
 kp->HardwareID = eHardware->Text.c_str(); // Offset to HardwareID

 kp->ExrirationYear = 2006; // Expiration year
 kp->ExpirationMonth = 1;   // Expiration month
 kp->ExpirationDay = 1;     // Expiration day

 ProjectFileName = AnsiString(ExtractFilePath(ParamStr(0)) + "enigma_test.ini").c_str();

 // Key generation function
 GenResult = fnGenerateRegistrationKeyFromProject(ProjectFileName, kp);
 if (GenResult == NULL)
   {eKey->Text = kp->Key;} else {eKey->Text = GenResult;}
}
//---------------------------------------------------------------------------

