How to communicate between processes in Windows using VC ++ (shared memory)

Source: Internet
Author: User

In an indirect way, you can apply for a shared memory block with a "shared name" for read/write:

Handle getshare (char * & sharep, int size, char * sharename)
{Sharep ApplicationOfMemory block address, size bytes, sharename shared name
Handle FH = createfilemapping (handle)-1, 0,
Page_readwrite, 0,
Size,
Sharename );
Sharep = (char *) mapviewoffile (FH,
File_map_all_access,
0, 0 );
If (getlasterror ()! = Error_already_exists)
Zeromemory (sharep, size); // share area Initialization
Return (FH );
}

Char * sharep = NULL;
Void test () // request a piece of 128 bytesOfCharacter array
{
Handle FH = getshare (sharep, 128, "formyprog ");
For (INT I = 0; I <128; I ++)
Sharep [I] = I;
Closehandle (FH );
}
Handle getshare (char * & sharep, int size, char * sharename)
{Sharep ApplicationOfMemory block address, size bytes, sharename shared name
Handle FH = createfilemapping (handle)-1, 0,
Page_readwrite, 0,
Size,
Sharename );
Sharep = (char *) mapviewoffile (FH,
File_map_all_access,
0, 0 );
If (getlasterror ()! = Error_already_exists)
Zeromemory (sharep, size); // share area Initialization
Return (FH );
}

Char * sharep = NULL;
// Apply for a block of 128 bytesOfCharacter array

Handle FH = getshare (sharep, 128, "formyprog"); file: // apply for an array of 128 byte characters

For (INT I = 0; I <128; I ++) file: // use, test
Sharep [I] = I;
.....................

Closehandle (FH); // release the shared memory handle

 

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.