Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I register date of First install of the software and write some function to demonstrate how many days left on trial software. If I am understanding right I can DWORD FIRSTINSTALLDATE in registery(REGEDIT). When Trial Period over I need to show ProductCode Page where activation code inserted and MAINFORM Comes Alive. This is MFC project I also like to show how many days left on software to expire.

C++
// Addapt.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Addaptapp.h"

#include "AddInChildFrm.h"
#include "FaultFIFOChildFrame.h"
#include "AddaptDoc.h"
#include "AddaptView.h"

#include "AddaptDocTemplate.h"
#include "AddInManager.h"

#include "TextDoc.h"
#include "TextView.h"
#include "DSDoc.h"
#include "DSGridFrm.h"

#include "PWDoc.h"
#include "PWReg.h"
#include "PWAppIO.h"
#include "PWUpdate.h"
#include "PWLogin.h"
#include "PWChgDlg.h"
#include "PWGridFrm.h"
#include "time.h"

#include "FaultFifoView.h"
#include "FaultFifoDoc.h"

#include "FileNewDlg.h"
#include "CfgDirDlg.h"
#include "ProductCode.h"
#include "Splash.h"

#include "AddaptMDIChildFrame.h"
#include "DriveFrame.h"

#include "Parameters.h"
#include "OptionsPropertySheet.h"

#include <winnetwk.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// the help file name 
//TCHAR* g_szHelpFile = _T("hh \\avtron\\addapthref="http://www.justanswer.com/computer-programming/5jq27-coledatetimespan.html0\\add_zap\\addapt.chm");

//#define _CRTDBG_MAP_ALLOC
//#include <stdlib.h>
//#include <crtdbg.h>

/////////////////////////////////////////////////////////////////////////////
// CAddaptApp
BEGIN_MESSAGE_MAP(CAddaptApp, CWinApp)
//{{AFX_MSG_MAP(CAddaptApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_OPEN_DEFAULTCFG, OnOpenDefaultCfg)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_HELP, OnHelp)
//}}AFX_MSG_MAP

ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnEnableFileNew)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnEnableEditCut)

ON_UPDATE_COMMAND_UI(ID_OPEN_DEFAULTCFG, OnEnableDefaultConfig)

ON_COMMAND(ID_CONFIG_FILES, OnConfigFilesDlg)
ON_UPDATE_COMMAND_UI(ID_CONFIG_FILES, OnEnableConfigFilesDlg)

ON_UPDATE_COMMAND_UI(IDC_ADD_STTN, OnEnableAddSection)
ON_COMMAND(IDC_ADD_STTN, OnAddSection)

ON_UPDATE_COMMAND_UI(IDC_ADD_ROW, OnEnableAddRow)
ON_COMMAND(IDC_ADD_ROW, OnAddRow)

ON_COMMAND(ID_CAL_FILE_NEW, OnCalFileOpen)
ON_UPDATE_COMMAND_UI(ID_CAL_FILE_NEW, OnEnableCalFileOpen)

ON_UPDATE_COMMAND_UI(ID_ACTIVITY, OnCheckActivityIndicator)
ON_COMMAND(ID_ACTIVITY, OnActivityIndicator)
ON_UPDATE_COMMAND_UI(IDC_DRIVEGRID, OnEnableDSGrid)
ON_COMMAND(IDC_DRIVEGRID, OnDSGrid)

ON_UPDATE_COMMAND_UI(IDC_LOGIN, OnEnableLogin)
ON_COMMAND(IDC_LOGIN, OnLogin)
ON_UPDATE_COMMAND_UI(IDC_LOGOUT, OnEnableLogout)
ON_COMMAND(IDC_LOGOUT, OnLogout)

ON_UPDATE_COMMAND_UI(IDC_PRODUCT_CODE, OnEnableProductCode)
ON_COMMAND(IDC_PRODUCT_CODE, OnProductCode)

ON_UPDATE_COMMAND_UI(IDC_CHANGE_PASSWORD, OnEnableChangePassword)
ON_COMMAND(IDC_CHANGE_PASSWORD, OnChangePassword)
ON_UPDATE_COMMAND_UI(IDC_CONFIGURE, OnEnablePasswordConfig)
ON_COMMAND(IDC_CONFIGURE, OnPasswordConfig)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddaptApp construction
CAddaptApp::CAddaptApp() : m_pFaultFIFOTemplate(NULL), m_bGXInitialized(FALSE),
m_bConfigOpen(FALSE), m_bEthernet(FALSE), m_b8024(FALSE),
m_bMonitorActivity(FALSE), m_bDSGridStatus(0),
m_bRestoreWindows(0), m_bCanAdd(0), m_bBlockCommTest(0),
m_bIsWindowsNT4orLater(0), m_bIsWindows98orLater(0),
m_bBeta(0)
{
m_hwndDialog=NULL;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CAddaptApp object
CAddaptApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CAddaptApp initialization
BOOL CAddaptApp::InitInstance()
{
// _CrtSetBreakAlloc(); // find memory leak

bFirst = true;

OSVERSIONINFOA osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (GetVersionExA(&osvi))
{
m_bIsWindowsNT4orLater = ( (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(osvi.dwMajorVersion >= 4) );

m_bIsWindows98orLater = ( (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
( (osvi.dwMajorVersion > 4) ||
(osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0) ) );
}
//if (m_bIsWindowsNT4orLater)
// CParameters::DisableLAN();

int nOS = CParameters::GetOS();
switch(nOS)
{
case NoEth:
break;

case Win98:
case FieldServ:
m_bEthernet = TRUE;
m_b8024 = TRUE;
break;

case XP:
case Me:
case WinNT:
m_bEthernet = TRUE;
break;

default:
AfxMessageBox(IDS_COULD_NOT_RETRIEVE_REGISTRYVALUE);
}

CCommandLineInfo cmdInfo;
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
AfxEnableControlContainer();

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

// BOOL bOk = AfxOleInit(); // start up the ole libraries.

// Change the registry key under which our settings are stored.
// You should modify this string to be something appropriate
// such as the name of your company or organization.
//
LoadStdProfileSettings(); // Load standard INI file options (including MRU)

// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
//
m_pAddInDocTemplate = new CAddaptDocTemplate( IDR_ADDIN,
RUNTIME_CLASS(CAddaptDoc),
RUNTIME_CLASS(CAddInChildFrame),
RUNTIME_CLASS(CAddaptView));
AddDocTemplate(m_pAddInDocTemplate);

m_pFaultFIFOTemplate = new CAddaptDocTemplate( IDR_FAULTFIFO,
RUNTIME_CLASS(CFaultFIFODoc),
RUNTIME_CLASS(CFaultFIFOChildFrame), 
RUNTIME_CLASS(CFaultFIFOView));
AddDocTemplate(m_pFaultFIFOTemplate);

m_pFormTemplate = new CMultiDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CPasswordDoc),
RUNTIME_CLASS(CAddaptMDIChildFrame),
RUNTIME_CLASS(CSecurityFormGridWnd));
AddDocTemplate(m_pFormTemplate);

m_pDriveStatusTemplate = new CMultiDocTemplate( IDR_MAINFRAME,
RUNTIME_CLASS(CDriveStatusDoc),
RUNTIME_CLASS(CDriveFrame),
RUNTIME_CLASS(CDriveStatusFormGridWnd));
AddDocTemplate(m_pDriveStatusTemplate);

m_pCompareTemplate = new CAddaptDocTemplate( IDR_MAINFRAME,
RUNTIME_CLASS(CTextDoc),
RUNTIME_CLASS(CAddaptMDIChildFrame),
RUNTIME_CLASS(CTextView));
AddDocTemplate(m_pCompareTemplate);

// create password obj ptr
m_pPW = (CPassword *) new CPassword;

CPWReg *pReg = (CPWReg *) new CPWReg; // create register entry
m_pPW->SetUserType((_securitylevel)pReg->GetDefaultSecurityLevel());

SetAvailability();

/* example for CTimeSpan::Format
CTimeSpan ts( 3, 1, 5, 12 ); // 3 days, 1 hour, 5 min, and 12 sec
CString s = ts.Format( "Total days: 00%D, hours: %H, mins: %M, secs: %S" );
//ASSERT( s == "Total days: 3, hours: 01, mins: 05, secs: 12" );


AfxMessageBox(s);
*/

COleDateTime timeStart; 
// COleDateTime timeEnd(30.0);
COleDateTimeSpan timePassed;


int daysLeft = 3;

timeStart = COleDateTime::GetCurrentTime(); // Date and time of the installation. 
// timeEnd = timeStart + timePassed; 
timePassed = COleDateTime::GetCurrentTime() - timeStart;
daysLeft = 30 - static_cast<int>(timePassed.GetTotalDays());

CString strMessage;

if (daysLeft > 3 && daysLeft <=3)

{
strMessage.Format("ADDapt was not activated within 30 days of \n"
"installation and currently not function. \n"
"To activate, please call Avtron Field Service \n"
"at 216-642-1230 ext 1214.\n"
"The %d" " days trial period started on the day you installed ADapt.\n"
"Do you want to Proceed?", daysLeft);

}
// AfxMessageBox(strMessage);

else 
{
strMessage.Format(_T("ADDapt is operating under a trial license. \n")
_T("You have %d" " days to activate ADDapt with valid license.\n")
_T("To activate, Please call Avtron Field Service at 216 642-1230 ext 1214. \n")
_T("Do you want to Proceed?"), daysLeft);
/*
strMessage("You have 00%d" " days to activate ADDapt with valid license.", daysLeft);
*/
}

AfxMessageBox(strMessage, MB_OKCANCEL);

//AfxMessageBox("Your 30 days trial Period is over, please Call Field Service at (216)642-1230 ext 1214 to purchase Complete software package");

// read installed add-ins file
HRESULT hr = GetDirectoryFromPath(m_pszHelpFilePath, m_strExeDirectory);

CString strAddInFile = m_strExeDirectory + "addins.bin";
CAddInManager AddInManager(strAddInFile);
AddInManager.ReadAll(m_ordAddIns); // mem 242 leak

// create main MDI Frame window pMainFrame = new CMainFrame;
pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
{
return FALSE;
}
m_pMainWnd = pMainFrame;

// Parse command line for standard shell commands, DDE, file open
ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line
if (cmdInfo.m_nShellCommand != CCommandLineInfo::FileNew)
if (!ProcessShellCommand(cmdInfo))
return FALSE;

if( !m_dwSerialNum ) 
CreateSerialNum();

try
{
m_ADDAPT.Create();
m_ADDAPT.Initialize();
}
catch(CUserException* e)
{
e->Delete();
}

pMainFrame->UpdateLevel(m_pPW->GetUserLevel());

GXInit(); // initialize grid
m_bGXInitialized = TRUE;

CString strConfig;
try
{
strConfig = m_ADDAPT.DefaultCfgFile();
}
catch(CUserException* e)
{ // no message, there is not window yet
e->Delete();
}

if( !strConfig.IsEmpty() )
pMainFrame->UpdateConfig(strConfig);

// get drive status check from registry
if( ReadDSCheck() )
{ // trick msg handler into thinking
// a user selected drv status grid
m_bConfigOpen = FALSE;
OnDSGrid();
pMainFrame->OnSectionStatus();
}

// done with password reg ptr
delete pReg;

m_bMonitorActivity = CParameters::MonitorActivity();

ULONG lLength = MAX_PATH;
CString strValue, strTitle;
CRegKey Key;
LONG lResult = Key.Open(HKEY_LOCAL_MACHINE,"SOFTWARE\\Avtron Manufacturing\\ADDapt\\", KEY_ALL_ACCESS);
if ( lResult == ERROR_SUCCESS )
{ 
lResult = Key.QueryValue(strValue.GetBuffer(MAX_PATH), "Company", &lLength);
if( lResult == ERROR_SUCCESS )
{
strValue.ReleaseBuffer();
if( strValue == "Avtron" )
{
m_bBeta = TRUE;
strTitle = pMainFrame->GetTitle();
pMainFrame->SetTitle(strTitle + " Beta Release");
}
}
}

// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();

return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
DWORD m_dwSerialNum;
DWORD m_dwCode; 

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
m_dwSerialNum = CParameters::SerialNumber();
m_dwCode = CParameters::GetOS();
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
DDX_Text(pDX, IDC_SN, m_dwSerialNum);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();

if(m_dwCode == 9)
GetDlgItem(IDC_STATIC1)->SetWindowText("ADDapt 98 Version 17");

else
{
CString msg = "ADDapt ";
msg += CParameters::Version();
GetDlgItem(IDC_STATIC1)->SetWindowText(msg);
}
return TRUE;
}

// App command to run the dialog
void CAddaptApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal(); 
}

/////////////////////////////////////////////////////////////////////////////
// CAddaptApp commands
void CAddaptApp::OnPasswordConfig()
{ 
if ( m_pPW->IsLoginRequired() )
SecurityMgr();

else
{ // only executed once
CAppIO file;

if ( file.Initialize() )
SecurityMgr();
}
}

void CAddaptApp::ExceptionErrorMessage(CString msg, int num)
{
CString s1;
CString s2;
s1.LoadString(IDS_COM_ERROR);
s2.Format(s1, msg, num);
AfxMessageBox(s2);
}

void CAddaptApp::SetAvailability()
{
m_bSA_Available = FALSE;
m_bProductCodeFound = m_bViewer_Available = m_bTuner_Available = FALSE;
m_bBlockViewer_Available = m_bDriveTuner_Available = FALSE;
m_dwSerialNum = CParameters::SerialNumber();

if( !(m_dwProductCode = CParameters::ProductCode()) )
return; // not available

DWORD num = (((m_dwSerialNum + 1001000) & 0xffc1f) + m_dwProductCode);
DWORD code = (num & 0x03e0);

if(code < 32 || code > 0x03e0)
return; // bogus product code

if(code & 0x020)
{ // 100 boot ups then reinstall or pay up
m_bProductCodeFound = TRUE;
m_bSA_Available = TRUE;

if( m_dwProductCode == 736 )
{ // it's temp
int bootups = CParameters::Boots();
if( bootups > 50 )
{
m_bSA_Available = FALSE;
m_bProductCodeFound = FALSE;
AfxMessageBox("Temporary Access Expired\nCall Avtron to Activate this Product");
}
else
CParameters::Boots(++bootups);
}
}

if(code & 0x0100)
m_bViewer_Available = TRUE;

if(code & 0x0200)
m_bTuner_Available = TRUE;
}

void CAddaptApp::CreateSerialNum()
{ // if we are here, all options are false
srand(time(NULL)); // make random
m_dwSerialNum = rand(); // rand returns int from 0 to 32,767
//m_dwSerialNum &= 0x1111111000111111; // 65087
CParameters::SerialNumber(m_dwSerialNum);
}

void CAddaptApp::SecurityMgr()
{
m_pPW->SetPWGridFlag(TRUE); 
((CMultiDocTemplate*)m_pFormTemplate)->OpenDocumentFile(NULL);
}

void CAddaptApp::OnEnableChangePassword(CCmdUI* pCmdUI)
{
TCHAR user[MAX_USERLEN];
m_pPW->GetBackdoorUser(user);

if ( m_pPW->IsUserLogged() && ( (m_pPW->GetCurrentUser().Compare(user)) != 0) )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnEnableFileNew(CCmdUI* pCmdUI)
{
if ( m_pPW->GetUserType() > sl_operator )
{
if( m_bConfigOpen )
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnEnableCalFileOpen(CCmdUI* pCmdUI) 
{
if ( m_pPW->GetUserType() > sl_operator )
pCmdUI->Enable(TRUE);

else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnChangePassword() 
{
CChange passDlg;
passDlg.DoModal();
}

void CAddaptApp::OnEnableDefaultConfig(CCmdUI* pCmdUI)
{
CString strFilename;
CFileStatus status; // get file size

if( m_bGXInitialized ) // causes a delay
{
strFilename = m_ADDAPT.DefaultCfgFile();

if ( CFile::GetStatus(strFilename, status) )
{
if( m_bConfigOpen )
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
else
pCmdUI->Enable(FALSE);
}
}

void CAddaptApp::OnEnableConfigFilesDlg(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE);
}

void CAddaptApp::OnConfigFilesDlg() 
{
if( ((CAddaptApp*)AfxGetApp())->ReadDSCheck() == ACM_XMIT )
{
CString msg;
msg.LoadString(IDS_DISABLE_COMM);
AfxMessageBox(msg);
return;
}

CString strTest = "Options";
COptionsPropertySheet dlg((LPCTSTR)strTest, AfxGetMainWnd());

if (dlg.DoModal() == IDOK)
{
if(dlg.m_bRestartADDAPT)
AfxMessageBox(IDS_RESTART_ADDAPT, MB_OK);

AfxGetMainWnd()->SendMessageToDescendants(AVM_PARAMETERS_CHANGED);

try
{
pMainFrame->UpdateConfig(m_ADDAPT.DefaultCfgFile());
}
catch(CUserException* e)
{
((CAddaptApp*)AfxGetApp())->ExceptionErrorMessage(THIS_FILE, __LINE__);
e->Delete();
}
}
}

void CAddaptApp::OnEnablePasswordConfig(CCmdUI* pCmdUI)
{
if ( m_pPW->GetUserType() > sl_observer && !m_pPW->IsGridOpen() )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnLogin()
{ // Launch the login dialog
CString s;
CAuthenticateUser dlg;
dlg.DoModal();

if ( m_pPW->IsUserLogged() )
{
pMainFrame->UpdateUser(m_pPW->GetCurrentUser());
pMainFrame->UpdateLevel(m_pPW->GetUserLevel());

// dont store backdoor user
TCHAR user[MAX_USERLEN];
m_pPW->GetBackdoorUser(user);

if ( (m_pPW->GetCurrentUser().Compare(user)) == 0 )
{ // overwrite backdoor user name
s.LoadString(IDS_DEFAULTUSER);
pMainFrame->UpdateUser(s);
return; // backdoor user, stop
}
CPWReg reg; // record user for next login
reg.WrtLastUser(m_pPW->GetCurrentUser());
}
}

void CAddaptApp::OnEnableLogin(CCmdUI* pCmdUI)
{
if ( m_pPW->IsLoginRequired() )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnLogout()
{
m_pPW->ClrUserLogged();
m_pPW->SetDefaultCurrentUser();

pMainFrame->UpdateUser(m_pPW->GetCurrentUser());
pMainFrame->UpdateLevel(m_pPW->GetUserLevel());

OnCloseDocs();
}

void CAddaptApp::OnCloseDocs()
{
if( ReadDSCheck() != ACM_OFFLINE)
{
int result = AfxMessageBox(IDS_LOGOUT, MB_YESNO);

if ( result == IDYES )
{
m_pPW->SetPWGridFlag(FALSE); 
m_pFormTemplate->CloseAllDocuments(TRUE);
m_pDriveStatusTemplate->CloseAllDocuments(TRUE);
m_pFaultFIFOTemplate->CloseAllDocuments(TRUE);
}
}
}

void CAddaptApp::OnEnableProductCode(CCmdUI* pCmdUI)
{
if ( m_pPW->GetUserType() == sl_engineer )
pCmdUI->Enable(TRUE);

else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnProductCode()
{
CProductCodeDlg dlg;
dlg.DoModal();
}

void CAddaptApp::OnEnableLogout(CCmdUI* pCmdUI)
{
if ( m_pPW->IsUserLogged() )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}

CString CAddaptApp::GetLastAddaptUser()
{
CPWReg reg;
CString s = reg.GetLastUser();
return s;
}

void CAddaptApp::OnCheckActivityIndicator(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE);
pCmdUI->SetCheck(m_bMonitorActivity);
}

void CAddaptApp::OnActivityIndicator()
{
m_bMonitorActivity = !m_bMonitorActivity;
CParameters::MonitorActivity(m_bMonitorActivity);

WPARAM wParam = MAKEWPARAM(m_bMonitorActivity, 0);

if( !m_bMonitorActivity )
WriteDSCheck(ACM_OFF);
else // start offline
WriteDSCheck(ACM_OFFLINE);
}

void CAddaptApp::OnEnableDSGrid(CCmdUI* pCmdUI)
{
m_bConfigOpen = ReadDSCheck();
pCmdUI->SetCheck(m_bConfigOpen);
}

void CAddaptApp::OnDSGrid()
{
}

void CAddaptApp::OnAddInNew(CAddInCollectable* pAddInCollectable) 
{
if( pAddInCollectable->m_strLaunchName.isNull() )
{
CAddInCreate AddInCreate;
CLSID clsid;
IID iid;
UuidFromString((UCHAR*)(LPCSTR)pAddInCollectable->m_strClsid, &clsid);
UuidFromString((UCHAR*)(LPCSTR)pAddInCollectable->m_strIID, &iid);

if( AddInCreate.Create(clsid, iid) )
{
IAddIn* pUnknown = NULL;
try
{
pUnknown = AddInCreate.New(CDispatchWrapper((IDispatch*)NULL), m_pPW->GetUserType());
}
catch(CUserException* e)
{
// user cancelled
e->Delete();
return;
}
// cancel pressed - probably
if( pUnknown != NULL )
m_pAddInDocTemplate->OpenDocumentFile(pUnknown, pAddInCollectable);
}
else
{
CString strText, strLoadString;
strLoadString.LoadString(IDS_ERROR_CANT_CREATE_FILE);
strText.Format(strLoadString, pAddInCollectable->m_strFileType);
AfxMessageBox(strText);
}
}
else
{
STARTUPINFO startUpInfo;
PROCESS_INFORMATION processInfo;
::ZeroMemory(&startUpInfo, sizeof(STARTUPINFO));
::ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));

startUpInfo.cb = sizeof(STARTUPINFO);
startUpInfo.lpTitle = strdup(pAddInCollectable->m_strFileType);

CString strPath;
HRESULT hr = GetDirectoryFromPath(m_pszHelpFilePath, strPath);
strPath += pAddInCollectable->m_strLaunchName;

BOOL bSuccess; 
bSuccess = ::CreateProcess( NULL, (LPSTR)(LPCSTR)strPath, NULL,
NULL, FALSE, CREATE_NEW_CONSOLE,
NULL, NULL, &startUpInfo, &processInfo );

if( !bSuccess )
{
CString strLoadString, strMessage;
strLoadString.LoadString(IDS_CANT_LAUNCH);
strMessage.Format(strLoadString, pAddInCollectable->m_strLaunchName);
}
}
}

void CAddaptApp::OnFileNew() 
{
CDocument* pDoc = m_pDriveStatusTemplate->OpenDocumentFile(NULL);

if( pDoc != NULL )
{ // when the user cancels all hell happens
// if when we try to open a no name file
// this quickly fixes that problem!
//m_bConfigOpen = TRUE;
OnAddRow();
}
}

CAddaptApp::~CAddaptApp()
{
m_ordAddIns.clearAndDestroy();
TRACE("CAddaptApp destructor\n");

if( m_pPW ) 
delete m_pPW;
}

int CAddaptApp::ExitInstance()
{
// persistent monitoring activity state
CParameters::MonitorActivity(m_bMonitorActivity);

if( ReadDSCheck() != ACM_OFFLINE )
{
m_pPW->SetPWGridFlag(FALSE); 
m_pFormTemplate->CloseAllDocuments(TRUE);
m_pDriveStatusTemplate->CloseAllDocuments(TRUE);
}
return CWinApp::ExitInstance();
}

void CAddaptApp::OnCalFileOpen() 
{
if ( m_pPW->GetUserType() == sl_observer )
return;

if( !m_bProductCodeFound )
{
AfxMessageBox(IDS_PRODUCTCODE_ERROR);
return;
}

AMCString fileName;
CFileDialog dlgFile(TRUE);

CString title;
VERIFY(title.LoadString(AFX_IDS_OPENFILE));

dlgFile.m_ofn.Flags |= OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_SHAREAWARE;

// Add configuration file
CString strDriveFile, strFilter, strNxt;

strDriveFile.LoadString(IDS_DRIVE_CONFIG_FILE);
strFilter += strDriveFile + " (*.add)";
strFilter += (TCHAR)'\0'; 
strFilter += "*.add";
strFilter += (TCHAR)'\0'; 
strFilter += (TCHAR)'\0'; // last string

dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);

// display files if they exist
CString path = CParameters::DownloadableFiles();
if( !path.IsEmpty() )
dlgFile.m_ofn.lpstrInitialDir = path.GetBuffer(MAX_PATH);

BOOL bResult = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
fileName.ReleaseBuffer();

if( bResult )
{
CDocument* pDoc = OpenDocumentFile(fileName);
}
}

void CAddaptApp::OnFileOpen()
{
if ( m_pPW->GetUserType() == sl_observer )
return;

AMCString fileName;
CFileDialog dlgFile(TRUE);

CString title;
VERIFY(title.LoadString(AFX_IDS_OPENFILE));

dlgFile.m_ofn.Flags |= OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_SHAREAWARE;

// Add configuration file
CString strDriveFile, strFilter, strNxt;

strFilter = "ADDapt Files (*.cfg; *.add; *.alz; *.cmp)";
strFilter += (TCHAR)'\0'; 
strFilter += "*.cfg; *.add; *.alz; *.cmp";
strFilter += (TCHAR)'\0'; 

strDriveFile.LoadString(IDS_ADDAPT_CONFIG_FILE);
strFilter += strDriveFile + " (*.cfg)";
strFilter += (TCHAR)'\0'; 
strFilter += "*.cfg";
strFilter += (TCHAR)'\0'; 

strDriveFile.LoadString(IDS_ANALYZER_FILE);
strFilter += strDriveFile + " (*.alz)";
strFilter += (TCHAR)'\0'; 
strFilter += "*.alz";
strFilter += (TCHAR)'\0'; 

strDriveFile.LoadString(IDS_DRIVE_CONFIG_FILE);
strFilter += strDriveFile + " (*.add)";
strFilter += (TCHAR)'\0'; 
strFilter += "*.add";
strFilter += (TCHAR)'\0'; 

strDriveFile.LoadString(IDS_DRIVE_COMPARE_FILE);
strFilter += strDriveFile + " (*.cmp)";
strFilter += (TCHAR)'\0'; 
strFilter += "*.cmp";
strFilter += (TCHAR)'\0'; 
strFilter += (TCHAR)'\0'; // last string

dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);

// display files if they exist
CString path = CParameters::DownloadableFiles();
if( !path.IsEmpty() )
dlgFile.m_ofn.lpstrInitialDir = path.GetBuffer(MAX_PATH);

BOOL bResult = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
fileName.ReleaseBuffer();

if( bResult )
CDocument* pDoc = OpenDocumentFile(fileName);
}

CDocument* CAddaptApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
BOOL bCfg, bDoc, bEmpty;
bCfg = bDoc = bEmpty = FALSE;
CString s(lpszFileName);

s.MakeLower();
if(s.Right(3) == "cfg")
{
if( m_bConfigOpen )
{
AfxMessageBox(ID_CANNOT_OPEN);
return NULL;
}
else
bCfg = TRUE;
}
if(s.Right(3) == "cmp")
bDoc = TRUE;

if(s.Right(3) == "alz")
{
OnLaunchAnalyzer((LPCTSTR)s);
return NULL;
}

// this could come from cwinapp::onopenrecentfile
if ( m_pPW->GetUserType() == sl_observer )
{
if( !bCfg )
return NULL;
}

if( bDoc )
{
CDocument* pDoc;
pDoc = (CDocument*) ((CAddaptApp*)AfxGetApp())->m_pCompareTemplate->OpenDocumentFile(lpszFileName);
pDoc->SetTitle(lpszFileName);
}

// the exception handling below looks shakey. jeh
IAddIn* pAddInInterface = NULL;
CLSID clsid;
IID iid;

CAddInFile* pAddInFile = NULL;
try
{
pAddInFile = new CAddInFile;
pAddInFile->Initialize((AMCString)lpszFileName, "rb+");
}
catch(...)
{
if( pAddInFile != NULL )
delete pAddInFile;

// Not an Addin file - try configuration file
// this used to be for something, use it to test for empty config file
CConfigurationFile TestFile;
TestFile.Create();
TestFile.Open(lpszFileName);

if( TestFile.GetEOF() )
bEmpty = TRUE;

CString s = lpszFileName; 
CDocument *pDoc = m_pDriveStatusTemplate->OpenDocumentFile(lpszFileName);

if( pDoc != NULL && !bEmpty )
{
pMainFrame->UpdateConfig(s);
}

return pDoc;
}
// we are on the addin path
if( !pAddInFile->isValid() )
return NULL;

CAddInCollectable* pAddIn, FindAddIn;
if( SUCCEEDED(pAddInFile->Open()) )
{
FindAddIn.m_dwAddInIdentifier = pAddInFile->m_AddInIdentifier.m_dwAddInIdentifier;
pAddIn = static_cast<caddincollectable*>(((CAddaptApp*)AfxGetApp())->m_ordAddIns.find(&FindAddIn));

if( pAddIn == NULL )
{
delete pAddInFile;
CString strTemp, strError;
strTemp.LoadString(IDS_ERROR_FILE_NOT_VALID);
strError.Format(strTemp, (LPCSTR)lpszFileName);
AfxMessageBox(strError);
return NULL;
}

UuidFromString((UCHAR*)(LPCSTR)pAddIn->m_strClsid, &clsid);
UuidFromString((UCHAR*)(LPCSTR)pAddIn->m_strIID, &iid);
CAddInCreate AddInCreate;

if( !AddInCreate.Create(clsid, iid) )
{
AfxMessageBox(IDS_ERROR_CANT_CREATE_ADDIN_CREATE);
return NULL;
}

delete pAddInFile;
pAddInInterface = AddInCreate.Open(lpszFileName, m_pPW->GetUserType());
if( pAddInInterface == NULL )
return NULL;
}
else
{
delete pAddInFile;
AfxMessageBox(g_strErrorMessage);
return NULL;
}

AddToRecentFileList(lpszFileName);
return m_pAddInDocTemplate->OpenDocumentFile(pAddInInterface, pAddIn);
}

void CAddaptApp::AddToRecentFileList(LPCTSTR lpszPathName)
{
CString s(lpszPathName);

if( s.Right(4) != ".cfg" )
CWinApp::AddToRecentFileList(lpszPathName);
}

BOOL CAddaptApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
/* if( m_hwndDialog != NULL )
{
if( lpMsg->hwnd == m_hwndDialog || ::IsChild(m_hwndDialog, lpMsg->hwnd) )
{
AfxMessageBox("Here for debugging dialog message bugs");
}
}*/

return CWinApp::ProcessMessageFilter(code, lpMsg);
}

BOOL CAddaptApp::PreTranslateMessage(MSG* pMsg)
{
// CG: The following lines were added by the Splash Screen component.
if (CSplashWnd::PreTranslateAppMessage(pMsg))
return TRUE;

return CWinApp::PreTranslateMessage(pMsg);
}

BOOL CAddaptApp::ReadDSCheck()
{
long state = CParameters::DisplayStatusGrid();
if( state < ACM_RESET || state > ACM_OFF )
state = ACM_OFF;

return state;
}

void CAddaptApp::WriteDSCheck(UINT bChk)
{ // write only changes
if( bChk != m_bDSGridStatus )
{
m_bDSGridStatus = bChk;
CParameters::DisplayStatusGrid(bChk); 
((CMainFrame*)AfxGetApp()->m_pMainWnd)->PostMessage(AVM_COMMUNICATION_EVENT,0,0);
}
}

void CAddaptApp::OnOpenDefaultCfg() 
{
CString strFilename;
try
{
strFilename = m_ADDAPT.DefaultCfgFile();
OpenDocumentFile(strFilename);
}
catch(CUserException* e)
{
((CAddaptApp*)AfxGetApp())->ExceptionErrorMessage(THIS_FILE, __LINE__);
e->Delete();
}
}

void CAddaptApp::RestoreWindows()
{
CRegKey Key;
LONG lResult = Key.Create(HKEY_LOCAL_MACHINE, "Software\\Avtron Manufacturing\\Addapt\\Save Window State");

FILETIME time;
TCHAR szBuffer[256];
DWORD dwSize = 256;
DWORD dwIndex = 0;

CWnd* pActiveWnd = NULL;

CRegKey MainWndKey;
if (MainWndKey.Open(Key, "Main Window") == ERROR_SUCCESS)
{
DWORD dwTemp;
CRect rcWnd;
MainWndKey.QueryValue(dwTemp, "Left");
rcWnd.left = dwTemp;
MainWndKey.QueryValue(dwTemp, "Top");
rcWnd.top = dwTemp;
MainWndKey.QueryValue(dwTemp, "Right");
rcWnd.right = dwTemp;
MainWndKey.QueryValue(dwTemp, "Bottom");
rcWnd.bottom = dwTemp;

AfxGetMainWnd()->SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height(), SWP_NOZORDER);
}

while (RegEnumKeyEx(Key.m_hKey, dwIndex++, szBuffer, &dwSize, NULL, NULL, NULL, &time) == ERROR_SUCCESS)
{
CRegKey FileKey;
FileKey.Open(Key, szBuffer);

CString strFilename;
DWORD dwLength = MAX_PATH;
if (FileKey.QueryValue(strFilename.GetBuffer(MAX_PATH), "Filename", &dwLength) == ERROR_SUCCESS)
{
strFilename.ReleaseBuffer();

DWORD dwTemp;
CRect rcWnd;
FileKey.QueryValue(dwTemp, "Left");
rcWnd.left = dwTemp;
FileKey.QueryValue(dwTemp, "Top");
rcWnd.top = dwTemp;
FileKey.QueryValue(dwTemp, "Right");
rcWnd.right = dwTemp;
FileKey.QueryValue(dwTemp, "Bottom");
rcWnd.bottom = dwTemp;

BOOL bMinimize = FALSE, bMaximize = FALSE, bActive = FALSE;
if (FileKey.QueryValue(dwTemp, "Minimized") == ERROR_SUCCESS)
bMinimize = dwTemp;

if (FileKey.QueryValue(dwTemp, "Maximized") == ERROR_SUCCESS)
bMaximize = dwTemp;

if (FileKey.QueryValue(dwTemp, "Active") == ERROR_SUCCESS)
bActive = dwTemp;

CDocument* pDoc = OpenDocumentFile(strFilename);
POSITION pos = pDoc->GetFirstViewPosition();
CView* pView = pDoc->GetNextView(pos);

if (!bActive)
pView->GetParent()->SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height(), SWP_NOZORDER);
else
pView->GetParent()->SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height(), SWP_NOZORDER);

if (bMinimize)
pView->GetParent()->PostMessage(WM_SYSCOMMAND, SC_MINIMIZE);
else if (bMaximize)
pView->GetParent()->PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE);

if (bActive)
pActiveWnd = pView->GetParent();
}
dwSize = 256;
}

if (pActiveWnd != NULL)
pActiveWnd->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}

void CAddaptApp::OnHelp() 
{
CString sHlp;
sHlp = (CParameters::DefaultDir() + "\\addapt.hlp");
::WinHelp(*AfxGetMainWnd(), sHlp, HELP_CONTENTS, 0);
}

void CAddaptApp::OnAddInCopy(CAddIn* pAddIn) 
{
IAddIn* pUnknown;
pAddIn->m_pUnknown->QueryInterface(IID_IAddIn, (void**)&pUnknown);
m_pAddInDocTemplate->OpenDocumentFile(pUnknown, NULL);
}

void CAddaptApp::OnEnableEditCut(CCmdUI* pCmdUI)
{
//if( m_bConfigOpen )
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnEnableAddSection(CCmdUI* pCmdUI)
{
if( m_pPW->GetUserType() > sl_operator &&
!m_pPW->IsGridOpen() && m_bCanAdd )
pCmdUI->Enable(TRUE);

else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnAddSection()
{
AfxGetMainWnd()->SendMessageToDescendants(AVM_NEWROW, 0, 0, TRUE);
}

void CAddaptApp::OnEnableAddRow(CCmdUI* pCmdUI)
{
if( m_pPW->GetUserType() > sl_operator && m_bConfigOpen )
pCmdUI->Enable(TRUE);

else
pCmdUI->Enable(FALSE);
}

void CAddaptApp::OnAddRow()
{
AfxGetMainWnd()->SendMessageToDescendants(AVM_GUESS, 0, 0, TRUE);
}

void CAddaptApp::OnLaunchAnalyzer(LPCTSTR filename) 
{
CString strValue;
CRegKey Key;

if( !((CAddaptApp*)AfxGetApp())->m_bSA_Available )
return;

LONG lResult = Key.Open(HKEY_LOCAL_MACHINE,"Software\\Avtron Manufacturing\\Addapt\\", KEY_ALL_ACCESS);
if ( lResult != ERROR_SUCCESS )
{ // The key does not exist in the registry, create it .
lResult = Key.Create(HKEY_LOCAL_MACHINE, "Software\\Avtron Manufacturing\\Addapt\\alz");
}

if( lResult != ERROR_SUCCESS )
AfxMessageBox(IDS_COULD_NOT_CREATE_REGISTRYKEY);

else // we can write to the registry
{ // start security with greatest access
lResult = Key.SetValue(filename, "alz");
if( lResult != ERROR_SUCCESS )
AfxMessageBox(IDS_COULD_NOT_CREATE_REGISTRYKEY);
}

STARTUPINFO startUpInfo;
PROCESS_INFORMATION processInfo;
::ZeroMemory(&startUpInfo, sizeof(STARTUPINFO));
::ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));

startUpInfo.cb = sizeof(STARTUPINFO);
startUpInfo.lpTitle = "Signal Analyzer";

BOOL bSuccess = ::CreateProcess( NULL, "C:\\Avtron\\ADDapt\\Analyzer.exe", 
NULL, NULL, FALSE, CREATE_NEW_CONSOLE,
NULL, NULL, &startUpInfo, &processInfo);

if (!bSuccess)
{
DWORD dwError = GetLastError();
CString strLoadString, strMessage;
strLoadString.LoadString(IDS_CANT_LAUNCH);
strMessage.Format(strLoadString, "Analyzer", dwError);
AfxMessageBox(strMessage);
}
}

BOOL CAddaptApp::TemplateOK(CSection Section, CString path)
{
CFileStatus stat;
CString sFilename, sLoadString, sText;
BOOL bFound=FALSE;

sFilename.Format("%s\\%0.6s%0.2s.tem", (LPCTSTR)path,
(LPCTSTR)Section.PartNumber(), (LPCTSTR)Section.VersionNumber());

if( CFile::GetStatus(sFilename, stat) )
bFound = TRUE;

if( !bFound )
AfxMessageBox(IDS_CANNOT_LOCATE_TEMPLATE_FILE);

return bFound;
}

void CAboutDlg::OnOK() 
{
// TODO: Add extra validation here

CDialog::OnOK();
}</int></crtdbg.h></stdlib.h></winnetwk.h>
Posted
Updated 15-Sep-11 5:32am
v8
Comments
DaveAuld 7-Sep-11 13:49pm    
Edit: code formatted.
Vijay Pate 7-Sep-11 16:17pm    
I did not see any code format
Vijay Pate 8-Sep-11 9:18am    
Dave you mentioned you have some code or comments :: I did not see anything .
Could you please mention more in detail.
Thank you.
TRK3 13-Sep-11 20:09pm    
What does the title of your question "How to SHOWWINDOW..." have to do with your actual question?

What is the problem you are having?

Assuming:
(a) you fixed the conditional to be <= 0 instead of > 0
and
(b) timeStart is actually recorded during installation and saved somewhere and retrieved from there each time the program is run

it seems like your code would work fine.
Vijay Pate 14-Sep-11 9:09am    
<= will not compile and it will not show me the days in %d in 2hd AfxMessageBox

1 solution

C++
if (daysLeft > 3 && daysLeft <=3)

How can this ever be true?
 
Share this answer
 
Comments
Vijay Pate 15-Sep-11 12:09pm    
I know it is not true it suppose to be
if (daysLeft > 0 && daysLeft <= 3) is I need but when I change the date in TOOLBAR other screen or AfxMessageBox is not appearing thats why I need to know how can I Store First Install date in Registry zand utilize registry date to determin how many days left before software expire from trial software.

Thank you very much.
Richard MacCutchan 15-Sep-11 13:59pm    
The trouble is you have posted so much code it is not easy to see where you are having a problem. Try doing some testing/debugging and narrow it down to the areas that fail and provide a clearer explanation of what happened.
Vijay Pate 15-Sep-11 14:12pm    
After installing this software I get first screen to mentioned enduser has 30 days as it is mentioned in::: strMessage.Format("ADDapt was not activated within 30 days of \n"
"installation and currently not function. \n"
"To activate, please call Avtron Field Service \n"
"at 216-642-1230 ext 1214.\n"
"The %d" " days trial period started on the day you installed ADapt.\n"
"Do you want to Proceed?", daysLeft);

After 25 days later that is 3days before software expire following message appear:::strMessage.Format(_T("ADDapt is operating under a trial license. \n")
_T("You have %d" " days to activate ADDapt with valid license.\n")
_T("To activate, Please call Avtron Field Service at 216 642-1230 ext 1214. \n")
_T("Do you want to Proceed?"), daysLeft);
But the value for %d never appear.

Thank you very much.

COleDateTime timeStart;
// COleDateTime timeEnd(30.0);
COleDateTimeSpan timePassed;


int daysLeft = 3;

timeStart = COleDateTime::GetCurrentTime(); // Date and time of the installation.
// timeEnd = timeStart + timePassed;
timePassed = COleDateTime::GetCurrentTime() - timeStart;
daysLeft = 30 - static_cast<int>(timePassed.GetTotalDays());

CString strMessage;

if (daysLeft > 0 && daysLeft <=3)

{
strMessage.Format("ADDapt was not activated within 30 days of \n"
"installation and currently not function. \n"
"To activate, please call Avtron Field Service \n"
"at 216-642-1230 ext 1214.\n"
"The %d" " days trial period started on the day you installed ADapt.\n"
"Do you want to Proceed?", daysLeft);

}
// AfxMessageBox(strMessage);

else
{
strMessage.Format(_T("ADDapt is operating under a trial license. \n")
_T("You have %d" " days to activate ADDapt with valid license.\n")
_T("To activate, Please call Avtron Field Service at 216 642-1230 ext 1214. \n")
_T("Do you want to Proceed?"), daysLeft);
Vijay Pate 15-Sep-11 14:16pm    
I also want to use installdate in REGISTRY and get teh date from registry to calculate how many days left before software expire, if that is available in VC++ 6.0
Richard MacCutchan 16-Sep-11 4:18am    
See here for information on registry functions. As to the situation above, you will need to use your debugger to check the values of the various variables to see what is happening.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900