Link to home
Start Free TrialLog in
Avatar of binstar
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.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

If the class is protected or private, then you must provide "access functions" that obtain the member's current value or set it to a new value.

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;
};
>> 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?
Avatar of binstar

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?
Avatar of binstar

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__0688FC2B_978D_11D3_8527_0060674B8FC6__INCLUDED_)
#define AFX_PASSWORD_H__0688FC2B_978D_11D3_8527_0060674B8FC6__INCLUDED_

#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(CPasswordApp)
      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__0688FC2B_978D_11D3_8527_0060674B8FC6__INCLUDED_)


>>>>>
// 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(CPasswordApp, CWinApp)
      //{{AFX_MSG_MAP(CPasswordApp)
      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_SETUP, 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-0060674B8FC6}
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::EnableSplashScreen(cmdInfo.m_bShowSplash);
      }

      // 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(CPasswordView));
      pDocTemplate->SetContainerInfo(IDR_PASSWOTYPE_CNTR_IP);
      pDocTemplate->SetServerInfo(
            IDR_PASSWOTYPE_SRVR_EMB, IDR_PASSWOTYPE_SRVR_IP,
            RUNTIME_CLASS(CInPlaceFrame));
      AddDocTemplate(pDocTemplate);

      // 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(clsid, pDocTemplate, FALSE);

      // Register all OLE server factories as running.  This enables the
      //  OLE libraries to create objects from other applications.
      COleTemplateServer::RegisterAll();
      _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
            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(IDR_MAINFRAME))
            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(OAT_INPLACE_SERVER);

    _Module.UpdateRegistryFromResource(IDR_PASSWORD, TRUE);
      _Module.RegisterServer(TRUE);

      COleObjectFactory::UpdateRegistryAll();

      // Dispatch commands specified on the command line
      if (!ProcessShellCommand(cmdInfo))
            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_nCmdShow);
      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(CDataExchange* 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(CAboutDlg)
      //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CAboutDlg)
      //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, 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::PreTranslateMessage(MSG* pMsg)
{
      // CG: The following lines were added by the Splash Screen component.
      if (CSplashWnd::PreTranslateAppMessage(pMsg))
      return TRUE;
      return CWinApp::PreTranslateMessage(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__0688FC4B_978D_11D3_8527_0060674B8FC6__INCLUDED_)
#define AFX_PWVU_H__0688FC4B_978D_11D3_8527_0060674B8FC6__INCLUDED_

#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(CDataExchange* 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__0688FC4B_978D_11D3_8527_0060674B8FC6__INCLUDED_)

>>>>>>> 
// 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(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{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__0688FC36_978D_11D3_8527_0060674B8FC6__INCLUDED_)
#define AFX_PASSWORDVIEW_H__0688FC36_978D_11D3_8527_0060674B8FC6__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(CPasswordView)

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(CPasswordView)
      public:
      virtual CRecordset* OnGetRecordset();
      virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      virtual void OnInitialUpdate(); // called first time after construct
      virtual BOOL OnPreparePrinting(CPrintInfo* 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__0688FC36_978D_11D3_8527_0060674B8FC6__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(CPasswordView, CRecordView)

BEGIN_MESSAGE_MAP(CPasswordView, CRecordView)
      //{{AFX_MSG_MAP(CPasswordView)
      ON_WM_DESTROY()
      ON_WM_SETFOCUS()
      ON_WM_SIZE()
      ON_COMMAND(ID_OLE_INSERT_NEW, 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_DIRECT, CRecordView::OnFilePrint)
      ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPasswordView construction/destruction

CPasswordView::CPasswordView()
      : CRecordView(CPasswordView::IDD)
{
      //{{AFX_DATA_INIT(CPasswordView)
      m_pSet = NULL;
      //}}AFX_DATA_INIT
      m_pSelection = NULL;
      // TODO: add construction code here

}

CPasswordView::~CPasswordView()
{
}

void CPasswordView::DoDataExchange(CDataExchange* pDX)
{
      CRecordView::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CPasswordView)
      DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_CUSTNAME, m_pSet);
      DDX_Control(pDX, IDC_CRYSTALREPORT1, m_cryst);
      //}}AFX_DATA_MAP
}

BOOL CPasswordView::PreCreateWindow(CREATESTRUCT& cs)
{
      // TODO: Modify the Window class or styles here by modifying
      //  the CREATESTRUCT cs

      return CRecordView::PreCreateWindow(cs);
}

void CPasswordView::OnInitialUpdate()
{
      m_pSet = &GetDocument()->m_passwordSet;
      CRecordView::OnInitialUpdate();
      ResizeParentToFit();
      

      // TODO: remove this code when final selection model code is written
      m_pSelection = NULL;    // initialize selection

}

/////////////////////////////////////////////////////////////////////////////
// CPasswordView printing

BOOL CPasswordView::OnPreparePrinting(CPrintInfo* pInfo)
{
      // default preparation
      return DoPreparePrinting(pInfo);
}

void CPasswordView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
      // TODO: add extra initialization before printing
}

void CPasswordView::OnEndPrinting(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()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == 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::OnInsertObject()
{
      // 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::createNewItem)
                  pItem->DoVerb(OLEIVERB_SHOW, 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_TO_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::OnCancelEditCntr()
{
      // Close any in-place active item on this view.
      COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
      if (pActiveItem != NULL)
      {
            pActiveItem->Close();
      }
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == 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()->GetInPlaceActiveItem(this);
      if (pActiveItem != NULL &&
            pActiveItem->GetItemState() == COleClientItem::activeUIState)
      {
            // need to set focus to this item if it is in the same view
            CWnd* pWnd = pActiveItem->GetInPlaceWindow();
            if (pWnd != NULL)
            {
                  pWnd->SetFocus();   // don't call the base class
                  return;
            }
      }

      CRecordView::OnSetFocus(pOldWnd);
}

void CPasswordView::OnSize(UINT nType, int cx, int cy)
{
      CRecordView::OnSize(nType, cx, cy);
      COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(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::OnCancelEditSrvr()
{
      GetDocument()->OnDeactivateUI(FALSE);
}

/////////////////////////////////////////////////////////////////////////////
// CPasswordView diagnostics

#ifdef _DEBUG
void CPasswordView::AssertValid() const
{
      CRecordView::AssertValid();
}

void CPasswordView::Dump(CDumpContext& dc) const
{
      CRecordView::Dump(dc);
}

CPasswordDoc* CPasswordView::GetDocument() // non-debug version is inline
{
      ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPasswordDoc)));
      return (CPasswordDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPasswordView database support
CRecordset* CPasswordView::OnGetRecordset()
{
      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?
Avatar of binstar

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?
Avatar of binstar

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?
Avatar of binstar

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.  
>> 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 function is AfxGetApp().  That will return apointer to the app object.
Avatar of binstar

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.