//---------------------------------------------------------------------------- // EgalTech 2013-2026 //---------------------------------------------------------------------------- // File : KeyGeneratorDlg.cpp Data : 02.01.26 Versione : 3.1a1 // Contenuto : Implementazione della classe dialogo generatore chiavi. // // // // Modifiche : 11.09.14 DS Creazione modulo. // 08.05.19 DS Portate Opzioni2 a 24. // 15.05.21 DS Aggiunto EgtBeamWall. // 02.01.23 DS Aggiunto Icarus. // 23.05.23 DS Aggiunto EgtEngine. // 09.10.23 DS Aggiunta gestione chiave di rete con LockId contenente AddrPort dopo @. // 05.01.24 DS Aggiunta versione 26. // 05.10.24 DS Aggiunto Effector. // 13.01.25 DS Aggiunta versione 27. // 02.01.26 DS Aggiunta versione 31. // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "afxdialogex.h" #include "resource.h" #include "KeyGenerator.h" #include "KeyGeneratorDlg.h" #include "KeyMaker.h" #include "/EgtDev/Include/EGnStringUtils.h" #include "/EgtDev/Include/EgtStringConverter.h" #include "/EgtDev/Include/EgtIniFile.h" #include "/EgtDev/Include/SELkKeyProc.h" using namespace std ; //---------------------------------------------------------------------------- #ifdef _DEBUG #define new DEBUG_NEW #endif //---------------------------------------------------------------------------- // Prodotti, versioni e sottoversioni static const vector vsProd = { "207", // librerie base "823", // generatore di licenze "1231", // SarmaxWall (vecchie librerie base) "3279", // EgtCAM5 "5327", // EgtBeamWall "5583", // Icarus "7375", // Effector "9423", // OmagCUT "9935" // EgtEngine } ; // altri valori : 463, 719, 975, 1487, 2255, 2511, 3535, 4559, 9679 (max 9999 e deve contenere i bit di 207) static const vector vsVer = { "31", "27", "26", "25", "24", "23", "22", "21", "19", "18", "16", "15" } ; static const vector vsSubVer = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "" } ; //---------------------------------------------------------------------------- // CAboutDlg dialog used for App About //---------------------------------------------------------------------------- class CAboutDlg : public CDialogEx { public : CAboutDlg( void) ; protected : BOOL OnInitDialog( void) ; DECLARE_MESSAGE_MAP() } ; //---------------------------------------------------------------------------- CAboutDlg::CAboutDlg( void) : CDialogEx( IDD_ABOUTBOX) { } //---------------------------------------------------------------------------- BOOL CAboutDlg::OnInitDialog( void) { CDialog::OnInitDialog() ; GetDlgItem( IDC_EXEVER)->SetWindowText( stringtoW( GetExeNameVer())) ; return TRUE ; } //---------------------------------------------------------------------------- BEGIN_MESSAGE_MAP( CAboutDlg, CDialogEx) END_MESSAGE_MAP() //---------------------------------------------------------------------------- // CKeyDlg dialog used for Key visualization //---------------------------------------------------------------------------- class CKeyDlg : public CDialogEx { public : CKeyDlg( const string& sKey) ; protected : BOOL OnInitDialog( void) ; private : string m_sKey ; DECLARE_MESSAGE_MAP() } ; //---------------------------------------------------------------------------- CKeyDlg::CKeyDlg( const string& sKey) : CDialogEx( IDD_KEYBOX) { m_sKey = sKey ; } //---------------------------------------------------------------------------- BOOL CKeyDlg::OnInitDialog( void) { CDialog::OnInitDialog() ; GetDlgItem( IDC_KEY)->SetWindowText( stringtoW( m_sKey)) ; return TRUE ; } //---------------------------------------------------------------------------- BEGIN_MESSAGE_MAP( CKeyDlg, CDialogEx) END_MESSAGE_MAP() //---------------------------------------------------------------------------- // CKeyGeneratorDlg dialog //---------------------------------------------------------------------------- CKeyGeneratorDlg::CKeyGeneratorDlg( const string& sFileToOpen, int nFlag, CWnd* pParent) : CDialogEx( IDD_KEYGENERATOR_DIALOG) { m_hIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME) ; // eventuale file da caricare all'avvio m_sFileToOpen = sFileToOpen ; m_bSpecial = ( ! m_sFileToOpen.empty() && nFlag == 1) ; } //---------------------------------------------------------------------------- BEGIN_MESSAGE_MAP( CKeyGeneratorDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED( IDC_OPENFILE, OnOpenFile) ON_BN_CLICKED( IDC_GENER, OnGenerate) ON_WM_CLOSE() END_MESSAGE_MAP() //---------------------------------------------------------------------------- BOOL CKeyGeneratorDlg::OnInitDialog( void) { CDialogEx::OnInitDialog() ; // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT( ( IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX) ; ASSERT( IDM_ABOUTBOX < 0xF000) ; CMenu* pSysMenu = GetSystemMenu( FALSE) ; if ( pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT( bNameValid) ; if ( ! strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu( MF_SEPARATOR) ; pSysMenu->AppendMenu( MF_STRING, IDM_ABOUTBOX, strAboutMenu) ; } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon( m_hIcon, TRUE) ; // Set big icon SetIcon( m_hIcon, FALSE) ; // Set small icon // Assegno prodotti ammessi CComboBox* pCbProd = (CComboBox*) GetDlgItem( IDC_PROD) ; if ( pCbProd != nullptr) { for ( auto& sProd : vsProd) pCbProd->AddString( stringtoW( sProd)) ; } // Assegno versioni ammesse CComboBox* pCbVer = (CComboBox*) GetDlgItem( IDC_VER) ; if ( pCbVer != nullptr) { for ( auto& sVer : vsVer) pCbVer->AddString( stringtoW( sVer)) ; } // Assegno sottoversioni ammesse CComboBox* pCbSub = (CComboBox*) GetDlgItem( IDC_SUBVER) ; if ( pCbSub != nullptr) { for ( auto& sSub : vsSubVer) pCbSub->AddString( stringtoW( sSub)) ; } // eventuale apertura o esecuzione di file all'avvio if ( ! m_sFileToOpen.empty()) { // leggo il file e genero if ( LoadFile( m_sFileToOpen.c_str())) { if ( m_bSpecial) PostMessage( WM_COMMAND, IDC_GENER, 0) ; return TRUE ; } } // Assegno ultimi valori inseriti LoadFile( WtoA( AfxGetApp()->m_pszProfileName)) ; return TRUE ; // return TRUE unless you set the focus to a control } //---------------------------------------------------------------------------- void CKeyGeneratorDlg::OnSysCommand( UINT nID, LPARAM lParam) { if ( ( nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout ; dlgAbout.DoModal() ; } else { CDialogEx::OnSysCommand( nID, lParam) ; } } //---------------------------------------------------------------------------- void CKeyGeneratorDlg::OnPaint( void) { if ( IsIconic()) { CPaintDC dc( this) ; // device context for painting SendMessage( WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0) ; // Center icon in client rectangle int cxIcon = GetSystemMetrics( SM_CXICON) ; int cyIcon = GetSystemMetrics( SM_CYICON) ; CRect rect ; GetClientRect( &rect) ; int x = ( rect.Width() - cxIcon + 1) / 2 ; int y = ( rect.Height() - cyIcon + 1) / 2 ; // Draw the icon dc.DrawIcon( x, y, m_hIcon) ; } else { CDialogEx::OnPaint() ; } } //---------------------------------------------------------------------------- HCURSOR CKeyGeneratorDlg::OnQueryDragIcon( void) { return static_cast( m_hIcon) ; } //---------------------------------------------------------------------------- void CKeyGeneratorDlg::OnCancel( void) { } //---------------------------------------------------------------------------- void CKeyGeneratorDlg::OnClose( void) { EndDialog( IDCANCEL) ; } //---------------------------------------------------------------------------- void CKeyGeneratorDlg::OnOpenFile( void) { // visualizzo il dialogo di scelta file CFileDialog dlg( TRUE, L"*.Kge", NULL, OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY|OFN_NOCHANGEDIR, L"Key info EgalTech (*.Kge)|*.Kge||", NULL, 0, FALSE) ; // imposto il direttorio iniziale wstring wsDir = stringtoW( GetPrivateProfileStringUtf8( "General", "DataDir", "", AfxGetApp()->m_pszProfileName)) ; if ( ! wsDir.empty()) dlg.m_ofn.lpstrInitialDir = wsDir.c_str() ; // lancio il dialogo di scelta file if ( dlg.DoModal() != IDOK) return ; // leggo il file LoadFile( WtoA( dlg.GetPathName())) ; } //---------------------------------------------------------------------------- bool CKeyGeneratorDlg::LoadFile( const char* szFile) { // verifico validità nome file if ( szFile == nullptr || szFile[0] == '\0') return false ; // leggo indice ultimo salvataggio int nLast = GetPrivateProfileInt( "Index", "Last", 0, szFile) ; // carico i dati del file string sSec = "Licence" ; if ( nLast > 0) sSec += ToString( nLast) ; string sCust = GetPrivateProfileStringUtf8( sSec.c_str(), "Customer", "", szFile) ; GetDlgItem( IDC_CUSTOMER)->SetWindowTextW( stringtoW( sCust)) ; string sLockId = GetPrivateProfileStringUtf8( sSec.c_str(), "LockId", "", szFile) ; GetDlgItem( IDC_MACHID)->SetWindowTextW( stringtoW( sLockId)) ; string sProd = GetPrivateProfileStringUtf8( sSec.c_str(), "Product", "", szFile) ; int nProdSel = - 1 ; for ( int i = 0 ; i < int( vsProd.size()) ; ++ i) { if ( sProd == vsProd[i]) nProdSel = i ; } ((CComboBox*)GetDlgItem( IDC_PROD))->SetCurSel( nProdSel) ; string sVerSub = GetPrivateProfileStringUtf8( sSec.c_str(), "Ver", "", szFile) ; string sVer = sVerSub.substr( 0, 2) ; int nVerSel = - 1 ; for ( int i = 0 ; i < int( vsVer.size()) ; ++ i) { if ( sVer == vsVer[i]) nVerSel = i ; } ((CComboBox*)GetDlgItem( IDC_VER))->SetCurSel( nVerSel) ; string sSub = sVerSub.substr( 2, 2) ; int nSubSel = - 1 ; for ( int i = 0 ; i < int( vsSubVer.size()) ; ++ i) { if ( sSub == vsSubVer[i]) nSubSel = i ; } ((CComboBox*)GetDlgItem( IDC_SUBVER))->SetCurSel( nSubSel) ; int nLev = GetPrivateProfileInt( sSec.c_str(), "Lev", 0, szFile) ; ((CComboBox*)GetDlgItem( IDC_LEV))->SetCurSel( nLev - 1) ; int nExpTime = GetPrivateProfileInt( sSec.c_str(), "ExpDays", 0, szFile) ; if ( nExpTime != 0) { CTime time( unsigned( nExpTime) * 24 * 3600) ; ((CDateTimeCtrl*)GetDlgItem( IDC_EXPIRATION))->SetTime( &time) ; } int nOpt1 = GetPrivateProfileInt( sSec.c_str(), "Opt1", 0, szFile) ; for ( int i = IDC_CHECK01, j = 0 ; i <= IDC_CHECK16 ; ++ i, ++ j) { if ( ( nOpt1 & ( 1 << j)) != 0) ((CButton*)GetDlgItem( i))->SetCheck( BST_CHECKED) ; else ((CButton*)GetDlgItem( i))->SetCheck( BST_UNCHECKED) ; } int nOpt2 = GetPrivateProfileInt( sSec.c_str(), "Opt2", 0, szFile) ; for ( int i = IDC_CHECK2_01, j = 0 ; i <= IDC_CHECK2_32 ; ++ i, ++ j) { if ( ( nOpt2 & ( 1 << j)) != 0) ((CButton*)GetDlgItem( i))->SetCheck( BST_CHECKED) ; else ((CButton*)GetDlgItem( i))->SetCheck( BST_UNCHECKED) ; } // check non ancora inseriti nel dialogo int nOptExpTime = GetPrivateProfileInt( sSec.c_str(), "OptExpDays", 0, szFile) ; if ( nOptExpTime != 0) { CTime time( unsigned( nOptExpTime) * 24 * 3600) ; ((CDateTimeCtrl*)GetDlgItem( IDC_OPT_EXPIR))->SetTime( &time) ; } return true ; } //---------------------------------------------------------------------------- void CKeyGeneratorDlg::OnGenerate( void) { KeyMaker mKey ; CStringW wszTemp ; // recupero il cliente GetDlgItem( IDC_CUSTOMER)->GetWindowTextW( wszTemp) ; string sCust = wstrztoA( wszTemp) ; if ( ! mKey.SetCustomer( sCust)) { AfxMessageBox( L"Dati cliente vuoto o con caratteri vietati (\\/:*?\"<>|)") ; return ; } WritePrivateProfileStringUtf8( "Licence", "Customer", sCust.c_str(), AfxGetApp()->m_pszProfileName) ; // recupero l'identificativo macchina (tipo 2) GetDlgItem( IDC_MACHID)->GetWindowTextW( wszTemp) ; string sLockId = wstrztoA( wszTemp) ; if ( ! mKey.SetLockId( sLockId)) { AfxMessageBox( L"Id Serratura vuoto o errato") ; return ; } WritePrivateProfileStringUtf8( "Licence", "LockId", sLockId.c_str(), AfxGetApp()->m_pszProfileName) ; // recupero il prodotto GetDlgItem( IDC_PROD)->GetWindowTextW( wszTemp) ; string sProd = wstrztoA( wszTemp) ; if ( ! mKey.SetProduct( sProd)) { AfxMessageBox( L"Prodotto vuoto o errato") ; return ; } WritePrivateProfileStringUtf8( "Licence", "Product", sProd.c_str(), AfxGetApp()->m_pszProfileName) ; // recupero la versione GetDlgItem( IDC_VER)->GetWindowTextW( wszTemp) ; string sVer = wstrztoA( wszTemp) ; GetDlgItem( IDC_SUBVER)->GetWindowTextW( wszTemp) ; string sSub = wstrztoA( wszTemp) ; string sVerSub = sVer + sSub ; if ( ! mKey.SetVersion( sVerSub)) { AfxMessageBox( L"Versione vuota o errata") ; return ; } WritePrivateProfileStringUtf8( "Licence", "Ver", sVerSub.c_str(), AfxGetApp()->m_pszProfileName) ; // recupero il livello GetDlgItem( IDC_LEV)->GetWindowTextW( wszTemp) ; string sLev = wstrztoA( wszTemp) ; if ( ! mKey.SetLevel( sLev)) { AfxMessageBox( L"Livello vuoto o errato") ; return ; } WritePrivateProfileStringUtf8( "Licence", "Lev", sLev.c_str(), AfxGetApp()->m_pszProfileName) ; // recupero la data di scadenza int nDays ; CTime tTime ; if ( ((CDateTimeCtrl*)GetDlgItem( IDC_EXPIRATION))->GetTime( tTime) == GDT_VALID) nDays = int( tTime.GetTime() / ( 24 * 3600)) ; else nDays = - 1 ; if ( ! mKey.SetExpirDays( nDays)) { AfxMessageBox( L"Inserisci una Data valida") ; return ; } WritePrivateProfileInt( "Licence", "ExpDays", nDays, AfxGetApp()->m_pszProfileName) ; // recupero le opzioni unsigned int nOpt1 = 0 ; for ( int i = IDC_CHECK01, j = 0 ; i <= IDC_CHECK16 ; ++ i, ++ j) { if ( ((CButton*)GetDlgItem( i))->GetCheck() == BST_CHECKED) nOpt1 += ( 1 << j) ; } unsigned int nOpt2 = 0 ; for ( int i = IDC_CHECK2_01, j = 0 ; i <= IDC_CHECK2_32 ; ++ i, ++ j) { if ( ((CButton*)GetDlgItem( i))->GetCheck() == BST_CHECKED) nOpt2 += ( 1 << j) ; } if ( ! mKey.SetOptions( nOpt1, nOpt2)) { AfxMessageBox( L"Inserisci Opzioni valide") ; return ; } WritePrivateProfileInt( "Licence", "Opt1", nOpt1, AfxGetApp()->m_pszProfileName) ; WritePrivateProfileInt( "Licence", "Opt2", nOpt2, AfxGetApp()->m_pszProfileName) ; // recupero la data di scadenza delle opzioni int nOptDays ; CTime tOptTime ; if ( ((CDateTimeCtrl*)GetDlgItem( IDC_OPT_EXPIR))->GetTime( tOptTime) == GDT_VALID) nOptDays = int( tOptTime.GetTime() / ( 24 * 3600)) ; else nOptDays = - 1 ; if ( ! mKey.SetOptExpirDays( nOptDays)) { AfxMessageBox( L"Inserisci una Data valida per le opzioni") ; return ; } WritePrivateProfileInt( "Licence", "OptExpDays", nOptDays, AfxGetApp()->m_pszProfileName) ; // verifico la protezione string sPKey = GetPrivateProfileStringUtf8( "General", "Key", "", AfxGetApp()->m_pszProfileName) ; if ( VerifyKey( sPKey, 823, 2305, 10) != KEY_OK) return ; // calcolo la chiave if ( ! mKey.CalcKey()) { AfxMessageBox( L"Errore nel calcolo della chiave") ; return ; } string sKey ; mKey.GetKey( sKey) ; // salvo i dati if ( ! mKey.SaveData( m_bSpecial)) AfxMessageBox( L"Errore nel salvataggio dei dati") ; // visualizzo i risultanti CKeyDlg dlgKey( sKey) ; dlgKey.DoModal() ; // se lanciato con file e flag 1, esco dal programma if ( m_bSpecial) PostMessage( WM_CLOSE, 0, 0) ; }