Advertisement
Guest User

dllmain

a guest
Aug 21st, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. // dllmain.cpp : Defines the entry point for the DLL application.
  2. #include "stdafx.h"
  3.  
  4. #define SHAREDMEMSIZE 8192
  5.  
  6. static LPVOID   lpvMem = NULL;
  7. static HANDLE   hMapObject = NULL;
  8.  
  9. BOOL APIENTRY DllMain( HINSTANCE hModule,
  10.                        DWORD  ul_reason_for_call,
  11.                        LPVOID lpReserved
  12.                      )
  13. {
  14.     BOOL fInit, fIgnore;
  15.  
  16.     fstream fs;
  17.     fs.open("C:\\Users\\Tomasz\\Documents\\log.txt", ios::out | ios::app);
  18.  
  19.     switch (ul_reason_for_call)
  20.     {
  21.     case DLL_PROCESS_ATTACH:
  22.         //Creating a named file mapping object
  23.         MessageBoxA(NULL, "Proces sie podlacza", "Process", 0x0000000L);
  24.         hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, SHAREDMEMSIZE, TEXT("mtmemfilemap"));
  25.  
  26.         if (fs.is_open())
  27.             fs << "#DLLMain: Process connected, hMapObject: " << hMapObject << "\tlpvMem: " << lpvMem << endl;
  28.  
  29.         if (hMapObject == NULL)
  30.             return FALSE;
  31.  
  32.         //The first process to attach initializes memory
  33.         fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
  34.  
  35.         //Get a pointer to the file-mapped shared memory
  36.  
  37.         lpvMem = MapViewOfFile(hMapObject, FILE_MAP_WRITE, 0, 0, 0);
  38.  
  39.         if (lpvMem == NULL)
  40.             return FALSE;
  41.  
  42.         //Initialize memory if this is the first process
  43.  
  44.         if (fInit) {
  45.             LPVOID lp = memset(lpvMem, '\0', SHAREDMEMSIZE);
  46.             if (fs.is_open())
  47.                 fs << "#DLLMain: First process setting memory. LPVOID : " << lp << endl;
  48.  
  49.             MessageBoxA(NULL, "Pierwszy proces", "Process", 0x0000000L);
  50.         }
  51.  
  52.         break;
  53.  
  54.     case DLL_THREAD_ATTACH:
  55.  
  56.         break;
  57.     case DLL_THREAD_DETACH:
  58.    
  59.         break;
  60.     case DLL_PROCESS_DETACH:
  61.         // Unmap shared memory from the process's address space
  62.  
  63.         MessageBoxA(NULL, "Bye bye", "Process", 0x0000000L);
  64.  
  65.         fIgnore = UnmapViewOfFile(lpvMem);
  66.  
  67.         //Close the process's handle to the file-mapping object
  68.  
  69.         fIgnore = CloseHandle(hMapObject);
  70.  
  71.         break;
  72.     default:
  73.         break;
  74.     }
  75.     return TRUE;
  76.  
  77.     UNREFERENCED_PARAMETER(hModule);
  78.     UNREFERENCED_PARAMETER(lpReserved);
  79. }
  80.  
  81.  
  82. _DLLAPI void __cdecl SetSharedMem(LPWSTR lpszBuf)
  83. {
  84.     LPWSTR lpszTmp;
  85.     DWORD dwCount = 1;
  86.  
  87.     // Get the addres of the shared memory block
  88.  
  89.  
  90.     CopyMemory(&lpvMem, lpszBuf, lstrlenW(lpszBuf));
  91. }
  92.  
  93. _DLLAPI void __cdecl GetSharedMem(LPWSTR lpszBuf)
  94. {
  95.     LPWSTR lpszTmp;
  96.  
  97.     //Get the address of the sared memory block
  98.  
  99.     lpszTmp = (LPWSTR)lpvMem;
  100.  
  101.    
  102.  
  103.     // Copy from shared memory iinto the callers buffer
  104.     //memcpy(lpszBuf, &lpvMem, 10);
  105.     CopyMemory(lpszBuf, &lpvMem, 10);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement