Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Shared memory with CreateDIBSection

Shared memory with CreateDIBSection

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsperformance
9 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phieu
    wrote on last edited by
    #1

    Hi all, I want to create a bitmap by the function CreateDIBSection, and share the bitmap with other process. HBITMAP CreateDIBSection( HDC hdc, // handle to DC CONST BITMAPINFO *pbmi, // bitmap data UINT iUsage, // data type indicator VOID **ppvBits, // bit values HANDLE hSection, // handle to file mapping object DWORD dwOffset // offset to bitmap bit values ); Following MSDN, hSection is handle that created by CreateFileMapping. Now I want ask that: in the shared-memory that hold by handle hSection: what is shared data? it is ppvBits?. If not, what is relating between ppvBits and data shared in the memory. I don't see any word in MSDN about this. That is good if anyone give me a sample code. Thank you. The world is not enough!

    K M 2 Replies Last reply
    0
    • P phieu

      Hi all, I want to create a bitmap by the function CreateDIBSection, and share the bitmap with other process. HBITMAP CreateDIBSection( HDC hdc, // handle to DC CONST BITMAPINFO *pbmi, // bitmap data UINT iUsage, // data type indicator VOID **ppvBits, // bit values HANDLE hSection, // handle to file mapping object DWORD dwOffset // offset to bitmap bit values ); Following MSDN, hSection is handle that created by CreateFileMapping. Now I want ask that: in the shared-memory that hold by handle hSection: what is shared data? it is ppvBits?. If not, what is relating between ppvBits and data shared in the memory. I don't see any word in MSDN about this. That is good if anyone give me a sample code. Thank you. The world is not enough!

      K Offline
      K Offline
      KaRl
      wrote on last edited by
      #2

      You could have a look to these two articles:* A freeware MFC class to encapsulate Memory Mapped Files[^]

      • Share variables in the shared memory across processes[^]

      Where do you expect us to go when the bombs fall?

      Fold with us! ¤ flickr

      1 Reply Last reply
      0
      • P phieu

        Hi all, I want to create a bitmap by the function CreateDIBSection, and share the bitmap with other process. HBITMAP CreateDIBSection( HDC hdc, // handle to DC CONST BITMAPINFO *pbmi, // bitmap data UINT iUsage, // data type indicator VOID **ppvBits, // bit values HANDLE hSection, // handle to file mapping object DWORD dwOffset // offset to bitmap bit values ); Following MSDN, hSection is handle that created by CreateFileMapping. Now I want ask that: in the shared-memory that hold by handle hSection: what is shared data? it is ppvBits?. If not, what is relating between ppvBits and data shared in the memory. I don't see any word in MSDN about this. That is good if anyone give me a sample code. Thank you. The world is not enough!

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        phieu wrote:

        what is shared data? it is ppvBits?.

        If you've provided a file mapping (hSection) then the data pointed to by ppvBits can be shared through the file mapping. To do that, refer to the links suggested above :) Mark

        "If you can dodge a wrench, you can dodge a ball."

        P 1 Reply Last reply
        0
        • M Mark Salsbery

          phieu wrote:

          what is shared data? it is ppvBits?.

          If you've provided a file mapping (hSection) then the data pointed to by ppvBits can be shared through the file mapping. To do that, refer to the links suggested above :) Mark

          "If you can dodge a wrench, you can dodge a ball."

          P Offline
          P Offline
          phieu
          wrote on last edited by
          #4

          Thanks Mark. OK, Now I see the data pointed to by ppvBits can be shared through the file mapping. But now I don’t know why: error code = 87 (ERROR_INVALID_PARAMETER) when I call CreateDIBSection thought out but bitmap is valid. Someone can tell me why? Thank you. Here is my code: #define BITMAP_MMF_NAME "Global\\PRJ_IMS" HANDLE m_hMMF; HBITMAP m_hBitmap; void CBitmapMMFDlg::OnButton3() { // TODO: この位置にコントロール通知ハンドラ用のコードを追加してください int nWidth = 100, nHeight = 100; long nSize = nWidth*nHeight*3; // Create a file-mapping m_hMMF = ::CreateFileMapping( (HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, nSize, BITMAP_MMF_NAME); DWORD dw = GetLastError(); if (m_hMMF) { // Create bitmap and map this bitmap to File-mapping m_hBitmap = NULL; HDC hdc = ::GetDC(NULL); // entire screen //off-screen bitmap/image size - width must be DWORD aligned (multiples of 4) LONG sizeImage = ((nWidth * 3 + 3) & 0xfffffffc) * nHeight; BITMAPINFO bmpInfo; memset(&bmpInfo,0,sizeof(bmpInfo)); bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth = nWidth; bmpInfo.bmiHeader.biHeight = -nHeight; bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = 24; bmpInfo.bmiHeader.biCompression = BI_RGB; bmpInfo.bmiHeader.biSizeImage = sizeImage ; bmpInfo.bmiHeader.biXPelsPerMeter = 0; bmpInfo.bmiHeader.biYPelsPerMeter = 0; bmpInfo.bmiHeader.biClrUsed = 0; bmpInfo.bmiHeader.biClrImportant = 0; //Create DIB Section - off screen bitmap void* pBits = NULL; DWORD dwOffset = 0; m_hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pBits, m_hMMF, dwOffset); TRACE("Error code = %d \n", GetLastError()); // change bitmap bit data -> to test output if(pBits != NULL) { for (int i = 0; i < 30000; i ++) { *((char*)(pBits) +i) = i%255; } } ///////////////////////////////////////////////// // display bitmap if (m_hBitmap != NULL) { CDC* pDC = GetDlgItem(IDC_STATIC)->GetDC(); if (pDC) { CDC dcMem; dcMem.CreateCompatibleDC(pDC); CBitmap* pBmp = CBitmap::FromHandle(m_hBitmap); CBitmap* pOldBmp= dcMem.SelectObject(pBmp); pDC->BitBlt(0,0, 100, 100, &dcMem, 0, 0, SRCCOPY); } } } }

          M 2 Replies Last reply
          0
          • P phieu

            Thanks Mark. OK, Now I see the data pointed to by ppvBits can be shared through the file mapping. But now I don’t know why: error code = 87 (ERROR_INVALID_PARAMETER) when I call CreateDIBSection thought out but bitmap is valid. Someone can tell me why? Thank you. Here is my code: #define BITMAP_MMF_NAME "Global\\PRJ_IMS" HANDLE m_hMMF; HBITMAP m_hBitmap; void CBitmapMMFDlg::OnButton3() { // TODO: この位置にコントロール通知ハンドラ用のコードを追加してください int nWidth = 100, nHeight = 100; long nSize = nWidth*nHeight*3; // Create a file-mapping m_hMMF = ::CreateFileMapping( (HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, nSize, BITMAP_MMF_NAME); DWORD dw = GetLastError(); if (m_hMMF) { // Create bitmap and map this bitmap to File-mapping m_hBitmap = NULL; HDC hdc = ::GetDC(NULL); // entire screen //off-screen bitmap/image size - width must be DWORD aligned (multiples of 4) LONG sizeImage = ((nWidth * 3 + 3) & 0xfffffffc) * nHeight; BITMAPINFO bmpInfo; memset(&bmpInfo,0,sizeof(bmpInfo)); bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth = nWidth; bmpInfo.bmiHeader.biHeight = -nHeight; bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = 24; bmpInfo.bmiHeader.biCompression = BI_RGB; bmpInfo.bmiHeader.biSizeImage = sizeImage ; bmpInfo.bmiHeader.biXPelsPerMeter = 0; bmpInfo.bmiHeader.biYPelsPerMeter = 0; bmpInfo.bmiHeader.biClrUsed = 0; bmpInfo.bmiHeader.biClrImportant = 0; //Create DIB Section - off screen bitmap void* pBits = NULL; DWORD dwOffset = 0; m_hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pBits, m_hMMF, dwOffset); TRACE("Error code = %d \n", GetLastError()); // change bitmap bit data -> to test output if(pBits != NULL) { for (int i = 0; i < 30000; i ++) { *((char*)(pBits) +i) = i%255; } } ///////////////////////////////////////////////// // display bitmap if (m_hBitmap != NULL) { CDC* pDC = GetDlgItem(IDC_STATIC)->GetDC(); if (pDC) { CDC dcMem; dcMem.CreateCompatibleDC(pDC); CBitmap* pBmp = CBitmap::FromHandle(m_hBitmap); CBitmap* pOldBmp= dcMem.SelectObject(pBmp); pDC->BitBlt(0,0, 100, 100, &dcMem, 0, 0, SRCCOPY); } } } }

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            I don't see anything right away... Try changing to bmpInfo.bmiHeader.biSizeImage = 0; Also you calculate your file mapping size like this: long nSize = nWidth*nHeight*3; and your bmpInfo.bmiHeader.biSizeImage like this LONG sizeImage = ((nWidth * 3 + 3) & 0xfffffffc) * nHeight; Both should use the same calculation and the second method is better since DIB rows should be aligned to a 4-byte boundary. In this case it's ok with a rowsize of 100 :) Mark

            "If you can dodge a wrench, you can dodge a ball."

            1 Reply Last reply
            0
            • P phieu

              Thanks Mark. OK, Now I see the data pointed to by ppvBits can be shared through the file mapping. But now I don’t know why: error code = 87 (ERROR_INVALID_PARAMETER) when I call CreateDIBSection thought out but bitmap is valid. Someone can tell me why? Thank you. Here is my code: #define BITMAP_MMF_NAME "Global\\PRJ_IMS" HANDLE m_hMMF; HBITMAP m_hBitmap; void CBitmapMMFDlg::OnButton3() { // TODO: この位置にコントロール通知ハンドラ用のコードを追加してください int nWidth = 100, nHeight = 100; long nSize = nWidth*nHeight*3; // Create a file-mapping m_hMMF = ::CreateFileMapping( (HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, nSize, BITMAP_MMF_NAME); DWORD dw = GetLastError(); if (m_hMMF) { // Create bitmap and map this bitmap to File-mapping m_hBitmap = NULL; HDC hdc = ::GetDC(NULL); // entire screen //off-screen bitmap/image size - width must be DWORD aligned (multiples of 4) LONG sizeImage = ((nWidth * 3 + 3) & 0xfffffffc) * nHeight; BITMAPINFO bmpInfo; memset(&bmpInfo,0,sizeof(bmpInfo)); bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth = nWidth; bmpInfo.bmiHeader.biHeight = -nHeight; bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = 24; bmpInfo.bmiHeader.biCompression = BI_RGB; bmpInfo.bmiHeader.biSizeImage = sizeImage ; bmpInfo.bmiHeader.biXPelsPerMeter = 0; bmpInfo.bmiHeader.biYPelsPerMeter = 0; bmpInfo.bmiHeader.biClrUsed = 0; bmpInfo.bmiHeader.biClrImportant = 0; //Create DIB Section - off screen bitmap void* pBits = NULL; DWORD dwOffset = 0; m_hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pBits, m_hMMF, dwOffset); TRACE("Error code = %d \n", GetLastError()); // change bitmap bit data -> to test output if(pBits != NULL) { for (int i = 0; i < 30000; i ++) { *((char*)(pBits) +i) = i%255; } } ///////////////////////////////////////////////// // display bitmap if (m_hBitmap != NULL) { CDC* pDC = GetDlgItem(IDC_STATIC)->GetDC(); if (pDC) { CDC dcMem; dcMem.CreateCompatibleDC(pDC); CBitmap* pBmp = CBitmap::FromHandle(m_hBitmap); CBitmap* pOldBmp= dcMem.SelectObject(pBmp); pDC->BitBlt(0,0, 100, 100, &dcMem, 0, 0, SRCCOPY); } } } }

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Is m_hBitmap NULL after the CreateDIBSection call?

              "If you can dodge a wrench, you can dodge a ball."

              P 1 Reply Last reply
              0
              • M Mark Salsbery

                Is m_hBitmap NULL after the CreateDIBSection call?

                "If you can dodge a wrench, you can dodge a ball."

                P Offline
                P Offline
                phieu
                wrote on last edited by
                #7

                m_hBitmap is not NULL, and I can display it ok, but GetLastError() = 87, INVALID_PARAMETER. I don't know why?

                M 1 Reply Last reply
                0
                • P phieu

                  m_hBitmap is not NULL, and I can display it ok, but GetLastError() = 87, INVALID_PARAMETER. I don't know why?

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  If it's not NULL then it was successful and calling GetLastError() isn't necessarily valid. You only need to call that on failure. From the docs: "Most functions that set the thread's last-error code set it when they fail. However, some functions also set the last-error code when they succeed. If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not." Generally errors are checked for and handled something like: m_hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pBits, m_hMMF, dwOffset); if (NULL == m_hBitmap) { TRACE("Error code = %d \n", GetLastError()); ...do appropriate error handling/cleanup here }

                  "If you can dodge a wrench, you can dodge a ball."

                  P 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    If it's not NULL then it was successful and calling GetLastError() isn't necessarily valid. You only need to call that on failure. From the docs: "Most functions that set the thread's last-error code set it when they fail. However, some functions also set the last-error code when they succeed. If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not." Generally errors are checked for and handled something like: m_hBitmap = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pBits, m_hMMF, dwOffset); if (NULL == m_hBitmap) { TRACE("Error code = %d \n", GetLastError()); ...do appropriate error handling/cleanup here }

                    "If you can dodge a wrench, you can dodge a ball."

                    P Offline
                    P Offline
                    phieu
                    wrote on last edited by
                    #9

                    Ok, thanks Mark!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups