Click here to Skip to main content
15,896,207 members
Articles / Desktop Programming / MFC

Shared memory

Rate me:
Please Sign up or sign in to vote.
4.80/5 (6 votes)
28 Sep 20028 min read 224.2K   6K   84  
Share variables in the shared memory across processes
#include "stdafx.h"
#include "testsm.h"
#include "Helpers.h"
#include "WaitMultiVarchDlg.h"

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


CWaitMultiVarchDlg::CWaitMultiVarchDlg(CWnd* pParent)
: CDialog(CWaitMultiVarchDlg::IDD, pParent)
{
	m_psm=NULL;
}



CWaitMultiVarchDlg::CWaitMultiVarchDlg(CSharedMemory *pSharedMemory, CWnd* pParent)
: m_psm(pSharedMemory),
CDialog(CWaitMultiVarchDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWaitMultiVarchDlg)
	m_bWaitForAll = FALSE;
	m_bSelectAll = FALSE;
	//}}AFX_DATA_INIT
}

void CWaitMultiVarchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWaitMultiVarchDlg)
	DDX_Control(pDX, IDC_SELECT_ALL, m_ctrlSelectAll);
	DDX_Control(pDX, IDC_MULTIVAR_LIST, m_ctrMultiVarList);
	DDX_Check(pDX, IDC_WAIT_FOR_ALL, m_bWaitForAll);
	DDX_Check(pDX, IDC_SELECT_ALL, m_bSelectAll);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWaitMultiVarchDlg, CDialog)
	//{{AFX_MSG_MAP(CWaitMultiVarchDlg)
	ON_BN_CLICKED(IDC_SELECT_ALL, OnSelectAll)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWaitMultiVarchDlg message handlers

BOOL CWaitMultiVarchDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_ctrlSelectAll.SetCheck(0);

	InitHeader();
	InitList();
	return TRUE;              
}


void CWaitMultiVarchDlg::InitHeader()
{
	m_ctrMultiVarList.DeleteAllItems();
	//Column Headers
	//		 	
	/*VERIFY(m_ctrMultiVarList.m_cImageList.Create(IDB_CHECKBOXES, 16, 3, RGB(255, 0, 255)));
	m_ctrMultiVarList.m_HeaderCtrl.SetImageList(&m_ctrMultiVarList.m_cImageList);

	HDITEM hditem;

	for (int i = 0; i < m_ctrMultiVarList.m_HeaderCtrl.GetItemCount(); i++)
	{
		hditem.mask = HDI_IMAGE | HDI_FORMAT;
		m_ctrMultiVarList.m_HeaderCtrl.GetItem(i, &hditem);
		hditem.fmt |=  HDF_IMAGE;
		if (i == 0 || i == 4)
			hditem.iImage = XHEADERCTRL_UNCHECKED_IMAGE;
		else
			hditem.iImage = XHEADERCTRL_NO_IMAGE;

		m_ctrMultiVarList.m_HeaderCtrl.SetItem(i, &hditem);
	}
*/
	CRect rect;
	m_ctrMultiVarList.GetWindowRect(&rect);
	m_ctrMultiVarList.SetExtendedStyle(LVS_EX_HEADERDRAGDROP|LVS_REPORT|LVS_EX_FULLROWSELECT|LVS_EX_ONECLICKACTIVATE|LVS_EX_CHECKBOXES|LVS_EX_TRACKSELECT);	

	CString strHead; 
	strHead.Format(IDS_VARIABLE);
	m_ctrMultiVarList.InsertColumn(0, strHead, LVCFMT_LEFT,rect.Width(), 0);
	m_ctrMultiVarList.SetHeaderCheckedState(0,1);
	
	m_ctrMultiVarList.EnableToolTips(TRUE);
	
}

void CWaitMultiVarchDlg::InitList()
{
	if(m_psm == NULL)
		return;
	m_ctrMultiVarList.DeleteAllItems(); 

	
	LV_ITEM lvitem;
	ValueHeader varInfo;

	TCHAR szText[128];

	DWORD dwCount = m_psm->GetVariablesCount();

	for (DWORD i = 0; i < dwCount; i++)
	{
		m_psm->GetValueInfo(i,&varInfo);

#ifndef _UNICODE
		DWORD dwNamLn = wcslen(varInfo.wszValueName)+2;
		char *szName = new char[dwNamLn];

		WideCharToMultiByte(CP_ACP,0,varInfo.wszValueName,-1,szName,dwNamLn,NULL,NULL);
#endif
		for(int j = 0; j < 1; j++)
		{	
			lvitem.mask = LVIF_TEXT;
			lvitem.iItem = i;
			lvitem.iSubItem = j;

			if(j == 0)
			{
#ifndef _UNICODE
				lvitem.pszText = szName;
				m_ctrMultiVarList.InsertItem(i,szName);
#else
				lvitem.pszText = varInfo.wszValueName;
				m_ctrMultiVarList.InsertItem(i,varInfo.wszValueName);
#endif				
			}
			if(j == 1)
			{
				_tcscpy(szText,_T("No"));
				lvitem.pszText = szText;
				m_ctrMultiVarList.SetItem(&lvitem);				
			}
			m_ctrMultiVarList.SetItemToolTipText(i,j,lvitem.pszText);
			m_ctrMultiVarList.SetCheckbox(i,j,0);
		}
	}
	
#if defined(_DEBUG) && defined(DUMP_SHARED_MEMORY)
	m_psm->Dump(afxDump);
#endif
	UpdateData(FALSE);
}


void CWaitMultiVarchDlg::OnOK() 
{
	CWaitCursor wait;
	UpdateData(TRUE);
	int nCount = m_ctrMultiVarList.GetItemCount();

	for(int i = 0; i < nCount; i++)
	{
		if(m_ctrMultiVarList.GetCheckbox(i,0) == 1)
			m_strArr.Add(m_ctrMultiVarList.GetItemText(i,0));	
	}

	if (m_strArr.GetSize()) {

		DWORD dwRet = m_psm->WaitForMultipleValuesChanges(m_strArr,m_bWaitForAll);
		if(dwRet == WAIT_FAILED)
		{
			CString str;
			GetErrorDescription(m_psm->GetLastError(),str);
			CString msg = _T("Cannot wait: ");
			msg += str;
			AfxMessageBox(msg);
		}
		else if(dwRet == WAIT_TIMEOUT)
		{
			AfxMessageBox(_T("No Change"));		
		}
		else 
		{
			if (m_bWaitForAll) {
				AfxMessageBox(_T("At least one variable has changed."));
			} else {
				CString str;
				str.Format(_T("%s has been changed or removed"),m_strArr.GetAt(WAIT_OBJECT_0 + dwRet));
				AfxMessageBox(str);	
			}
		}
		CDialog::OnOK();
	} else {
		AfxMessageBox(_T("Select at least one variable please."));
	}
}

void CWaitMultiVarchDlg::OnSelectAll() 
{
	UpdateData(TRUE);

	int nCount = m_ctrMultiVarList.GetItemCount();

	for(int i = 0; i < nCount; i++)
	{
		m_ctrMultiVarList.SetCheckbox(i,0,m_bSelectAll);				
	}
	
	m_ctrlSelectAll.SetWindowText(m_bSelectAll ? _T("Unselect all variables") :  _T("Select all variables"));
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions