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. SendMessage issue

SendMessage issue

Scheduled Pinned Locked Moved C#
helpcsharpjson
8 Posts 3 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
    Aviv Halperin
    wrote on last edited by
    #1

    Maybe I wasnt clear enough in my last thread: I want to use the API function SendMessage in my .NET CF apllication. I use the following code to get the handle of my TextBox window: [DllImport("coredll.dll")] internal extern static IntPtr GetCapture(); this.Capture = true; hwnd = GetCapture(); this.Capture = false; I want to send messages in order to extend the functionality of my TextBox. I use: [DllImport("coredll.dll")] internal extern static int SendMessage(IntPtr Hwnd, int Msg, int WParam, int LParam); SendMessage(this.hwnd, (int)0x0300/*WM_CUT*/, 0, 0); This does not cut the text in my TextBox. I have read all through the .NET CF and could not find the reason for this. Any other p/invoke I use works fine (like GetCapture) but the SendMessage doesnt. Please help... Thanks. avivhal

    M H 2 Replies Last reply
    0
    • A Aviv Halperin

      Maybe I wasnt clear enough in my last thread: I want to use the API function SendMessage in my .NET CF apllication. I use the following code to get the handle of my TextBox window: [DllImport("coredll.dll")] internal extern static IntPtr GetCapture(); this.Capture = true; hwnd = GetCapture(); this.Capture = false; I want to send messages in order to extend the functionality of my TextBox. I use: [DllImport("coredll.dll")] internal extern static int SendMessage(IntPtr Hwnd, int Msg, int WParam, int LParam); SendMessage(this.hwnd, (int)0x0300/*WM_CUT*/, 0, 0); This does not cut the text in my TextBox. I have read all through the .NET CF and could not find the reason for this. Any other p/invoke I use works fine (like GetCapture) but the SendMessage doesnt. Please help... Thanks. avivhal

      M Offline
      M Offline
      Matt Gerrans
      wrote on last edited by
      #2

      Seems kind of convoluted. Are you just trying to move some text from a textbox to the clipboard? Anyway, if you are doing this in an event handler that may be the problem; have you tried PostMessage() instead? Matt Gerrans

      A 1 Reply Last reply
      0
      • M Matt Gerrans

        Seems kind of convoluted. Are you just trying to move some text from a textbox to the clipboard? Anyway, if you are doing this in an event handler that may be the problem; have you tried PostMessage() instead? Matt Gerrans

        A Offline
        A Offline
        Aviv Halperin
        wrote on last edited by
        #3

        Thanks for the answer. I tried PostMessage as well. It does not work. I do not do it in an event handler of a menu. My goal is to create a more robust editor then TextBox(that’s all there is in the .net CF, no RichTextBox). Therefore I build an extended TextBox with cut,copy,paste and more. My other problem is that when I try to add text programmatically: int len = textBoxTerminal.Text.Length; textBoxTerminal.SelectionStart = len; textBoxTerminal.SelectionLength = len; textBoxTerminal.SelectedText = (string)StringToAdd; the caret always positions itself in the beginning of the TextBox's Text. I can reposition it to the end and scroll, but this causes the TextBox to flicker. I thought PostMessage will help solving this, but I cannot test it for the Since PostMesage does not work for me. Hope you have an answer for this... Thanks. avivhal

        1 Reply Last reply
        0
        • A Aviv Halperin

          Maybe I wasnt clear enough in my last thread: I want to use the API function SendMessage in my .NET CF apllication. I use the following code to get the handle of my TextBox window: [DllImport("coredll.dll")] internal extern static IntPtr GetCapture(); this.Capture = true; hwnd = GetCapture(); this.Capture = false; I want to send messages in order to extend the functionality of my TextBox. I use: [DllImport("coredll.dll")] internal extern static int SendMessage(IntPtr Hwnd, int Msg, int WParam, int LParam); SendMessage(this.hwnd, (int)0x0300/*WM_CUT*/, 0, 0); This does not cut the text in my TextBox. I have read all through the .NET CF and could not find the reason for this. Any other p/invoke I use works fine (like GetCapture) but the SendMessage doesnt. Please help... Thanks. avivhal

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          What is the return code for SendMessage? Check this against the errors in winerror.h. You should also set the DllImportAttribute.CharSet field to CharSet.Auto. There is actually no SendMessage function, but SendMessageA (ANSI) and SendMessageW (Wide-char, or Unicode). The Windows .NET CE platform is actually Unicode, so you could P/Invoke SendMessageW and set DllImportAttribute.EntryPoint to "SendMessageW" (since DllImportAttribute.ExactSpelling is not supported for the .NET CF). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          A 1 Reply Last reply
          0
          • H Heath Stewart

            What is the return code for SendMessage? Check this against the errors in winerror.h. You should also set the DllImportAttribute.CharSet field to CharSet.Auto. There is actually no SendMessage function, but SendMessageA (ANSI) and SendMessageW (Wide-char, or Unicode). The Windows .NET CE platform is actually Unicode, so you could P/Invoke SendMessageW and set DllImportAttribute.EntryPoint to "SendMessageW" (since DllImportAttribute.ExactSpelling is not supported for the .NET CF). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

            A Offline
            A Offline
            Aviv Halperin
            wrote on last edited by
            #5

            Neither DllImportAttribute.CharSet nor SendMessageW worked. I did get it to work though (somehow)... I took the declarations I have of SendMessage(I have a few overloads) : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); and wrapped them in a separate class. Then I called them as this class's static members and it works! I do not understand why would this solve the problem though...? Maybe the problem was that they were members of a TextBox (Form) derived class and this caused congestion problems with the Form’s own messages? I wish I had the OS source code so I could check this;) avivhal

            H 1 Reply Last reply
            0
            • A Aviv Halperin

              Neither DllImportAttribute.CharSet nor SendMessageW worked. I did get it to work though (somehow)... I took the declarations I have of SendMessage(I have a few overloads) : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); and wrapped them in a separate class. Then I called them as this class's static members and it works! I do not understand why would this solve the problem though...? Maybe the problem was that they were members of a TextBox (Form) derived class and this caused congestion problems with the Form’s own messages? I wish I had the OS source code so I could check this;) avivhal

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              How were you doing it before? C# does not support functions - only methods. They would've had to have been members of a class before or else you couldn't have even compiled your source file. BTW - those last two parameters are process-dependent types and should be IntPtr. If you're needing to pass pointers to structs or char arrays (strings), you can define overloads that would amount to a pointer, such as simply string or ref MyStruct. int is an Int32 in C# and will always be 32 bits. If you ever hope to port this - whether the source or binary - you'll need to change it anyway. External methods must always be static, too, but you declared them statically before, did you not? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

              A 1 Reply Last reply
              0
              • H Heath Stewart

                How were you doing it before? C# does not support functions - only methods. They would've had to have been members of a class before or else you couldn't have even compiled your source file. BTW - those last two parameters are process-dependent types and should be IntPtr. If you're needing to pass pointers to structs or char arrays (strings), you can define overloads that would amount to a pointer, such as simply string or ref MyStruct. int is an Int32 in C# and will always be 32 bits. If you ever hope to port this - whether the source or binary - you'll need to change it anyway. External methods must always be static, too, but you declared them statically before, did you not? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

                A Offline
                A Offline
                Aviv Halperin
                wrote on last edited by
                #7

                They were static members of the my TextBoxEx class before. All I did was build a new class and pass the methods to be static members of the new class. then call them as the new class methods. e.g. if my old class was Aclass and the new Bclass then in Aclass I call: Bclass.SendMessage(...); I have a few overloads such as [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, String lParam); [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, byte[] lParam); that take addresses (pointers). I only use : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); to call with 0 lparam and wparam thus: Bclass.SendMessage(this.Handle, (int)ClipboardMessage.WM_UNDO, 0, 0); I probably should use 2 pointers (IntPtr) instead and lock the IntPtr for the GC before calling this method? Thanks, Aviv. avivhal

                H 1 Reply Last reply
                0
                • A Aviv Halperin

                  They were static members of the my TextBoxEx class before. All I did was build a new class and pass the methods to be static members of the new class. then call them as the new class methods. e.g. if my old class was Aclass and the new Bclass then in Aclass I call: Bclass.SendMessage(...); I have a few overloads such as [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, String lParam); [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, byte[] lParam); that take addresses (pointers). I only use : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); to call with 0 lparam and wparam thus: Bclass.SendMessage(this.Handle, (int)ClipboardMessage.WM_UNDO, 0, 0); I probably should use 2 pointers (IntPtr) instead and lock the IntPtr for the GC before calling this method? Thanks, Aviv. avivhal

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  On what class they're defined shouldn't matter, but perhaps some of your overloads were conflicting. With value types - like int - you do not need to pin them (see the GCHandle class, BTW). Reference types may be moved by the GC on the heap, but it depends how the message handler uses the data; you may not need to pin the memory. Strings are reference types but this data is typically used synchronously with SendMessage (PostMessage, on the other hand, is a different story). When passing addresses of reference objects (as IntPtr, or just a GCHandle which works, too, as well as a HandleRef) you should pin them. For robust code, never use fixed-size integers for the WPARAM and LPARAM. Always use IntPtr. For 0, just use IntPtr.Zero and to wrap an int or long use new IntPtr(int) or new IntPtr(long), respectively. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

                  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