binstar
asked on
Accessing another class' variable.
A password is entered in a dialog box (m_pw) and it is assigned to a variable (pw) in the Initinstance function of the application . I am unable to retrieve pw in the Button1 function of a view (passwordview).
I have tried making pw global but this doesn't work either. I have looked for info. in the help files but this basic question (how to access another class's variable?) is not specifically
addressed. I need to restrict access to an application I have written for a client.
I have tried making pw global but this doesn't work either. I have looked for info. in the help files but this basic question (how to access another class's variable?) is not specifically
addressed. I need to restrict access to an application I have written for a client.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> I need to restrict access to an application
>> I have written for a client
C++ does not "directly" provide a good means to prevent access to data in this sort of context. Its mechanisms are easily circumvented. The mechanisms its provides for protection are to prevent a programmer from accidentally accessing a value in a context where he/she shoudl not. They do not prevent a programmer or hacker from intentionally accessing the value.
The best thing to do would be to encrypt the password so that the client cannot "understand it" or "change it" without using your code and therefor subject to the restrictons you code wishes to make.
Does that help? If not can you provide more details?
>> I have written for a client
C++ does not "directly" provide a good means to prevent access to data in this sort of context. Its mechanisms are easily circumvented. The mechanisms its provides for protection are to prevent a programmer from accidentally accessing a value in a context where he/she shoudl not. They do not prevent a programmer or hacker from intentionally accessing the value.
The best thing to do would be to encrypt the password so that the client cannot "understand it" or "change it" without using your code and therefor subject to the restrictons you code wishes to make.
Does that help? If not can you provide more details?
ASKER
I do get access to the variable if it is in a class other than the main application class (where the variable is public also).
The problem is that I get " " instead of
the value that I put into the dialog which is "gugu".
So you are talking about run-time, not compile time, right?
Are you sure you are trying to get the password from the right object? i.e. each object of this class has its own password, could you be accessing the wrong object?
If that isnt't the problem, can you post the relevant code?
Are you sure you are trying to get the password from the right object? i.e. each object of this class has its own password, could you be accessing the wrong object?
If that isnt't the problem, can you post the relevant code?
ASKER
Hello nietod, the following is the sample source code. Please let me know
if I am sending too much, and I won't do it again.
1. CPasswordApp
2. The Dialog PWVu
3. CPasswordView
>>>>>>>>>>>> 1. CPasswordApp
// password.h : main header file for the PASSWORD application
//
#if !defined(AFX_PASSWORD_H__0 688FC2B_97 8D_11D3_85 27_0060674 B8FC6__INC LUDED_)
#define AFX_PASSWORD_H__0688FC2B_9 78D_11D3_8 527_006067 4B8FC6__IN CLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
#include "Password_i.h"
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordApp:
// See password.cpp for the implementation of this class
//
class CPasswordApp : public CWinApp
{
public:
char thepw(); // <<--
CString apw;
virtual BOOL PreTranslateMessage(MSG* pMsg);
CPasswordApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPasswordA pp)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
// Implementation
COleTemplateServer m_server;
// Server object for document creation
//{{AFX_MSG(CPasswordApp)
afx_msg void OnAppAbout();
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
BOOL m_bATLInited;
private:
BOOL InitATL();
};
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PASSWORD_H__0 688FC2B_97 8D_11D3_85 27_0060674 B8FC6__INC LUDED_)
>>>>>
// password.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "password.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "IpFrame.h"
#include "passwordSet.h"
#include "passwordDoc.h"
#include "passwordView.h"
#include <initguid.h>
#include "Password_i.c"
#include "PWVu.h"
#include "Splash.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordApp
BEGIN_MESSAGE_MAP(CPasswor dApp, CWinApp)
//{{AFX_MSG_MAP(CPasswordA pp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_S ETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordApp construction
CPasswordApp::CPasswordApp ()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// The one and only CPasswordApp object
CPasswordApp theApp;
// This identifier was generated to be statistically unique for your app.
// You may change it if you prefer to choose a specific identifier.
// {0688FC26-978D-11D3-8527-0 060674B8FC 6}
static const CLSID clsid =
{ 0x688fc26, 0x978d, 0x11d3, { 0x85, 0x27, 0x0, 0x60, 0x67, 0x4b, 0x8f, 0xc6 } };
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordApp initialization
BOOL CPasswordApp::InitInstance ()
{
// CG: The following block was added by the Splash Screen component.
{
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashSc reen(cmdIn fo.m_bShow Splash);
}
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT _FAILED);
return FALSE;
}
if (!InitATL())
return FALSE;
AfxEnableControlContainer( );
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
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.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_PASSWOTYPE,
RUNTIME_CLASS(CPasswordDoc ),
RUNTIME_CLASS(CChildFrame) , // custom MDI child frame
RUNTIME_CLASS(CPasswordVie w));
pDocTemplate->SetContainer Info(IDR_P ASSWOTYPE_ CNTR_IP);
pDocTemplate->SetServerInf o(
IDR_PASSWOTYPE_SRVR_EMB, IDR_PASSWOTYPE_SRVR_IP,
RUNTIME_CLASS(CInPlaceFram e));
AddDocTemplate(pDocTemplat e);
// Connect the COleTemplateServer to the document template.
// The COleTemplateServer creates new documents on behalf
// of requesting OLE containers by using information
// specified in the document template.
m_server.ConnectTemplate(c lsid, pDocTemplate, FALSE);
// Register all OLE server factories as running. This enables the
// OLE libraries to create objects from other applications.
COleTemplateServer::Regist erAll();
_Module.RegisterClassObjec ts(CLSCTX_ LOCAL_SERV ER,
REGCLS_MULTIPLEUSE);
// Note: MDI applications register all server objects without regard
// to the /Embedding or /Automation on the command line.
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(ID R_MAINFRAM E))
return FALSE;
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Check to see if launched as OLE server
if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
{
// Application was run with /Embedding or /Automation. Don't show the
// main window in this case.
return TRUE;
}
// When a server application is launched stand-alone, it is a good idea
// to update the system registry in case it has been damaged.
m_server.UpdateRegistry(OA T_INPLACE_ SERVER);
_Module.UpdateRegistryFrom Resource(I DR_PASSWOR D, TRUE);
_Module.RegisterServer(TRU E);
COleObjectFactory::UpdateR egistryAll ();
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdI nfo))
return FALSE;
// The main window has been initialized, so show and update it.
// PWVu getpw;
// getpw.DoModal();
// pw=getpw.m_pw;
thepw(); <<----Password retrieved from dialog, after splash screen, but before Passwordview.
pMainFrame->ShowWindow(m_n CmdShow);
pMainFrame->UpdateWindow() ;
return TRUE;
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchan ge* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDl g)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange( CDataExcha nge* pDX)
{
CDialog::DoDataExchange(pD X);
//{{AFX_DATA_MAP(CAboutDlg )
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDl g, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CPasswordApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordApp message handlers
CPasswordModule _Module;
BEGIN_OBJECT_MAP(ObjectMap )
END_OBJECT_MAP()
LONG CPasswordModule::Unlock()
{
AfxOleUnlockApp();
return 0;
}
LONG CPasswordModule::Lock()
{
AfxOleLockApp();
return 1;
}
int CPasswordApp::ExitInstance ()
{
if (m_bATLInited)
{
_Module.RevokeClassObjects ();
_Module.Term();
}
return CWinApp::ExitInstance();
}
BOOL CPasswordApp::InitATL()
{
m_bATLInited = TRUE;
_Module.Init(ObjectMap, AfxGetInstanceHandle());
_Module.dwThreadID = GetCurrentThreadId();
return TRUE;
}
BOOL CPasswordApp::PreTranslate Message(MS G* pMsg)
{
// CG: The following lines were added by the Splash Screen component.
if (CSplashWnd::PreTranslateA ppMessage( pMsg))
return TRUE;
return CWinApp::PreTranslateMessa ge(pMsg);
}
char CPasswordApp::thepw() <<----
{
PWVu getpw;
getpw.DoModal();
// apw=getpw.m_pw;
getpw.pw=getpw.m_pw;
return 0;
}
>>>>>>> 2. THE DIALOG
#if !defined(AFX_PWVU_H__0688F C4B_978D_1 1D3_8527_0 060674B8FC 6__INCLUDE D_)
#define AFX_PWVU_H__0688FC4B_978D_ 11D3_8527_ 0060674B8F C6__INCLUD ED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// PWVu.h : header file
//
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// PWVu dialog
class PWVu : public CDialog
{
// Construction
public:
PWVu(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(PWVu)
enum { IDD = IDD_FORMVIEW };
CString m_pw; <<-----
//}}AFX_DATA
CString pw; <<----
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(PWVu)
protected:
virtual void DoDataExchange(CDataExchan ge* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(PWVu)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PWVU_H__0688F C4B_978D_1 1D3_8527_0 060674B8FC 6__INCLUDE D_)
>>>>>>>
// PWVu.cpp : implementation file
//
#include "stdafx.h"
#include "password.h"
#include "PWVu.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// PWVu dialog
PWVu::PWVu(CWnd* pParent /*=NULL*/)
: CDialog(PWVu::IDD, pParent)
{
//{{AFX_DATA_INIT(PWVu)
m_pw = _T("");
//}}AFX_DATA_INIT
}
void PWVu::DoDataExchange(CData Exchange* pDX)
{
CDialog::DoDataExchange(pD X);
//{{AFX_DATA_MAP(PWVu)
DDX_Text(pDX, IDC_EDIT1, m_pw);
//}}AFX_DATA_MAP
pw=m_pw;
}
BEGIN_MESSAGE_MAP(PWVu, CDialog)
//{{AFX_MSG_MAP(PWVu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// PWVu message handlers
>>>>>> 3. THE VIEW AFTER THE DIALOG, FROM WHICH I WANT TO RETRIEVE THE VALUE OF THE PASSWORD
// passwordView.h : interface of the CPasswordView class
//
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
//{{AFX_INCLUDES()
#include "crystalctrl.h"
//}}AFX_INCLUDES
#if !defined(AFX_PASSWORDVIEW_ H__0688FC3 6_978D_11D 3_8527_006 0674B8FC6_ _INCLUDED_ )
#define AFX_PASSWORDVIEW_H__0688FC 36_978D_11 D3_8527_00 60674B8FC6 __INCLUDED _
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CPasswordCntrItem;
class CPasswordSet;
class CPasswordView : public CRecordView
{
protected: // create from serialization only
CPasswordView();
DECLARE_DYNCREATE(CPasswor dView)
public:
//{{AFX_DATA(CPasswordView )
enum { IDD = IDD_PASSWORD_FORM };
CPasswordSet* m_pSet;
CCrystalCtrl m_cryst;
//}}AFX_DATA
// Attributes
public:
CPasswordDoc* GetDocument();
// m_pSelection holds the selection to the current CPasswordCntrItem.
// For many applications, such a member variable isn't adequate to
// represent a selection, such as a multiple selection or a selection
// of objects that are not CPasswordCntrItem objects. This selection
// mechanism is provided just to help you get started.
// TODO: replace this selection mechanism with one appropriate to your app.
CPasswordCntrItem* m_pSelection;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPasswordV iew)
public:
virtual CRecordset* OnGetRecordset();
virtual BOOL PreCreateWindow(CREATESTRU CT& cs);
protected:
virtual void DoDataExchange(CDataExchan ge* pDX); // DDX/DDV support
virtual void OnInitialUpdate(); // called first time after construct
virtual BOOL OnPreparePrinting(CPrintIn fo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual BOOL IsSelected(const CObject* pDocItem) const;// Container support
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CPasswordView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CPasswordView)
afx_msg void OnDestroy();
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnInsertObject();
afx_msg void OnCancelEditCntr();
afx_msg void OnCancelEditSrvr();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in passwordView.cpp
inline CPasswordDoc* CPasswordView::GetDocument ()
{ return (CPasswordDoc*)m_pDocument ; }
#endif
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PASSWORDVIEW_ H__0688FC3 6_978D_11D 3_8527_006 0674B8FC6_ _INCLUDED_ )
>>>>>
// passwordView.cpp : implementation of the CPasswordView class
//
#include "stdafx.h"
#include "password.h"
#include "passwordSet.h"
#include "passwordDoc.h"
#include "CntrItem.h"
#include "passwordView.h"
#include "PWVu.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordView
IMPLEMENT_DYNCREATE(CPassw ordView, CRecordView)
BEGIN_MESSAGE_MAP(CPasswor dView, CRecordView)
//{{AFX_MSG_MAP(CPasswordV iew)
ON_WM_DESTROY()
ON_WM_SETFOCUS()
ON_WM_SIZE()
ON_COMMAND(ID_OLE_INSERT_N EW, OnInsertObject)
ON_COMMAND(ID_CANCEL_EDIT_ CNTR, OnCancelEditCntr)
ON_COMMAND(ID_CANCEL_EDIT_ SRVR, OnCancelEditSrvr)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_D IRECT, CRecordView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_P REVIEW, CRecordView::OnFilePrintPr eview)
END_MESSAGE_MAP()
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordView construction/destruction
CPasswordView::CPasswordVi ew()
: CRecordView(CPasswordView: :IDD)
{
//{{AFX_DATA_INIT(CPasswor dView)
m_pSet = NULL;
//}}AFX_DATA_INIT
m_pSelection = NULL;
// TODO: add construction code here
}
CPasswordView::~CPasswordV iew()
{
}
void CPasswordView::DoDataExcha nge(CDataE xchange* pDX)
{
CRecordView::DoDataExchang e(pDX);
//{{AFX_DATA_MAP(CPassword View)
DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_CUSTNAME, m_pSet);
DDX_Control(pDX, IDC_CRYSTALREPORT1, m_cryst);
//}}AFX_DATA_MAP
}
BOOL CPasswordView::PreCreateWi ndow(CREAT ESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWind ow(cs);
}
void CPasswordView::OnInitialUp date()
{
m_pSet = &GetDocument()->m_password Set;
CRecordView::OnInitialUpda te();
ResizeParentToFit();
// TODO: remove this code when final selection model code is written
m_pSelection = NULL; // initialize selection
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordView printing
BOOL CPasswordView::OnPreparePr inting(CPr intInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPasswordView::OnBeginPrin ting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CPasswordView::OnEndPrinti ng(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CPasswordView::OnDestroy()
{
// Deactivate the item on destruction; this is important
// when a splitter view is being used.
CRecordView::OnDestroy();
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA ctiveItem( this);
if (pActiveItem != NULL && pActiveItem->GetActiveView () == this)
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetI nPlaceActi veItem(thi s) == NULL);
}
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// OLE Client support and commands
BOOL CPasswordView::IsSelected( const CObject* pDocItem) const
{
// The implementation below is adequate if your selection consists of
// only CPasswordCntrItem objects. To handle different selection
// mechanisms, the implementation here should be replaced.
// TODO: implement this function that tests for a selected OLE client item
return pDocItem == m_pSelection;
}
void CPasswordView::OnInsertObj ect()
{
// Invoke the standard Insert Object dialog box to obtain information
// for new CPasswordCntrItem object.
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return;
BeginWaitCursor();
CPasswordCntrItem* pItem = NULL;
TRY
{
// Create new item connected to this document.
CPasswordDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new CPasswordCntrItem(pDoc);
ASSERT_VALID(pItem);
// Initialize the item from the dialog data.
if (!dlg.CreateItem(pItem))
AfxThrowMemoryException(); // any exception will do
ASSERT_VALID(pItem);
if (dlg.GetSelectionType() == COleInsertDialog::createNe wItem)
pItem->DoVerb(OLEIVERB_SHO W, this);
ASSERT_VALID(pItem);
// As an arbitrary user interface design, this sets the selection
// to the last item inserted.
// TODO: reimplement selection as appropriate for your application
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL) ;
}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_T O_CREATE);
}
END_CATCH
EndWaitCursor();
}
// The following command handler provides the standard keyboard
// user interface to cancel an in-place editing session. Here,
// the container (not the server) causes the deactivation.
void CPasswordView::OnCancelEdi tCntr()
{
// Close any in-place active item on this view.
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA ctiveItem( this);
if (pActiveItem != NULL)
{
pActiveItem->Close();
}
ASSERT(GetDocument()->GetI nPlaceActi veItem(thi s) == NULL);
}
// Special handling of OnSetFocus and OnSize are required for a container
// when an object is being edited in-place.
void CPasswordView::OnSetFocus( CWnd* pOldWnd)
{
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA ctiveItem( this);
if (pActiveItem != NULL &&
pActiveItem->GetItemState( ) == COleClientItem::activeUISt ate)
{
// need to set focus to this item if it is in the same view
CWnd* pWnd = pActiveItem->GetInPlaceWin dow();
if (pWnd != NULL)
{
pWnd->SetFocus(); // don't call the base class
return;
}
}
CRecordView::OnSetFocus(pO ldWnd);
}
void CPasswordView::OnSize(UINT nType, int cx, int cy)
{
CRecordView::OnSize(nType, cx, cy);
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA ctiveItem( this);
if (pActiveItem != NULL)
pActiveItem->SetItemRects( );
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// OLE Server support
// The following command handler provides the standard keyboard
// user interface to cancel an in-place editing session. Here,
// the server (not the container) causes the deactivation.
void CPasswordView::OnCancelEdi tSrvr()
{
GetDocument()->OnDeactivat eUI(FALSE) ;
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordView diagnostics
#ifdef _DEBUG
void CPasswordView::AssertValid () const
{
CRecordView::AssertValid() ;
}
void CPasswordView::Dump(CDumpC ontext& dc) const
{
CRecordView::Dump(dc);
}
CPasswordDoc* CPasswordView::GetDocument () // non-debug version is inline
{
ASSERT(m_pDocument->IsKind Of(RUNTIME _CLASS(CPa sswordDoc) ));
return (CPasswordDoc*)m_pDocument ;
}
#endif //_DEBUG
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordView database support
CRecordset* CPasswordView::OnGetRecord set()
{
return m_pSet;
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// /
// CPasswordView message handlers
void CPasswordView::OnButton1() <<------
{
// TODO: Add your control notification handler code here
//CPasswordApp p; //doesnt work with this class
PWVu p; //accessing variables from other classes works
//but the problem is accessing the assigned value
if (p.pw=="gugu") //how do i get access to the password assigned in the
{ //PWVu dialog box
Beep(440,1000);
}
}
if I am sending too much, and I won't do it again.
1. CPasswordApp
2. The Dialog PWVu
3. CPasswordView
>>>>>>>>>>>> 1. CPasswordApp
// password.h : main header file for the PASSWORD application
//
#if !defined(AFX_PASSWORD_H__0
#define AFX_PASSWORD_H__0688FC2B_9
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
#include "Password_i.h"
//////////////////////////
// CPasswordApp:
// See password.cpp for the implementation of this class
//
class CPasswordApp : public CWinApp
{
public:
char thepw(); // <<--
CString apw;
virtual BOOL PreTranslateMessage(MSG* pMsg);
CPasswordApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPasswordA
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
// Implementation
COleTemplateServer m_server;
// Server object for document creation
//{{AFX_MSG(CPasswordApp)
afx_msg void OnAppAbout();
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
BOOL m_bATLInited;
private:
BOOL InitATL();
};
//////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PASSWORD_H__0
>>>>>
// password.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "password.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "IpFrame.h"
#include "passwordSet.h"
#include "passwordDoc.h"
#include "passwordView.h"
#include <initguid.h>
#include "Password_i.c"
#include "PWVu.h"
#include "Splash.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////
// CPasswordApp
BEGIN_MESSAGE_MAP(CPasswor
//{{AFX_MSG_MAP(CPasswordA
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_S
END_MESSAGE_MAP()
//////////////////////////
// CPasswordApp construction
CPasswordApp::CPasswordApp
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
//////////////////////////
// The one and only CPasswordApp object
CPasswordApp theApp;
// This identifier was generated to be statistically unique for your app.
// You may change it if you prefer to choose a specific identifier.
// {0688FC26-978D-11D3-8527-0
static const CLSID clsid =
{ 0x688fc26, 0x978d, 0x11d3, { 0x85, 0x27, 0x0, 0x60, 0x67, 0x4b, 0x8f, 0xc6 } };
//////////////////////////
// CPasswordApp initialization
BOOL CPasswordApp::InitInstance
{
// CG: The following block was added by the Splash Screen component.
{
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
CSplashWnd::EnableSplashSc
}
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT
return FALSE;
}
if (!InitATL())
return FALSE;
AfxEnableControlContainer(
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
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.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_PASSWOTYPE,
RUNTIME_CLASS(CPasswordDoc
RUNTIME_CLASS(CChildFrame)
RUNTIME_CLASS(CPasswordVie
pDocTemplate->SetContainer
pDocTemplate->SetServerInf
IDR_PASSWOTYPE_SRVR_EMB, IDR_PASSWOTYPE_SRVR_IP,
RUNTIME_CLASS(CInPlaceFram
AddDocTemplate(pDocTemplat
// Connect the COleTemplateServer to the document template.
// The COleTemplateServer creates new documents on behalf
// of requesting OLE containers by using information
// specified in the document template.
m_server.ConnectTemplate(c
// Register all OLE server factories as running. This enables the
// OLE libraries to create objects from other applications.
COleTemplateServer::Regist
_Module.RegisterClassObjec
REGCLS_MULTIPLEUSE);
// Note: MDI applications register all server objects without regard
// to the /Embedding or /Automation on the command line.
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(ID
return FALSE;
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Check to see if launched as OLE server
if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
{
// Application was run with /Embedding or /Automation. Don't show the
// main window in this case.
return TRUE;
}
// When a server application is launched stand-alone, it is a good idea
// to update the system registry in case it has been damaged.
m_server.UpdateRegistry(OA
_Module.UpdateRegistryFrom
_Module.RegisterServer(TRU
COleObjectFactory::UpdateR
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdI
return FALSE;
// The main window has been initialized, so show and update it.
// PWVu getpw;
// getpw.DoModal();
// pw=getpw.m_pw;
thepw(); <<----Password retrieved from dialog, after splash screen, but before Passwordview.
pMainFrame->ShowWindow(m_n
pMainFrame->UpdateWindow()
return TRUE;
}
//////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchan
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDl
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(
{
CDialog::DoDataExchange(pD
//{{AFX_DATA_MAP(CAboutDlg
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDl
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CPasswordApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
//////////////////////////
// CPasswordApp message handlers
CPasswordModule _Module;
BEGIN_OBJECT_MAP(ObjectMap
END_OBJECT_MAP()
LONG CPasswordModule::Unlock()
{
AfxOleUnlockApp();
return 0;
}
LONG CPasswordModule::Lock()
{
AfxOleLockApp();
return 1;
}
int CPasswordApp::ExitInstance
{
if (m_bATLInited)
{
_Module.RevokeClassObjects
_Module.Term();
}
return CWinApp::ExitInstance();
}
BOOL CPasswordApp::InitATL()
{
m_bATLInited = TRUE;
_Module.Init(ObjectMap, AfxGetInstanceHandle());
_Module.dwThreadID = GetCurrentThreadId();
return TRUE;
}
BOOL CPasswordApp::PreTranslate
{
// CG: The following lines were added by the Splash Screen component.
if (CSplashWnd::PreTranslateA
return TRUE;
return CWinApp::PreTranslateMessa
}
char CPasswordApp::thepw() <<----
{
PWVu getpw;
getpw.DoModal();
// apw=getpw.m_pw;
getpw.pw=getpw.m_pw;
return 0;
}
>>>>>>> 2. THE DIALOG
#if !defined(AFX_PWVU_H__0688F
#define AFX_PWVU_H__0688FC4B_978D_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// PWVu.h : header file
//
//////////////////////////
// PWVu dialog
class PWVu : public CDialog
{
// Construction
public:
PWVu(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(PWVu)
enum { IDD = IDD_FORMVIEW };
CString m_pw; <<-----
//}}AFX_DATA
CString pw; <<----
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(PWVu)
protected:
virtual void DoDataExchange(CDataExchan
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(PWVu)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PWVU_H__0688F
>>>>>>>
// PWVu.cpp : implementation file
//
#include "stdafx.h"
#include "password.h"
#include "PWVu.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////
// PWVu dialog
PWVu::PWVu(CWnd* pParent /*=NULL*/)
: CDialog(PWVu::IDD, pParent)
{
//{{AFX_DATA_INIT(PWVu)
m_pw = _T("");
//}}AFX_DATA_INIT
}
void PWVu::DoDataExchange(CData
{
CDialog::DoDataExchange(pD
//{{AFX_DATA_MAP(PWVu)
DDX_Text(pDX, IDC_EDIT1, m_pw);
//}}AFX_DATA_MAP
pw=m_pw;
}
BEGIN_MESSAGE_MAP(PWVu, CDialog)
//{{AFX_MSG_MAP(PWVu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////
// PWVu message handlers
>>>>>> 3. THE VIEW AFTER THE DIALOG, FROM WHICH I WANT TO RETRIEVE THE VALUE OF THE PASSWORD
// passwordView.h : interface of the CPasswordView class
//
//////////////////////////
//{{AFX_INCLUDES()
#include "crystalctrl.h"
//}}AFX_INCLUDES
#if !defined(AFX_PASSWORDVIEW_
#define AFX_PASSWORDVIEW_H__0688FC
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CPasswordCntrItem;
class CPasswordSet;
class CPasswordView : public CRecordView
{
protected: // create from serialization only
CPasswordView();
DECLARE_DYNCREATE(CPasswor
public:
//{{AFX_DATA(CPasswordView
enum { IDD = IDD_PASSWORD_FORM };
CPasswordSet* m_pSet;
CCrystalCtrl m_cryst;
//}}AFX_DATA
// Attributes
public:
CPasswordDoc* GetDocument();
// m_pSelection holds the selection to the current CPasswordCntrItem.
// For many applications, such a member variable isn't adequate to
// represent a selection, such as a multiple selection or a selection
// of objects that are not CPasswordCntrItem objects. This selection
// mechanism is provided just to help you get started.
// TODO: replace this selection mechanism with one appropriate to your app.
CPasswordCntrItem* m_pSelection;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPasswordV
public:
virtual CRecordset* OnGetRecordset();
virtual BOOL PreCreateWindow(CREATESTRU
protected:
virtual void DoDataExchange(CDataExchan
virtual void OnInitialUpdate(); // called first time after construct
virtual BOOL OnPreparePrinting(CPrintIn
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual BOOL IsSelected(const CObject* pDocItem) const;// Container support
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CPasswordView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CPasswordView)
afx_msg void OnDestroy();
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnInsertObject();
afx_msg void OnCancelEditCntr();
afx_msg void OnCancelEditSrvr();
afx_msg void OnButton1();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in passwordView.cpp
inline CPasswordDoc* CPasswordView::GetDocument
{ return (CPasswordDoc*)m_pDocument
#endif
//////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_PASSWORDVIEW_
>>>>>
// passwordView.cpp : implementation of the CPasswordView class
//
#include "stdafx.h"
#include "password.h"
#include "passwordSet.h"
#include "passwordDoc.h"
#include "CntrItem.h"
#include "passwordView.h"
#include "PWVu.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////
// CPasswordView
IMPLEMENT_DYNCREATE(CPassw
BEGIN_MESSAGE_MAP(CPasswor
//{{AFX_MSG_MAP(CPasswordV
ON_WM_DESTROY()
ON_WM_SETFOCUS()
ON_WM_SIZE()
ON_COMMAND(ID_OLE_INSERT_N
ON_COMMAND(ID_CANCEL_EDIT_
ON_COMMAND(ID_CANCEL_EDIT_
ON_BN_CLICKED(IDC_BUTTON1,
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_D
ON_COMMAND(ID_FILE_PRINT_P
END_MESSAGE_MAP()
//////////////////////////
// CPasswordView construction/destruction
CPasswordView::CPasswordVi
: CRecordView(CPasswordView:
{
//{{AFX_DATA_INIT(CPasswor
m_pSet = NULL;
//}}AFX_DATA_INIT
m_pSelection = NULL;
// TODO: add construction code here
}
CPasswordView::~CPasswordV
{
}
void CPasswordView::DoDataExcha
{
CRecordView::DoDataExchang
//{{AFX_DATA_MAP(CPassword
DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_CUSTNAME, m_pSet);
DDX_Control(pDX, IDC_CRYSTALREPORT1, m_cryst);
//}}AFX_DATA_MAP
}
BOOL CPasswordView::PreCreateWi
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWind
}
void CPasswordView::OnInitialUp
{
m_pSet = &GetDocument()->m_password
CRecordView::OnInitialUpda
ResizeParentToFit();
// TODO: remove this code when final selection model code is written
m_pSelection = NULL; // initialize selection
}
//////////////////////////
// CPasswordView printing
BOOL CPasswordView::OnPreparePr
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPasswordView::OnBeginPrin
{
// TODO: add extra initialization before printing
}
void CPasswordView::OnEndPrinti
{
// TODO: add cleanup after printing
}
void CPasswordView::OnDestroy()
{
// Deactivate the item on destruction; this is important
// when a splitter view is being used.
CRecordView::OnDestroy();
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA
if (pActiveItem != NULL && pActiveItem->GetActiveView
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetI
}
}
//////////////////////////
// OLE Client support and commands
BOOL CPasswordView::IsSelected(
{
// The implementation below is adequate if your selection consists of
// only CPasswordCntrItem objects. To handle different selection
// mechanisms, the implementation here should be replaced.
// TODO: implement this function that tests for a selected OLE client item
return pDocItem == m_pSelection;
}
void CPasswordView::OnInsertObj
{
// Invoke the standard Insert Object dialog box to obtain information
// for new CPasswordCntrItem object.
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return;
BeginWaitCursor();
CPasswordCntrItem* pItem = NULL;
TRY
{
// Create new item connected to this document.
CPasswordDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new CPasswordCntrItem(pDoc);
ASSERT_VALID(pItem);
// Initialize the item from the dialog data.
if (!dlg.CreateItem(pItem))
AfxThrowMemoryException();
ASSERT_VALID(pItem);
if (dlg.GetSelectionType() == COleInsertDialog::createNe
pItem->DoVerb(OLEIVERB_SHO
ASSERT_VALID(pItem);
// As an arbitrary user interface design, this sets the selection
// to the last item inserted.
// TODO: reimplement selection as appropriate for your application
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL)
}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_T
}
END_CATCH
EndWaitCursor();
}
// The following command handler provides the standard keyboard
// user interface to cancel an in-place editing session. Here,
// the container (not the server) causes the deactivation.
void CPasswordView::OnCancelEdi
{
// Close any in-place active item on this view.
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA
if (pActiveItem != NULL)
{
pActiveItem->Close();
}
ASSERT(GetDocument()->GetI
}
// Special handling of OnSetFocus and OnSize are required for a container
// when an object is being edited in-place.
void CPasswordView::OnSetFocus(
{
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA
if (pActiveItem != NULL &&
pActiveItem->GetItemState(
{
// need to set focus to this item if it is in the same view
CWnd* pWnd = pActiveItem->GetInPlaceWin
if (pWnd != NULL)
{
pWnd->SetFocus(); // don't call the base class
return;
}
}
CRecordView::OnSetFocus(pO
}
void CPasswordView::OnSize(UINT
{
CRecordView::OnSize(nType,
COleClientItem* pActiveItem = GetDocument()->GetInPlaceA
if (pActiveItem != NULL)
pActiveItem->SetItemRects(
}
//////////////////////////
// OLE Server support
// The following command handler provides the standard keyboard
// user interface to cancel an in-place editing session. Here,
// the server (not the container) causes the deactivation.
void CPasswordView::OnCancelEdi
{
GetDocument()->OnDeactivat
}
//////////////////////////
// CPasswordView diagnostics
#ifdef _DEBUG
void CPasswordView::AssertValid
{
CRecordView::AssertValid()
}
void CPasswordView::Dump(CDumpC
{
CRecordView::Dump(dc);
}
CPasswordDoc* CPasswordView::GetDocument
{
ASSERT(m_pDocument->IsKind
return (CPasswordDoc*)m_pDocument
}
#endif //_DEBUG
//////////////////////////
// CPasswordView database support
CRecordset* CPasswordView::OnGetRecord
{
return m_pSet;
}
//////////////////////////
// CPasswordView message handlers
void CPasswordView::OnButton1()
{
// TODO: Add your control notification handler code here
//CPasswordApp p; //doesnt work with this class
PWVu p; //accessing variables from other classes works
//but the problem is accessing the assigned value
if (p.pw=="gugu") //how do i get access to the password assigned in the
{ //PWVu dialog box
Beep(440,1000);
}
}
>> let me know if I am sending too much
Well, if you have to ask, you probably are. :-)
In the future, you can always e-mail the code if it is too long.
I'm not sure where you "see" the problem. I see that there is a "Apw" string in the app class, i.e.
CString apw;
But that string is never set to a value. You have the commented out in
char CPasswordApp::thepw() <<----
{
PWVu getpw;
getpw.DoModal();
// apw=getpw.m_pw;
getpw.pw=getpw.m_pw;
return 0;
}
You do set getpw's pw string, but I don't know why. That does not seem to be useful, as getpw is a local and is destroyed immediately afterwards, so there doesn't seem to be any opportunity to use the string.
Can you explain this a bit?
Well, if you have to ask, you probably are. :-)
In the future, you can always e-mail the code if it is too long.
I'm not sure where you "see" the problem. I see that there is a "Apw" string in the app class, i.e.
CString apw;
But that string is never set to a value. You have the commented out in
char CPasswordApp::thepw() <<----
{
PWVu getpw;
getpw.DoModal();
// apw=getpw.m_pw;
getpw.pw=getpw.m_pw;
return 0;
}
You do set getpw's pw string, but I don't know why. That does not seem to be useful, as getpw is a local and is destroyed immediately afterwards, so there doesn't seem to be any opportunity to use the string.
Can you explain this a bit?
ASKER
What I don't seem to know how to do is to get the value "gugu" given to m_pw (in PWVu), when I'm in another view (PasswordView). I tried setting apw to m_pw but once in PasswordView the value "gugu" is gone. I also tried to pass the value to a global variable but this gives me other problems.
I'm not following.
The PWVu is temporary, so the password stored in side its m_pw is temporary too.
How is this password supposed to exist?
One password that is used throughout the program?
One password that is per "something", like one password per view?
What is the "apw" stored in CPasswordApp, does it have any relationship with this?
The PWVu is temporary, so the password stored in side its m_pw is temporary too.
How is this password supposed to exist?
One password that is used throughout the program?
One password that is per "something", like one password per view?
What is the "apw" stored in CPasswordApp, does it have any relationship with this?
ASKER
One password that is used throughout the program. m_pw is destroyed when the dialog is, how do I get to use the value of m_pw, before it disappears, throughout the program, in subsequent views. How do I pass the value of m_pw on to other views.
Why have the password "in" a view. You've got many views and only one passsword, so you would have to have many copies of the password and you would have to take steps to insure the copies stay in synch. You have 3 options.
1. Store the password inside the application object (CPasswordApp) as there will be one instance (object) of this class
2. Store the password in a global variable.
3. Store the password in a static member of the view class. As a static member, there would be only one copy of this data that is shared between all instances (objects) of the class. (Basically a static member is a global variable, but its within the class's namespace so code outside of the class can't access it without specifying the class name and also can't access it if it is private.
Does that help?
1. Store the password inside the application object (CPasswordApp) as there will be one instance (object) of this class
2. Store the password in a global variable.
3. Store the password in a static member of the view class. As a static member, there would be only one copy of this data that is shared between all instances (objects) of the class. (Basically a static member is a global variable, but its within the class's namespace so code outside of the class can't access it without specifying the class name and also can't access it if it is private.
Does that help?
ASKER
Option 1) is what I tried in first. If I do the following in CPasswordApp:
char CPasswordApp::thepw()
{
PWVu getpw;
getpw.DoModal();
theApp.apw=getpw.m_pw;
getpw.pw=getpw.m_pw;
return 0;
}
apw has "gugu" assigned to it even after
the dialog box goes and passwordView comes up. But when i modify button1 in this view to get the theApp.apw it doesn't work:
void CPasswordView::OnButton1()
{
if (theApp.apw=="gugu")
{
Beep(440,1000);
}
}
the error I get is that theApp is an undeclared identifier.
char CPasswordApp::thepw()
{
PWVu getpw;
getpw.DoModal();
theApp.apw=getpw.m_pw;
getpw.pw=getpw.m_pw;
return 0;
}
apw has "gugu" assigned to it even after
the dialog box goes and passwordView comes up. But when i modify button1 in this view to get the theApp.apw it doesn't work:
void CPasswordView::OnButton1()
{
if (theApp.apw=="gugu")
{
Beep(440,1000);
}
}
the error I get is that theApp is an undeclared identifier.
>> apw has "gugu" assigned to it even after
>> the dialog box goes
What is in getpw.m_pw?
>> the error I get is that theApp
"theApp" is declated inside the main .cpp file, but it is not declared inside a .h file, so it is not "vissible" to other .cpp files. You could simply add a line like
extern CPasswordApp theApp;
to the file that needs theApp, or add it to a .h file. However, I beleive MFC provides a procedure for finding the app object, but I'm not sure what.
>> the dialog box goes
What is in getpw.m_pw?
>> the error I get is that theApp
"theApp" is declated inside the main .cpp file, but it is not declared inside a .h file, so it is not "vissible" to other .cpp files. You could simply add a line like
extern CPasswordApp theApp;
to the file that needs theApp, or add it to a .h file. However, I beleive MFC provides a procedure for finding the app object, but I'm not sure what.
The function is AfxGetApp(). That will return apointer to the app object.
ASKER
this works:
extern CPasswordApp theApp;
if (theApp.apw=="gugu")
{
Beep(440,1000);
}
this works too:
//((CPasswordApp*) AfxGetApp ())->apw
if (((CPasswordApp*) AfxGetApp ())->apw=="gugu")
{
Beep(440,1000);
}
Thanks.
extern CPasswordApp theApp;
if (theApp.apw=="gugu")
{
Beep(440,1000);
}
this works too:
//((CPasswordApp*) AfxGetApp ())->apw
if (((CPasswordApp*) AfxGetApp ())->apw=="gugu")
{
Beep(440,1000);
}
Thanks.
class Cls
{
private:
string m_pw;
public;
string GetPw() const { return m_pw; };
SetPw(string s) {m_pw = s; };
};
int main()
{
Cls A;
string s;
cin >> s;
A.SetPw(s);
return 0;
};