📄 sharememcdlg.cpp
字号:
// ShareMemCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ShareMemC.h"
#include "ShareMemCDlg.h"
#include "afxmt.h"
#define INVALID_USED_SIGN 255
#define IS_TRUE 1
#define IS_FALSE 255
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//HANDLE m_hReceiveMap = NULL;
//LPSTR m_lpbReceiveBuf = NULL;
typedef struct MemTable
{
int chaid;
char name[50];
char attr[800];
}MemTable,*lpMemTable;;
typedef struct ShareMem
{
int worktype;
int keytype;
char info[100];
unsigned char isUsed;
unsigned char havereturn;
MemTable memtable;
}ShareMem,*lpShareMem;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CShareMemCDlg dialog
CShareMemCDlg::CShareMemCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShareMemCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CShareMemCDlg)
m_csKey = _T("1");
m_iKeyType = 0;
m_iWorkType = 0;
m_csAttr = _T("1111");
m_csName = _T("11");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CShareMemCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShareMemCDlg)
DDX_Text(pDX, IDC_EDIT_ID, m_csKey);
DDX_Text(pDX, IDC_EDIT_KEYTYPE, m_iKeyType);
DDX_Text(pDX, IDC_EDIT_WORKTYPE, m_iWorkType);
DDX_Text(pDX, IDC_EDIT_ATTR, m_csAttr);
DDX_Text(pDX, IDC_EDIT_NAME, m_csName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CShareMemCDlg, CDialog)
//{{AFX_MSG_MAP(CShareMemCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_LOGOUT, OnButtonLogout)
ON_BN_CLICKED(IDC_BUTTON_GET, OnButtonGet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShareMemCDlg message handlers
BOOL CShareMemCDlg::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_hDataLock = ::CreateMutex(NULL, TRUE, "ShareMem Data Mutex");
if((m_hFileMapping=OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE,TRUE,"DataMap"))==NULL)
{
m_hFileMapping=CreateFileMapping((HANDLE)0XFFFFFFFF,NULL,PAGE_READWRITE,0,sizeof(ShareMem),"DataMap");
}
m_pSharedBlock = (char*)::MapViewOfFile(
m_hFileMapping, // File mapping object
FILE_MAP_ALL_ACCESS, // Read/Write
0, // Offset - high 32 bits
0, // Offset - low 32 bits
sizeof(ShareMem)); // Map the whole thing
::ReleaseMutex(m_hDataLock);
return TRUE; // return TRUE unless you set the focus to a control
}
void CShareMemCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CShareMemCDlg::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 CShareMemCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CShareMemCDlg::DestroyWindow()
{
if(m_pSharedBlock)
::UnmapViewOfFile(m_pSharedBlock);
::CloseHandle(m_hFileMapping) ;
::CloseHandle(m_hDataLock) ;
return CDialog::DestroyWindow();
}
int CShareMemCDlg::SetMem()
{
GetDlgItem(IDC_BUTTON_FIND)->EnableWindow(FALSE);
::WaitForSingleObject(m_hDataLock, INFINITE);
if(!m_pSharedBlock)
{
::ReleaseMutex(m_hDataLock);
return -1;
}
int tmpid = atoi(m_csKey.GetBuffer(m_csKey.GetLength()));
ShareMem tmpsmem;
tmpsmem.memtable.chaid = tmpid;
tmpsmem.worktype = m_iWorkType;
tmpsmem.keytype = m_iKeyType;
char * tmpname = m_csName.GetBuffer(m_csName.GetLength());
char * tmpattr = m_csAttr.GetBuffer(m_csAttr.GetLength());
memset(tmpsmem.memtable.name,0,50);
memset(tmpsmem.memtable.attr,0,800);
memset(tmpsmem.info,0,100);
strcpy(tmpsmem.memtable.name,tmpname);
strcpy(tmpsmem.memtable.attr,tmpattr);
tmpsmem.isUsed = IS_TRUE;
tmpsmem.havereturn = IS_FALSE;
memcpy(m_pSharedBlock,&tmpsmem,sizeof(ShareMem));
::ReleaseMutex(m_hDataLock);
GetDlgItem(IDC_BUTTON_GET)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_INFO)->SetWindowText("操作结束,请接收数据!");
return 0;
}
int CShareMemCDlg::GetMem()
{
GetDlgItem(IDC_BUTTON_GET)->EnableWindow(FALSE);
::WaitForSingleObject(m_hDataLock, INFINITE);
if(!m_pSharedBlock)
{
::ReleaseMutex(m_hDataLock);
return -1;
}
lpShareMem lpReceive =new ShareMem;
memcpy(lpReceive,m_pSharedBlock,sizeof(ShareMem));
if(lpReceive && lpReceive->havereturn == IS_TRUE)
{
CString tmpinfo;
tmpinfo.Format("从服务端返回的信息:%s. 取得的数据为:ID=%d,name=%s,attr=%s.",lpReceive->info,\
lpReceive->memtable.chaid,lpReceive->memtable.name,lpReceive->memtable.attr);
TRACE(tmpinfo);
lpReceive->isUsed= IS_FALSE;
lpReceive->havereturn =IS_FALSE;
GetDlgItem(IDC_EDIT_INFO)->SetWindowText(tmpinfo);
m_csName.Format("%s",lpReceive->memtable.name);
m_csAttr.Format("%s",lpReceive->memtable.attr);
GetDlgItem(IDC_EDIT_NAME)->SetWindowText(m_csName);
GetDlgItem(IDC_EDIT_ATTR)->SetWindowText(m_csAttr);
}
delete lpReceive;
lpReceive = NULL;
::ReleaseMutex(m_hDataLock);
GetDlgItem(IDC_BUTTON_FIND)->EnableWindow(TRUE);
return 0;
}
void CShareMemCDlg::OnButtonFind()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_csKey == "")
{
AfxMessageBox("ID不能为空!");
return;
}
m_csName = "";
m_csAttr = "";
UpdateData(FALSE);
m_iWorkType = WORK_SELECT;
m_iKeyType = KEY_INT;
SetMem();
}
void CShareMemCDlg::OnButtonUpdate()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_csKey == "" || m_csName == "" || m_csAttr == "")
{
AfxMessageBox("ID,Name,Attr都不能为空!");
return;
}
m_iWorkType = WORK_UPDATE;
m_iKeyType = KEY_INT;
SetMem();
}
void CShareMemCDlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_csKey == "" || m_csName == "" || m_csAttr == "")
{
AfxMessageBox("ID,Name,Attr都不能为空!");
return;
}
m_iWorkType = WORK_INSERT;
m_iKeyType = KEY_INT;
SetMem();
}
void CShareMemCDlg::OnButtonLogout()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_csKey == "" || m_csName == "" || m_csAttr == "")
{
AfxMessageBox("ID,Name,Attr都不能为空!");
return;
}
m_iWorkType = WORK_DELETE;
m_iKeyType = KEY_INT;
SetMem();
}
void CShareMemCDlg::OnButtonGet()
{
// TODO: Add your control notification handler code here
GetMem();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -