Using memory-mapped files for EXE, DLL communication (Non-MFC)

Source: Internet
Author: User

Want to do EXE, DLL communication, Online said more is Wm_copydata message, found that need to add a message map more trouble, and the Internet is almost MFC code, want to use the console code.

I thought of the memory-mapped file. EXE sends data to the DLL.

EXE's code:

/**************************************************************************************** file Name: test.cpp* function: EXE and DLL communication using memory-mapped files *************************************************************************************** */#include"stdafx.h"#include<stdlib.h>#include<afxwin.h>int_tmain (intARGC, _tchar*argv[]) {    //1. Create a named file mapHANDLE Hmapfile =createfilemapping (Invalid_handle_value, NULL, Page_readwrite,0, -, L"file_mapping_test"); if(NULL = = Hmapfile | | Invalid_handle_value = =hmapfile) {        returnFALSE; }    //2. Create a ViewPVOID PBuf = MapViewOfFile (Hmapfile, File_map_all_access,0,0, -); if(NULL = =PBuf) {        returnFALSE; }    //3. Copy the shared data to a file mapwcscpy_s (Pwchar) PBuf, -, L"Aheadshooter"); //4. Loading DLLsLoadLibrary (L"Testdll"); //5. Wait for the endGetChar (); //6. Cancel mapping, close handleUnmapViewOfFile (PBUF);    CloseHandle (Hmapfile); return 0;}

DLL code:

/**************************************************************************************** file Name: dllmain.cpp* function: EXE and DLL communication using memory-mapped files *************************************************************************************** */#include"stdafx.h"#include<stdio.h>BOOL apientry DllMain (hmodule hmodule, DWORD Ul_reason_for_call, LPVOID lpreserved) {Switch(ul_reason_for_call) { CaseDll_process_attach: {//1. Open File MappingHANDLE hmapfile = openfilemapping (file_map_all_access, FALSE, L"file_mapping_test"); if(NULL = =hmapfile) {                returnFALSE; }            //2. Create a ViewPVOID PBuf = MapViewOfFile (Hmapfile, File_map_all_access,0,0, -); if(NULL = =PBuf) {                returnFALSE; }            //3. displaying shared dataMessageBox (NULL, (LPCWSTR) PBuf, L"EXE to pass the data is:", MB_OK); //4. Cancel mapping, close handleUnmapViewOfFile (PBUF);        CloseHandle (Hmapfile); }         Break;  CaseDll_thread_attach: CaseDll_thread_detach: CaseDll_process_detach: Break; }    returnTRUE;}

Run:

Using memory-mapped files for EXE, DLL communication (Non-MFC)

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.