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*  Energy: EXE and DLL communication using memory-mapped files ****************************************************************************************/# Include "stdafx.h" #include <stdlib.h> #include <afxwin.h>int _tmain (int argc, _tchar* argv[]) {    //1. Create a named file map    HANDLE hmapfile = createfilemapping (Invalid_handle_value, NULL, Page_readwrite,        0, M, L "File_ Mapping_test ");    if (NULL = = Hmapfile | | Invalid_handle_value = = hmapfile)    {        return FALSE;    }    2. Create VIEW    PVOID PBuf = MapViewOfFile (hmapfile, file_map_all_access, 0, 0, +);    if (NULL = = PBuf)    {        return FALSE;    }    3. Copy the shared data into the file map    wcscpy_s ((Pwchar) PBuf, L "Aheadshooter");    4. Load DLL    LoadLibrary (L "Testdll");    5. Wait to end    GetChar ();    6. Cancel mapping, close handle    unmapviewoffile (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_f Or_call, LPVOID lpreserved) {switch (ul_reason_for_call) {case DLL_PR Ocess_attach: {//1. Open file mapping HANDLE hmapfile = openfilemapping (file_map_all_access, Fals            E, L "file_mapping_test");            if (NULL = = hmapfile) {return FALSE; }//2.            Create VIEW PVOID PBuf = MapViewOfFile (hmapfile, file_map_all_access, 0, 0, 16);            if (NULL = = PBuf) {return FALSE; }//3.            Displays the shared data MessageBox (NULL, (LPCWSTR) PBuf, L "EXE passes over the data as:", MB_OK); 4.Cancel mapping, close handle unmapviewoffile (PBUF);        CloseHandle (Hmapfile);    } break;    Case Dll_thread_attach:case Dll_thread_detach:case Dll_process_detach:break; } return TRUE;

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.