Menu

Sharing memory in .dll

2003-01-27
2012-09-26
  • Ondrej Majerech

    Ondrej Majerech - 2003-01-27

    In my textbook, there`s that I can share data in .dll like this:

      // some code before
    #pragma data_seg ("shared")
      //some initialized, global variables
    int a = 0;
    #pragma data_seg ()
    #pragma comment (linker, "/SECTION:shared,RWS")

    but that doesn`t work, when I run the app twice, the data AREN`T shared, between theese two instances. Can anyone help?

    Thanks,

    Oxygenium.

     
    • Patrick Ogay

      Patrick Ogay - 2003-01-28

      There was a disussion on pragma in GCC...
      The conclusion was, that pragma make incompatible code and GCC doesn't support (m)any pragmas.

      I guess you have to use the shared memory calls...

      Patrick

       
    • Nobody/Anonymous

      Shared memory calls? How can I do that?

       
    • Nobody/Anonymous

      Same issue here.  I'm trying to code a keyboard hook and all the examples (MSVC) I can find use the pragmas to define a shared dataseg.  Someone please post a work-around if minGW can't do it.

      Thanks in advance.

       
    • Ondrej Majerech

      Ondrej Majerech - 2003-01-28

      That's nice, but you haven't told me how to share that data?

      Please help,

      Oxygenium.

       
    • Nobody/Anonymous

      Ok, I figured it out on my own. Character building, even if a waste of time.

      Here is an example of a keyboard hook that works when compiled by minGW. It includes the steps of how to implement shared memory within a DLL:

      #include <windows.h>
      #include <stdio.h>

      #include <memory.h>

      static LPVOID lpvMem = NULL; // pointer to shared memory

      typedef struct sharedstruct{
        UINT uMsg;
        HWND hWndMain;
        HHOOK hKeyHook;
      } shareddataareatype;

      HINSTANCE hInstance = 0;
      HOOKPROC lpfnHookProc = 0;
      #include "hookmain.h"

      int writetrace(char *buf,char *mode){
        FILE *fd=fopen("tracedll.txt",mode);
      //  char buf[100];
      //  sprintf(buf,"setting module handle to %d\n", hInstance);
        fputs(buf,fd);
        fclose(fd);
      }

      //int DllClass::instances;
      LRESULT __stdcall KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam) {
         char szDebug[100];
         shareddataareatype *shared=(shareddataareatype*)lpvMem;

         // Check for exception cases...
         char buf[100];

         if (nCode < 0)
            return (CallNextHookEx (shared->hKeyHook, nCode, wParam, lParam));
         if (nCode == HC_NOREMOVE)
            return (CallNextHookEx (shared->hKeyHook, nCode, wParam, lParam));

         // pass it to hook caller
         int up=(HIWORD (lParam) & 0xC000);
         if(!up) {
           GetKeyNameText(lParam, szDebug,sizeof(szDebug));
           sprintf(buf,"KeyboardFunc(%d) ncode=%d wparam=%d, lparam=%d (%d,%d) - %s up:%d\n",
              (int)shared,nCode,wParam,lParam,shared->hWndMain,shared->hKeyHook,szDebug);
           writetrace(buf,"a");
           sprintf(buf,"send to %d, msg:%d, %d,%d\n",shared->hWndMain, shared->uMsg, wParam, lParam);
           writetrace(buf,"a");
           PostMessage (shared->hWndMain, shared->uMsg, wParam, lParam);
         }

         // pass it on through to who it belongs to
         return (CallNextHookEx (shared->hKeyHook, nCode, wParam, lParam));
      }

      extern "C" EXPORT BOOL __stdcall InstallKeyboardHook (HWND hWnd, UINT uMyMsg, DWORD threadId)
      {
      //   hWndMain = hWnd ;
      //   uMsg     = uMyMsg;
         shareddataareatype *shared=(shareddataareatype*)lpvMem;
         char buf[100];
         HHOOK hKeyHook;

         shared->hWndMain=hWnd;
         shared->uMsg=uMyMsg;
         lpfnHookProc = (HOOKPROC) KeyboardFunc ;
         sprintf(buf,"InstallKeyboardHook hInstance=%d\n", hInstance);
         writetrace(buf,"a");
         hKeyHook = SetWindowsHookEx (WH_KEYBOARD, lpfnHookProc, hInstance, threadId);
         if (hKeyHook) {
           writetrace("SetWindowsHookEx worked\n","a");
           shared->hKeyHook=hKeyHook;
           return TRUE ;
         }
         else {
           sprintf(buf,"SetWindowsHookEx failed err=%d\n", GetLastError());
           writetrace(buf,"a");
            return FALSE ;
         }
      }

      extern "C" BOOL APIENTRY DllMain(
        HINSTANCE hModule     /* Library instance handle. */ ,
        DWORD reason        /* Reason this function is being called. */ ,
        LPVOID reserved     /* Not used. */ )
      {
        HANDLE hMapObject = NULL;  // handle to file mapping
        BOOL fInit, fIgnore;

        char buf[100];
          switch (reason)
          {
            case DLL_PROCESS_ATTACH:
              hInstance = hModule; // the DLLs instance handle.
              sprintf(buf,"DLL_PROCESS_ATTACH hInstance=%d\n",hInstance);
              writetrace(buf,"a");
              hMapObject = CreateFileMapping(
                      INVALID_HANDLE_VALUE, // use paging file
                      NULL,                 // default security attributes
                      PAGE_READWRITE,       // read/write access
                      0,                    // size: high 32-bits
                      sizeof(shareddataareatype), // size: low 32-bits
                      "HookShareDataFile");       // name of map object
              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,     // object to map view of
                  FILE_MAP_WRITE, // read/write access
                  0,              // high offset:  map from
                  0,              // low offset:   beginning
                  0);             // default: map entire file
              if (lpvMem == NULL)
                return FALSE;

              // Initialize memory if this is the first process.

      //        if (fInit)
      //           memset(lpvMem, '\0', SHMEMSIZE);
              break;

            case DLL_PROCESS_DETACH:
              break;
            case DLL_THREAD_ATTACH:
              break;
            case DLL_THREAD_DETACH:
              // Unmap shared memory from the process's address space.
              fIgnore = UnmapViewOfFile(lpvMem);
              // Close the process's handle to the file-mapping object.
              fIgnore = CloseHandle(hMapObject);
              break;
          }

          /* Returns TRUE on success, FALSE on failure */
          return TRUE;
      }

       
    • Nobody/Anonymous

      apparently you can't post anything more than a code snippet here.  You can find an example of how to implement shared memory under Win32 at:

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/using_shared_memory_in_a_dynamic_link_library.asp

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.