Marshaling Struct for native SendMessage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michelqa

    #1

    Marshaling Struct for native SendMessage

    Hi,

    I need to call a lot of different native SendMessage to retreive
    informations from non managed application.

    Some win32 messages use struct pointer for lparam....how to create and
    marshaling the struct to be able to use it in sendmessage...

    Here is an example LM_GETITEM:
    http://msdn.microsoft.com/en-us/libr...20(VS.85).aspx
    ---------------------------------------------------------

    [DllImport("user 32.dll")]
    public static extern IntPtr SendMessage(Int Ptr hwnd, int msg, IntPtr
    wparam, IntPtr lparam);
    //...
    [StructLayout(La youtKind.Sequen tial)]
    public struct LITEM
    {
    uint mask;
    int iLink;
    uint state;
    uint stateMask;
    [MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
    string szID; //??? is it equivalent to WCHAR ?//
    [MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
    string szUrl; //??? is it equivalent to WCHAR ?//
    }

    LITEM MyLItem = new LITEM();
    int MsgResult = SendMessage(Han dle,LM_GETITEM, IntPtr.Zero,ref
    MyLItem); //<----- ???????

    I know the information in the struct must be set before calling
    sendmessage...t he code is just an incomplete example, but how to
    create and marshaling a valid struct to call sendmessage in this
    case? Is it possible when the Handle is in a different process??

    Thanks for any help and example...
  • Pavel Minaev

    #2
    Re: Marshaling Struct for native SendMessage

    On Jul 13, 1:10 pm, michelqa <miche...@yahoo .cawrote:
    Hi,
    >
       I need to call a lot of different native SendMessage to retreive
    informations from non managed application.
    >
    Some win32 messages use struct pointer for lparam....how to create and
    marshaling the struct to be able to use it in sendmessage...
    >
    Here is an example LM_GETITEM:http://msdn.microsoft.com/en-us/libr...20(VS.85).aspx
    ---------------------------------------------------------
    >
    [DllImport("user 32.dll")]
    public static extern IntPtr SendMessage(Int Ptr hwnd, int msg, IntPtr
    wparam, IntPtr lparam);
    //...
    [StructLayout(La youtKind.Sequen tial)]
    public struct LITEM
    {
            uint mask;
            int iLink;
            uint state;
            uint stateMask;
            [MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
            string szID;    //??? is it equivalent to WCHAR  ?//
            [MarshalAs(Unman agedType.ByValT Str, SizeConst = 260)]
            string szUrl;   //??? is it equivalent to WCHAR  ?//
    >
    }
    >
    LITEM MyLItem = new LITEM();
    int MsgResult = SendMessage(Han dle,LM_GETITEM, IntPtr.Zero,ref
    MyLItem);   //<----- ???????
    >
    I know the information in the struct must be set before calling
    sendmessage...t he code is just an incomplete example, but how to
    create and marshaling a valid struct to call sendmessage in this
    case?
    Consider using Marshal.Structu reToPtr() method. You'll need to
    allocate a memory block of proper size first, then call StructureToPtr
    to marshal your structure into that block in the appropriate format,
    and then you can pass the address of that block as IntPtr to
    SendMessage.
    Is it possible when the Handle is in a different process??
    It is, though it has no relevance to P/Invoke marshalling - it's the
    SendMessage function itself that checks whether a handle belongs to a
    different process, and does any further necessary marshalling to
    deliver it properly. However, this SendMessage feature only works for
    Win32 predefined message types (with codes less than WM_USER), not for
    any custom messages.

    Comment

    • michelqa

      #3
      Re: Marshaling Struct for native SendMessage

      Thanks for your help... I really need help on this :(

      Ok now my code look like this... but sendMessage return 0
      - Is anybody can confirm that the code look correct (for passing a
      structure pointer to sendmessage??)
      - After calling sendmessage how can I access to the structure
      information returned by MyLItemPtr???
      - Since LM_GETITEM is WM_USER +771.... I suspect I need other
      marshaling to get the structure back with the information??

      //----------- BEGIN CODE -------------//
      [DllImport("user 32.dll")]
      public static extern IntPtr SendMessage(Int Ptr hwnd, int msg, IntPtr
      wparam, IntPtr lparam);
      //...
      int WM_USER =1024,
      int LM_GETITEM = (WM_USER + 0x303)
      int LIF_ITEMINDEX = 1;
      int LIF_URL = 8;

      //..
      [StructLayout(La youtKind.Sequen tial)]
      public struct LITEM
      {
      uint mask;
      int iLink;
      uint state;
      uint stateMask;
      [MarshalAs(Unman agedType.ByValT Str, SizeConst = 48)]
      string szID; //??? is it equivalent to WCHAR ?//
      [MarshalAs(Unman agedType.ByValT Str, SizeConst = 2084)]
      string szUrl; //??? is it equivalent to WCHAR ?//
      }


      LITEM MyLItem = new LITEM();
      MyLItem.mask=(u int)LIF_ITEMID | (uint)LIF_URL; //must use these
      masks to get the url
      MyLItem.iLink=0 ;
      IntPtr MyLItemPtr = Marshal.AllocHG lobal(Marshal.S izeOf(MyLItem)) ;
      Marshal.Structu reToPtr(MyLItem , MyLItemPtr, false);
      IntPtr MsgResult =
      Win32.SendMessa ge(Handle,LM_GE TITEM,IntPtr.Ze ro,MyLItemPtr);
      MessageBox.Show (MsgResult.ToSt ring()); //this return 0 :(
      Marshal.FreeHGl obal(MyLItemPtr );
      //----------- END CODE -------------//

      Thanks again

      Comment

      • michelqa

        #4
        Re: Marshaling Struct for native SendMessage

        Still playing with the code all day without any success :(

        I desperately need help

        Comment

        • Peter Duniho

          #5
          Re: Marshaling Struct for native SendMessage

          On Sun, 13 Jul 2008 22:32:31 -0700, michelqa <michelqa@yahoo .cawrote:
          Still playing with the code all day without any success :(
          >
          I desperately need help
          For what it's worth, there is a newsgroup specifically dedicated to
          interop questions like yours:
          microsoft.publi c.dotnet.framew ork.interop

          You may have better luck getting your question answered there, especially
          if you're in a rush.

          Pete

          Comment

          • michelqa

            #6
            Re: Marshaling Struct for native SendMessage

            Thanks for the suggestion.. I post my problem in this newsgroup also.

            For now I will maybe try to recreate this directly in a win32 project
            to make sure the problem is not a misunderstandin g of the LM_GETITEM
            API....but by looking at the LM_GETITEM in controlSpy my parameters
            seems to be ok. (http://www.microsoft.com/downloads/details.aspx?
            familyid=19d429 4d-0531-4ec2-8b27-2e463b315f16&di splaylang=en)

            Comment

            Working...