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)