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. Sharing a HBITMAP between 2 applications [modified]

Sharing a HBITMAP between 2 applications [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelpquestion
11 Posts 4 Posters 0 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.
  • L Offline
    L Offline
    Llasus
    wrote on last edited by
    #1

    Hello and good day. I have 2 applications wherein I want the 1st which creates a graph send its image to the 2nd which will display it. I searched for some ways and ended up using a MFC shared DLL wherein I have a HBITMAP which the 1st one will write image on, and the 2nd one will read (using WM_COPYDATA for signalling, and mutex for synch). It's declared as the ff:

    #pragma bss_seg("MFCDLLSample")
    	TESTDLL_API HBITMAP DLLImageBmp;
    #pragma bss_seg()
    

    Now, I checked first if it works by having the 1st app make a graph then display it on another picture box (same app) and it actually worked. When I tried to display it in the 2nd, it does not display. I coded it like this in the 2nd app:

    	//obtain the CDC handle for the picture box
    	PicWnd = GetDlgItem(IDC_PICBOX);
    	PicDC  = PicWnd->GetDC();
    
    	oldBmp = (HBITMAP)PicDC->SelectObject(DLLImageBmp);
    
    	PicPaintDC.BitBlt(0, 0, 404, 404, PicDC, 0, 0, SRCCOPY);
    	CDialog::OnPaint();
    

    Can anyone help me out with this? I really need to know what might be wrong with my code or implementation. Thank you in advance for the help and time! --added contents: I forgot, TESTDLL_API's definition is this:

    #ifdef TESTDLL_EXPORTS
    #define TESTDLL_API __declspec(dllexport)
    #else
    #define TESTDLL_API __declspec(dllimport)
    #endif
    

    Thanks again!

    modified on Monday, June 23, 2008 12:48 AM

    N H 2 Replies Last reply
    0
    • L Llasus

      Hello and good day. I have 2 applications wherein I want the 1st which creates a graph send its image to the 2nd which will display it. I searched for some ways and ended up using a MFC shared DLL wherein I have a HBITMAP which the 1st one will write image on, and the 2nd one will read (using WM_COPYDATA for signalling, and mutex for synch). It's declared as the ff:

      #pragma bss_seg("MFCDLLSample")
      	TESTDLL_API HBITMAP DLLImageBmp;
      #pragma bss_seg()
      

      Now, I checked first if it works by having the 1st app make a graph then display it on another picture box (same app) and it actually worked. When I tried to display it in the 2nd, it does not display. I coded it like this in the 2nd app:

      	//obtain the CDC handle for the picture box
      	PicWnd = GetDlgItem(IDC_PICBOX);
      	PicDC  = PicWnd->GetDC();
      
      	oldBmp = (HBITMAP)PicDC->SelectObject(DLLImageBmp);
      
      	PicPaintDC.BitBlt(0, 0, 404, 404, PicDC, 0, 0, SRCCOPY);
      	CDialog::OnPaint();
      

      Can anyone help me out with this? I really need to know what might be wrong with my code or implementation. Thank you in advance for the help and time! --added contents: I forgot, TESTDLL_API's definition is this:

      #ifdef TESTDLL_EXPORTS
      #define TESTDLL_API __declspec(dllexport)
      #else
      #define TESTDLL_API __declspec(dllimport)
      #endif
      

      Thanks again!

      modified on Monday, June 23, 2008 12:48 AM

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      GDI handles cannot be shared across process. You have to think of some alternative methods to transfer the image from the first process to next. A possible method is sending the pixel data of the bitmap to the second process and the second process should recreate the bitmap using the pixel data. 1) you can use GetDIBits() function to get the pixel data of a bitmap 2) use WM_COPYDATA or other interprocess communication methods to send the pixel data to the second process. 3. use CreateDIBitmap() function to recreate the bitmap.

      nave [OpenedFileFinder]

      L 1 Reply Last reply
      0
      • N Naveen

        GDI handles cannot be shared across process. You have to think of some alternative methods to transfer the image from the first process to next. A possible method is sending the pixel data of the bitmap to the second process and the second process should recreate the bitmap using the pixel data. 1) you can use GetDIBits() function to get the pixel data of a bitmap 2) use WM_COPYDATA or other interprocess communication methods to send the pixel data to the second process. 3. use CreateDIBitmap() function to recreate the bitmap.

        nave [OpenedFileFinder]

        L Offline
        L Offline
        Llasus
        wrote on last edited by
        #3

        Naveen wrote:

        GDI handles cannot be shared across process.

        Yes, but if it was shared through DLL, its still not possible? :confused: I was adviced last time to do that. Please refer to this link[^].

        N 1 Reply Last reply
        0
        • L Llasus

          Naveen wrote:

          GDI handles cannot be shared across process.

          Yes, but if it was shared through DLL, its still not possible? :confused: I was adviced last time to do that. Please refer to this link[^].

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          Llasus wrote:

          I was adviced last time to do that. Please refer to this link[^].

          I am also confused seeing that. :confused: AFAIK, it cannot be shared.

          From MSDN:

          "_GDI objects support only one handle per object. Handles to GDI objects are private to a process. That is, only the process that created the GDI object can use the object handle_" The GDI handles are actually offset to the handle map's that reside in the client side. So when you pass a handle to another process, it simply points to a location in the handle table in that process. Some times, the object in that location will be entirly a different one such as Brush or Pen or some times the object itself is not created. So the result will be unpredictable. if you want to know more please read the article Give Me a Handle, and I'll Show You an Object[^]

          nave [OpenedFileFinder]

          L S 2 Replies Last reply
          0
          • N Naveen

            Llasus wrote:

            I was adviced last time to do that. Please refer to this link[^].

            I am also confused seeing that. :confused: AFAIK, it cannot be shared.

            From MSDN:

            "_GDI objects support only one handle per object. Handles to GDI objects are private to a process. That is, only the process that created the GDI object can use the object handle_" The GDI handles are actually offset to the handle map's that reside in the client side. So when you pass a handle to another process, it simply points to a location in the handle table in that process. Some times, the object in that location will be entirly a different one such as Brush or Pen or some times the object itself is not created. So the result will be unpredictable. if you want to know more please read the article Give Me a Handle, and I'll Show You an Object[^]

            nave [OpenedFileFinder]

            L Offline
            L Offline
            Llasus
            wrote on last edited by
            #5

            Naveen wrote:

            I am also confused seeing that. [Confused]

            Guess I trusted that advice too early, even though I had a doubt in my mind if it was possible in the first place. Anyway, I'll read the article to have more knowledge on this and try coding the solution that you gave me earlier on. Thank you very much for your help and time! :)

            S 1 Reply Last reply
            0
            • L Llasus

              Naveen wrote:

              I am also confused seeing that. [Confused]

              Guess I trusted that advice too early, even though I had a doubt in my mind if it was possible in the first place. Anyway, I'll read the article to have more knowledge on this and try coding the solution that you gave me earlier on. Thank you very much for your help and time! :)

              S Offline
              S Offline
              SandipG
              wrote on last edited by
              #6

              :(( Sorry for the advice man but i was really not aware of the fact that handles cant be shared across the processes.

              Regards, Sandip.

              L 1 Reply Last reply
              0
              • N Naveen

                Llasus wrote:

                I was adviced last time to do that. Please refer to this link[^].

                I am also confused seeing that. :confused: AFAIK, it cannot be shared.

                From MSDN:

                "_GDI objects support only one handle per object. Handles to GDI objects are private to a process. That is, only the process that created the GDI object can use the object handle_" The GDI handles are actually offset to the handle map's that reside in the client side. So when you pass a handle to another process, it simply points to a location in the handle table in that process. Some times, the object in that location will be entirly a different one such as Brush or Pen or some times the object itself is not created. So the result will be unpredictable. if you want to know more please read the article Give Me a Handle, and I'll Show You an Object[^]

                nave [OpenedFileFinder]

                S Offline
                S Offline
                SandipG
                wrote on last edited by
                #7

                Hi Naveen, Thanks for the information.

                Regards, Sandip.

                N 1 Reply Last reply
                0
                • S SandipG

                  :(( Sorry for the advice man but i was really not aware of the fact that handles cant be shared across the processes.

                  Regards, Sandip.

                  L Offline
                  L Offline
                  Llasus
                  wrote on last edited by
                  #8

                  Hi Sandip. its ok. :) you helped me out not to do the clipboard solution anyway. Thank you. :)

                  1 Reply Last reply
                  0
                  • S SandipG

                    Hi Naveen, Thanks for the information.

                    Regards, Sandip.

                    N Offline
                    N Offline
                    Naveen
                    wrote on last edited by
                    #9

                    :)

                    nave [OpenedFileFinder]

                    1 Reply Last reply
                    0
                    • L Llasus

                      Hello and good day. I have 2 applications wherein I want the 1st which creates a graph send its image to the 2nd which will display it. I searched for some ways and ended up using a MFC shared DLL wherein I have a HBITMAP which the 1st one will write image on, and the 2nd one will read (using WM_COPYDATA for signalling, and mutex for synch). It's declared as the ff:

                      #pragma bss_seg("MFCDLLSample")
                      	TESTDLL_API HBITMAP DLLImageBmp;
                      #pragma bss_seg()
                      

                      Now, I checked first if it works by having the 1st app make a graph then display it on another picture box (same app) and it actually worked. When I tried to display it in the 2nd, it does not display. I coded it like this in the 2nd app:

                      	//obtain the CDC handle for the picture box
                      	PicWnd = GetDlgItem(IDC_PICBOX);
                      	PicDC  = PicWnd->GetDC();
                      
                      	oldBmp = (HBITMAP)PicDC->SelectObject(DLLImageBmp);
                      
                      	PicPaintDC.BitBlt(0, 0, 404, 404, PicDC, 0, 0, SRCCOPY);
                      	CDialog::OnPaint();
                      

                      Can anyone help me out with this? I really need to know what might be wrong with my code or implementation. Thank you in advance for the help and time! --added contents: I forgot, TESTDLL_API's definition is this:

                      #ifdef TESTDLL_EXPORTS
                      #define TESTDLL_API __declspec(dllexport)
                      #else
                      #define TESTDLL_API __declspec(dllimport)
                      #endif
                      

                      Thanks again!

                      modified on Monday, June 23, 2008 12:48 AM

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #10

                      Why you dont use of WM_COPYDATA?Inter-Process Communication using WM_COPYDATA[^].

                      L 1 Reply Last reply
                      0
                      • H Hamid Taebi

                        Why you dont use of WM_COPYDATA?Inter-Process Communication using WM_COPYDATA[^].

                        L Offline
                        L Offline
                        Llasus
                        wrote on last edited by
                        #11

                        Hamid. wrote:

                        Why you dont use of WM_COPYDATA?Inter-Process Communication using WM_COPYDATA[^].

                        Thank you for the suggestion. I am looking into it right now but the size of the bitmap bits actually is not a constant value. So I'll be needing a pointer for that in which WM_COPYDATA can't be used. I'm still checking some other routes like named pipes, or shared memory. Any further suggestions? Thank you!

                        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