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 Function (Sub in C#)

SendMessage Function (Sub in C#)

Scheduled Pinned Locked Moved C#
csharpcomtoolsquestion
14 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.
  • P Programm3r

    Hi all, What can I use within C# to replace the function:LRESULT SendMessage( HWND hWnd, // handle of destination window UINT Msg, // message to send WPARAM wParam, // first message parameter LPARAM lParam // second message parameter );
    Many Thanks in advance Regards,


    The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

    P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #2

    OK, so I'm using PInvoke - doing the following:[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    But I'm receiving the following error:

    Error 2 The modifier 'extern' is not valid for this item
    Error 3 Expected class, delegate, enum, interface, or struct

    Why ??? :confused::confused:


    The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

    W L 2 Replies Last reply
    0
    • P Programm3r

      OK, so I'm using PInvoke - doing the following:[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
      But I'm receiving the following error:

      Error 2 The modifier 'extern' is not valid for this item
      Error 3 Expected class, delegate, enum, interface, or struct

      Why ??? :confused::confused:


      The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

      W Offline
      W Offline
      wienzzz
      wrote on last edited by
      #3

      where did you find "HandleRef"? that's why the compiler expected class, delegate, enum, interface, etc I think.. try the code below.. I have no idea how to use it because it only return the message ID which I don't know how to process.. [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); CMIIW

      Mail me at erwin@holyknight.us

      P 1 Reply Last reply
      0
      • W wienzzz

        where did you find "HandleRef"? that's why the compiler expected class, delegate, enum, interface, etc I think.. try the code below.. I have no idea how to use it because it only return the message ID which I don't know how to process.. [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); CMIIW

        Mail me at erwin@holyknight.us

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #4

        Where do I place it. At the moment I placed it here (is this correct?... using System.IO; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); namespace WindowsService { ...
        Many thanks for the reply Regards,


        The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

        1 Reply Last reply
        0
        • P Programm3r

          OK, so I'm using PInvoke - doing the following:[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
          But I'm receiving the following error:

          Error 2 The modifier 'extern' is not valid for this item
          Error 3 Expected class, delegate, enum, interface, or struct

          Why ??? :confused::confused:


          The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #5

          Hi,

          Programm3r wrote:

          [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

          you have to put these lines inside a class, any class would do (I use LP_user32 for all user32.dll functions). :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          P W 2 Replies Last reply
          0
          • L Luc Pattyn

            Hi,

            Programm3r wrote:

            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

            you have to put these lines inside a class, any class would do (I use LP_user32 for all user32.dll functions). :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            W Offline
            W Offline
            wienzzz
            wrote on last edited by
            #6

            Yep, put it inside a class.. search in microsoft documentation for more info about windows messaging, the message type, and other info.. CMIIW

            Mail me at erwin@holyknight.us

            1 Reply Last reply
            0
            • L Luc Pattyn

              Hi,

              Programm3r wrote:

              [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

              you have to put these lines inside a class, any class would do (I use LP_user32 for all user32.dll functions). :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              P Offline
              P Offline
              Programm3r
              wrote on last edited by
              #7

              Ok, I'm trying this .... class WindowsService : ServiceBase { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
              But I receive this: Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)


              The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

              M G L 3 Replies Last reply
              0
              • P Programm3r

                Ok, I'm trying this .... class WindowsService : ServiceBase { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
                But I receive this: Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)


                The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #8

                using System.Runtime.InteropServices;

                All the best, Martin

                P 1 Reply Last reply
                0
                • P Programm3r

                  Ok, I'm trying this .... class WindowsService : ServiceBase { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
                  But I receive this: Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)


                  The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  Visual Studio is kind enough to include the probable fix in the error message. MSDN will tell you which namespace DllImport belongs to. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                  1 Reply Last reply
                  0
                  • P Programm3r

                    Ok, I'm trying this .... class WindowsService : ServiceBase { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
                    But I receive this: Error 1 The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)


                    The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                    G Offline
                    G Offline
                    Giorgi Dalakishvili
                    wrote on last edited by
                    #10

                    Add this line to your using directives list: using System.Runtime.InteropServices;

                    #region signature my articles #endregion

                    P 1 Reply Last reply
                    0
                    • M Martin 0

                      using System.Runtime.InteropServices;

                      All the best, Martin

                      P Offline
                      P Offline
                      Programm3r
                      wrote on last edited by
                      #11

                      Thanks :)


                      The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                      1 Reply Last reply
                      0
                      • G Giorgi Dalakishvili

                        Add this line to your using directives list: using System.Runtime.InteropServices;

                        #region signature my articles #endregion

                        P Offline
                        P Offline
                        Programm3r
                        wrote on last edited by
                        #12

                        Thanks :)


                        The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                        1 Reply Last reply
                        0
                        • P Programm3r

                          Hi all, What can I use within C# to replace the function:LRESULT SendMessage( HWND hWnd, // handle of destination window UINT Msg, // message to send WPARAM wParam, // first message parameter LPARAM lParam // second message parameter );
                          Many Thanks in advance Regards,


                          The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                          P Offline
                          P Offline
                          Programm3r
                          wrote on last edited by
                          #13

                          Is it possible to show a MessageBox from a Service developed in C# ? Many Thanks Regards,


                          The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                          B 1 Reply Last reply
                          0
                          • P Programm3r

                            Is it possible to show a MessageBox from a Service developed in C# ? Many Thanks Regards,


                            The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                            B Offline
                            B Offline
                            Big Daddy Farang
                            wrote on last edited by
                            #14

                            When you say "Service" do you mean that your C# program is running as a Windows Service? That's my assumption here, and yes it's possible. Whether or not it's advisable is another matter. Now when I say "possible" I mean that the code will compile and build, and the .exe can be installed as a Windows Service and run. Often times one of the main reasons to develop a solution to run as a Windows Service is so that it can run even when no user is logged in to the computer. Now if that's the case, and the code finds it way down the path to where the MessageBox is .Show()'n, I don't know what will happen. Maybe nothing. Maybe it's displayed and causes the Service to stop executing until the nonexistent user clicks the unseen "OK" button. Of course you could try it and see what happens. If you do, post your results here so everyone can know, too. Then try to find out if a tree falls in the forest.... ;) BDF

                            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