Memory ing for inter-process communication

Source: Internet
Author: User

The example uses memory ing to load the BMP bitmap from the server to the memory, and the client reads and displays it from the memory.

// Part of the server code void cserverdlg: oncreatefilemap () {// todo: add your control notification handler code herecbutton * pbutton = (cbutton *) getdlgitem (idc_button1 ); pbutton-> enablewindow (false); cstring filename = _ T ("image \ 3.bmp"); // 1. load the file m_hfile = createfile (filename, generic_read, file_share_read, null, open_existing, file_attribute_normal, null); If (m_hfile = invalid_handle_value) {MessageBox (_ T ("file loading failed !!! "), Null, mb_ OK); return;} m_hmap = createfilemapping (m_hfile, null, page_readonly, null, null, _ T (" myjpg "); // 2. map the entire file to m_pbasemapfile = mapviewoffile (m_hmap, file_map_read, 0, 0);} void cserverdlg: onclosefilemap () {// todo: add your control notification handler code herecbutton * pbutton = (cbutton *) getdlgitem (idc_button2); pbutton-> enablewindow (false); If (m_pbasemapfile) {unmapviewoffile (partial );} if (m_hmap) {closehandle (m_hmap) ;}if (m_hfile) {closehandle (m_hfile );}}

// Some client code void cclientview: onopenfilemap () {// todo: add your command handler code herem_hmapfile = openfilemapping (file_map_read, false, _ T ("myjpg ")); if (m_hmapfile = NULL) {MessageBox (_ T ("failed to open file ing! "); Return;} response = mapviewoffile (m_hmapfile, file_map_read, 0); setbitstobmp (byte *) m_pbasemapfile);} void cclientview: onclosefilemap () {// todo: add your command handler code hereif (m_pbasemapfile) {unmapviewoffile (m_pbasemapfile);} If (m_hmapfile) {closehandle (m_hmapfile);} void cclientview :: setbitstobmp (lpbyte lpbase) {bitmapfileheader * pfileheader; // bitmap file-headerbitmapinfo * pin Foheader; // bitmap Info-headerpfileheader = (bitmapfileheader *) lpbase; If (pfileheader-> bftype! = Makeword ('B', 'M') {MessageBox (_ T ("this program only reads BMP files! ");: Unmapviewoffile (lpbase);: closehandle (m_hmapfile); return;} byte * pbits = (byte *) lpbase + pfileheader-> bfoffbits; pinfoheader = (bitmapinfo *) (byte *) lpbase + sizeof (bitmapfileheader); m_nheight = pinfoheader-> bmiheader. biheight; m_nwidth = pinfoheader-> bmiheader. biwidth; cclientdc DC (this); // create an uninitialized bitmap compatible with the specified DC. Select m_hmemdc = createcompatibledc (DC) in the memory compatible DC; hbitmap = :: createcompatiblebitmap (DC, m_nwidth, m_nheight);: SelectObject (m_hmemdc, hbitmap); // 1. normally display setdibitstodevice (m_hmemdc, 0, 0, m_nwidth, m_nheight, 0, m_nheight, pbits, pinfoheader, dib_rgb_colors); // 2. zoom mode/* rect RC; getclientrect (& rc); int nwidth = RC. right-RC. left; int nheight = RC. bottom-RC. top; stretchdibits (m_hmemdc, 0, 0, nwidth, nheight, 0, 0, m_nwidth, m_nheight, pbits, pinfoheader, dib_rgb_colors, srccopy); */invalidaterect (null, true );: deleteobject (hbitmap );}

void CClientView::OnDraw(CDC* pDC){CClientDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereBitBlt(pDC->m_hDC,0, 0, m_nWidth, m_nHeight, m_hMemDC,0,0,SRCCOPY);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.