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)