// ShareMemDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ShareMem.h"
#include "ShareMemDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CShareMemDlg dialog
CShareMemDlg::CShareMemDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShareMemDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CShareMemDlg)
m_strServer = _T("在此输入的内容将被拷贝到内存共享区");
m_strClient = _T("在此显示共享内存去的内容");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CShareMemDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShareMemDlg)
DDX_Control(pDX, IDC_BUTTON_SETCUSTOM, m_setcustom);
DDX_Control(pDX, IDC_BUTTON_DEFAULT, m_default);
DDX_Control(pDX, IDC_BUTTON_CUSTOM, m_custom);
DDX_Text(pDX, IDC_EDIT_SERVER, m_strServer);
DDX_Text(pDX, IDC_EDIT_CLIENT, m_strClient);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CShareMemDlg, CDialog)
//{{AFX_MSG_MAP(CShareMemDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_DEFAULT, OnButtonDefault)
ON_EN_CHANGE(IDC_EDIT_SERVER, OnChangeEditServer)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON_SETCUSTOM, OnButtonSetcustom)
ON_BN_CLICKED(IDC_BUTTON_CUSTOM, OnButtonCustom)
ON_BN_CLICKED(IDC_BUTTON_RUNAG, OnButtonRunag)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShareMemDlg message handlers
BOOL CShareMemDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_custom.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CShareMemDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CShareMemDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CShareMemDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CShareMemDlg::OnChangeEditServer()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData();
//更新共享内存块数据
char *p = (char*)m_SFMS.GetBuffer();
if (p) strcpy(p, LPCTSTR(m_strServer));
}
void CShareMemDlg::OnButtonDefault()
{
// TODO: Add your control notification handler code here
//获取共享内存块数据
CSFMClient SFMC;
char *p = (char*)SFMC.GetBuffer();
if (p) m_strClient = p;
UpdateData(FALSE);
}
void CShareMemDlg::OnCancel()
{
// TODO: Add extra cleanup here
// CDialog::OnCancel();
}
void CShareMemDlg::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
}
void CShareMemDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
CDialog::OnOK();
}
void CShareMemDlg::OnButtonSetcustom()
{
// TODO: Add your control notification handler code here
m_strServer = "";
m_strClient = "";
UpdateData(FALSE);
m_SFMS.Create(NULL, "_ZZZ_OBJ_", 1);
m_custom.EnableWindow(TRUE);
m_default.EnableWindow(FALSE);
m_setcustom.EnableWindow(FALSE);
}
void CShareMemDlg::OnButtonCustom()
{
// TODO: Add your control notification handler code here
CSFMClient SFMC;
SFMC.Open(FILE_MAP_READ|FILE_MAP_WRITE, "_ZZZ_OBJ_");
char *p = (char*)SFMC.GetBuffer();
if (p) m_strClient = p;
UpdateData(FALSE);
}
void CShareMemDlg::OnButtonRunag()
{
// TODO: Add your control notification handler code here
CString strName;
::GetModuleFileName(NULL, strName.GetBuffer(256), 256);
//strName.ReleaseBuffer(-1);
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi = {NULL, NULL, 0, 0};
::CreateProcess(
strName,
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi
);
}
void CShareMemDlg::OnButton1()
{
// TODO: Add your control notification handler code here
//CSFMServer *pCSFMS = new CSFMServer("c:\\obj_zzz.mem", "_OBJ_ZZZ_", 0);
CSFMServer *pCSFMS = new CSFMServer("c:\\obj_zzz.mem", "_OBJ_ZZZ_", 1);
char *p = (char*)pCSFMS->GetBuffer();
if (p)
{
strcpy(p, "1234567890");
::AfxMessageBox(p);
}
//delete pCSFMS;//不要释放
}
void CShareMemDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CSFMClient *pCSFMC = new CSFMClient(FILE_MAP_READ, "_OBJ_ZZZ_");
char *p = (char*)pCSFMC->GetBuffer();
if (p) strcpy(p, "1234567890");
delete pCSFMC;//释放
}