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. Pointer error with SendMessage() and LPARAM

Pointer error with SendMessage() and LPARAM

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingquestion
9 Posts 6 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.
  • J Offline
    J Offline
    Johann Gerell
    wrote on last edited by
    #1

    Hi, We have a problem with corrupted addresses when sending a pointer in the LPARAM argument of ::SendMessage() to another application. Roughly, the code looks like this:

    TCHAR g_szName[] = _T("abcd");

    VOID SendName(HWND hWnd)
    {
    ::SendMessage(hWnd, WM_USER, 0, (LPARAM)g_szName);
    }

    In the debugger, we can have g_szName equal to for instance 0x0e006719 before the ::SendMessage() call. But in the receiving application the higher bits are cleared so we only have 0x00006719 there, which isn't likely to contain the string we want to use... Any idea why this is? I can assure you it's annoying and it feels like I've done exactly this successfully at least a thousand times before, not to mention WM_SETTEXT, which is practically the same, only it works... I appreciate any pointers ;) on this subject. Thanks! /Johann Gerell

    P R Z 3 Replies Last reply
    0
    • J Johann Gerell

      Hi, We have a problem with corrupted addresses when sending a pointer in the LPARAM argument of ::SendMessage() to another application. Roughly, the code looks like this:

      TCHAR g_szName[] = _T("abcd");

      VOID SendName(HWND hWnd)
      {
      ::SendMessage(hWnd, WM_USER, 0, (LPARAM)g_szName);
      }

      In the debugger, we can have g_szName equal to for instance 0x0e006719 before the ::SendMessage() call. But in the receiving application the higher bits are cleared so we only have 0x00006719 there, which isn't likely to contain the string we want to use... Any idea why this is? I can assure you it's annoying and it feels like I've done exactly this successfully at least a thousand times before, not to mention WM_SETTEXT, which is practically the same, only it works... I appreciate any pointers ;) on this subject. Thanks! /Johann Gerell

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      Pointers do not generally marshall across process boundaries. You will need to allocate memory using the GlobalAlloc function in order to pass the data between your two applications. WM_SETTEXT has some special processing in it to marshall your pointers for you. I think the same goes with WM_GETTEXT and possibly a few other messages.


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      J M 2 Replies Last reply
      0
      • J Johann Gerell

        Hi, We have a problem with corrupted addresses when sending a pointer in the LPARAM argument of ::SendMessage() to another application. Roughly, the code looks like this:

        TCHAR g_szName[] = _T("abcd");

        VOID SendName(HWND hWnd)
        {
        ::SendMessage(hWnd, WM_USER, 0, (LPARAM)g_szName);
        }

        In the debugger, we can have g_szName equal to for instance 0x0e006719 before the ::SendMessage() call. But in the receiving application the higher bits are cleared so we only have 0x00006719 there, which isn't likely to contain the string we want to use... Any idea why this is? I can assure you it's annoying and it feels like I've done exactly this successfully at least a thousand times before, not to mention WM_SETTEXT, which is practically the same, only it works... I appreciate any pointers ;) on this subject. Thanks! /Johann Gerell

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        Use WM_COPYDATA to send data between applications. /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com

        J 1 Reply Last reply
        0
        • P Paul M Watt

          Pointers do not generally marshall across process boundaries. You will need to allocate memory using the GlobalAlloc function in order to pass the data between your two applications. WM_SETTEXT has some special processing in it to marshall your pointers for you. I think the same goes with WM_GETTEXT and possibly a few other messages.


          Build a man a fire, and he will be warm for a day
          Light a man on fire, and he will be warm for the rest of his life!

          J Offline
          J Offline
          Johann Gerell
          wrote on last edited by
          #4

          Paul Watt wrote: You will need to allocate memory using the GlobalAlloc function in order to pass the data between your two applications. I can't with pride use a function documented as being there to provide compatibility for 16 bits apps. And, are you really sure it does what it promises? Since Win32 doesn't have separate local and global heaps (only local, right?), how could that function allocate global heap? Pointers do not generally marshall across process boundaries. Yes, of course - I know that. Normally. But last night/morning at 5.30 AM, after long hours preparing for a first press demo of our product some hours later, neither me or my co-worker accepted this reason (happens way too often). I told him (with an enlightened smile) "...but each process has its own address space and pointers from one doesn't translate to the other." Upon which he (of course) replied "Of course." But did we accept that? No. At that hour without any sleep, things should just work... We already use WM_COPYDATA for IPC and that's the way to here also, but this morning I could have sworn there were no such thing as WM_COPYDATA... Thanks anyway! /Johann Gerell

          T 1 Reply Last reply
          0
          • R Ravi Bhavnani

            Use WM_COPYDATA to send data between applications. /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com

            J Offline
            J Offline
            Johann Gerell
            wrote on last edited by
            #5

            Definately. That's what we've been doing and should without question continue to do. Early morning hours can do much damage to ones brain. Thanks for the input! /Johann Gerell

            R 1 Reply Last reply
            0
            • P Paul M Watt

              Pointers do not generally marshall across process boundaries. You will need to allocate memory using the GlobalAlloc function in order to pass the data between your two applications. WM_SETTEXT has some special processing in it to marshall your pointers for you. I think the same goes with WM_GETTEXT and possibly a few other messages.


              Build a man a fire, and he will be warm for a day
              Light a man on fire, and he will be warm for the rest of his life!

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              Paul Watt wrote: You will need to allocate memory using the GlobalAlloc function in order to pass the data between your two applications. That is incorrect. The local/global distinction is a holdover from Win16. From MSDN:

              Memory objects allocated by GlobalAlloc and LocalAlloc are in private, committed pages with read/write access that cannot be accessed by other processes. Memory allocated by using GlobalAlloc with GMEM_DDESHARE is not actually shared globally as it is in 16-bit Windows. This value has no effect and is available only for compatibility. Applications requiring shared memory for other purposes must use file-mapping objects.

              I wish it were still as easy as calling GlobalAlloc. ;) --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

              1 Reply Last reply
              0
              • J Johann Gerell

                Hi, We have a problem with corrupted addresses when sending a pointer in the LPARAM argument of ::SendMessage() to another application. Roughly, the code looks like this:

                TCHAR g_szName[] = _T("abcd");

                VOID SendName(HWND hWnd)
                {
                ::SendMessage(hWnd, WM_USER, 0, (LPARAM)g_szName);
                }

                In the debugger, we can have g_szName equal to for instance 0x0e006719 before the ::SendMessage() call. But in the receiving application the higher bits are cleared so we only have 0x00006719 there, which isn't likely to contain the string we want to use... Any idea why this is? I can assure you it's annoying and it feels like I've done exactly this successfully at least a thousand times before, not to mention WM_SETTEXT, which is practically the same, only it works... I appreciate any pointers ;) on this subject. Thanks! /Johann Gerell

                Z Offline
                Z Offline
                zarzor
                wrote on last edited by
                #7

                What? Data between two applications? This means inter-communication, so use Dynamic Data Exchange (DDE). :rolleyes: ZARZOR13

                1 Reply Last reply
                0
                • J Johann Gerell

                  Paul Watt wrote: You will need to allocate memory using the GlobalAlloc function in order to pass the data between your two applications. I can't with pride use a function documented as being there to provide compatibility for 16 bits apps. And, are you really sure it does what it promises? Since Win32 doesn't have separate local and global heaps (only local, right?), how could that function allocate global heap? Pointers do not generally marshall across process boundaries. Yes, of course - I know that. Normally. But last night/morning at 5.30 AM, after long hours preparing for a first press demo of our product some hours later, neither me or my co-worker accepted this reason (happens way too often). I told him (with an enlightened smile) "...but each process has its own address space and pointers from one doesn't translate to the other." Upon which he (of course) replied "Of course." But did we accept that? No. At that hour without any sleep, things should just work... We already use WM_COPYDATA for IPC and that's the way to here also, but this morning I could have sworn there were no such thing as WM_COPYDATA... Thanks anyway! /Johann Gerell

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  I love reason messages like this. Every programmer worth his weight in salt has done stuff like this. It brings back so many "happy" memories. :laugh: Tim Smith I'm going to patent thought. I have yet to see any prior art.

                  1 Reply Last reply
                  0
                  • J Johann Gerell

                    Definately. That's what we've been doing and should without question continue to do. Early morning hours can do much damage to ones brain. Thanks for the input! /Johann Gerell

                    R Offline
                    R Offline
                    Ravi Bhavnani
                    wrote on last edited by
                    #9

                    Johann Gerell wrote: Early morning hours can do much damage to ones brain. :) I couldn't agree more. Happens to me at least 5 times a day! /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com

                    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