Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dllmain.cpp : Defines the entry point for the DLL application.
- #include "stdafx.h"
- #define SHAREDMEMSIZE 8192
- static LPVOID lpvMem = NULL;
- static HANDLE hMapObject = NULL;
- BOOL APIENTRY DllMain( HINSTANCE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- BOOL fInit, fIgnore;
- fstream fs;
- fs.open("C:\\Users\\Tomasz\\Documents\\log.txt", ios::out | ios::app);
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- //Creating a named file mapping object
- MessageBoxA(NULL, "Proces sie podlacza", "Process", 0x0000000L);
- hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, SHAREDMEMSIZE, TEXT("mtmemfilemap"));
- if (fs.is_open())
- fs << "#DLLMain: Process connected, hMapObject: " << hMapObject << "\tlpvMem: " << lpvMem << endl;
- if (hMapObject == NULL)
- return FALSE;
- //The first process to attach initializes memory
- fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
- //Get a pointer to the file-mapped shared memory
- lpvMem = MapViewOfFile(hMapObject, FILE_MAP_WRITE, 0, 0, 0);
- if (lpvMem == NULL)
- return FALSE;
- //Initialize memory if this is the first process
- if (fInit) {
- LPVOID lp = memset(lpvMem, '\0', SHAREDMEMSIZE);
- if (fs.is_open())
- fs << "#DLLMain: First process setting memory. LPVOID : " << lp << endl;
- MessageBoxA(NULL, "Pierwszy proces", "Process", 0x0000000L);
- }
- break;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- break;
- case DLL_PROCESS_DETACH:
- // Unmap shared memory from the process's address space
- MessageBoxA(NULL, "Bye bye", "Process", 0x0000000L);
- fIgnore = UnmapViewOfFile(lpvMem);
- //Close the process's handle to the file-mapping object
- fIgnore = CloseHandle(hMapObject);
- break;
- default:
- break;
- }
- return TRUE;
- UNREFERENCED_PARAMETER(hModule);
- UNREFERENCED_PARAMETER(lpReserved);
- }
- _DLLAPI void __cdecl SetSharedMem(LPWSTR lpszBuf)
- {
- LPWSTR lpszTmp;
- DWORD dwCount = 1;
- // Get the addres of the shared memory block
- CopyMemory(&lpvMem, lpszBuf, lstrlenW(lpszBuf));
- }
- _DLLAPI void __cdecl GetSharedMem(LPWSTR lpszBuf)
- {
- LPWSTR lpszTmp;
- //Get the address of the sared memory block
- lpszTmp = (LPWSTR)lpvMem;
- // Copy from shared memory iinto the callers buffer
- //memcpy(lpszBuf, &lpvMem, 10);
- CopyMemory(lpszBuf, &lpvMem, 10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement