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#
  4. Marshaling Object

Marshaling Object

Scheduled Pinned Locked Moved C#
question
3 Posts 2 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.
  • A Offline
    A Offline
    Anh_Tuan
    wrote on last edited by
    #1

    Hi, I have applications that need to communicate with each other using HWND_BROADCAST Here is the prototype for unmanged code function SendMessage [DllImport("user32.dll")] extern static IntPtr SendMessage(IntPtr hWnd, int msg, MyClass myClass, IntPtr lParam); where WParam is passed as an object reference. In the overriden WndProc method on the other application, the WParam variable is marshaled back to the original object like this protected override void WndProc(ref Message m) { if (m.Msg == CommonMessageID) { ... // myClass contains junk data here MyClass myClass = (MyClass)Marshal.PtrToStructure(m.WParam, typeof(MyClass)); ... } base.WndProc(ref m); } However, it does not hold the information that was initialized by the other app. What did I do wrong here? Jup

    M 1 Reply Last reply
    0
    • A Anh_Tuan

      Hi, I have applications that need to communicate with each other using HWND_BROADCAST Here is the prototype for unmanged code function SendMessage [DllImport("user32.dll")] extern static IntPtr SendMessage(IntPtr hWnd, int msg, MyClass myClass, IntPtr lParam); where WParam is passed as an object reference. In the overriden WndProc method on the other application, the WParam variable is marshaled back to the original object like this protected override void WndProc(ref Message m) { if (m.Msg == CommonMessageID) { ... // myClass contains junk data here MyClass myClass = (MyClass)Marshal.PtrToStructure(m.WParam, typeof(MyClass)); ... } base.WndProc(ref m); } However, it does not hold the information that was initialized by the other app. What did I do wrong here? Jup

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Hi! You cannot simply stuff an object into an arbitrary SendMessage call and expect it to appear unchanged at another application. SendMessage is sending pointers as parameters (wPar, lPar), but these pointers point to an adress in the current process adress space! You can transmit simple datatypes (int) that way but no objects. In Win32 there's a special message WM_COPYDATA that's used to transfer data from one process to another, but I strongly doubt that this will solve your problem. For transfering objects (or rather object references) you should use .NET Remoting. Regards, mav -- Black holes are the places where god divided by 0...

      A 1 Reply Last reply
      0
      • M mav northwind

        Hi! You cannot simply stuff an object into an arbitrary SendMessage call and expect it to appear unchanged at another application. SendMessage is sending pointers as parameters (wPar, lPar), but these pointers point to an adress in the current process adress space! You can transmit simple datatypes (int) that way but no objects. In Win32 there's a special message WM_COPYDATA that's used to transfer data from one process to another, but I strongly doubt that this will solve your problem. For transfering objects (or rather object references) you should use .NET Remoting. Regards, mav -- Black holes are the places where god divided by 0...

        A Offline
        A Offline
        Anh_Tuan
        wrote on last edited by
        #3

        Thanks, you're right. I forgot about address space. Should I access other's process address space to read/write data? Jup

        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