none
SendMessage p\Invoke RRS feed

  • Question

  • I am porting an old utility from C++ to C#.  The application needs to be able to pass commands to 3rd party application and does so via SendMessage.  I know how to do that with p\Invoke, but my question is this ... 

    Now that we have VS 2017 and our newest versions of C# and the .NET Framework, do I still have to use p\Invoke to call SendMessage in order to pass a command to a running application?  Or do we finally have this ability wrapped somewhere in the framework?

    Thanks in advance for the insights!!

    Wednesday, September 27, 2017 5:57 AM

Answers

All replies

  • Here is example code using PostMessage() but SendMessage() works similarly.

    IMO, PostMessage() usually is preferred because it'll not block your application, but it really depends on whether you want your application to wait.

    Wednesday, September 27, 2017 6:14 AM
    Answerer
  • Thanks for the response, but I am asking if there is a way to do it without using p\Invoke.  I already know how to use SendMessage just fine, I was trying to keep it all in managed code if possible.

    Wednesday, September 27, 2017 6:26 AM
  • Nothing has changed. P/Invoke is the easiest way to call Win32 functions. 

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Proposed as answer by cheong00Editor Thursday, September 28, 2017 2:41 AM
    • Marked as answer by cheong00Editor Monday, October 9, 2017 1:53 AM
    Wednesday, September 27, 2017 1:57 PM
    Moderator
  • The main difference between the p/Invoke (I am using your terminology) version of SendMessage and a managed version is the window handle.

    See the NativeWindow Class. You can use the AssignHandle(IntPtr) member to set the handle. I don't see a SendMessage member so you would need to derive a class from the class. If you only want to do this for SendMessage then I think you will agree that it is not worth it. The NativeWindow class is good if you want to do more, such as process messages sent to the other window. See the following.

    How to subclass Windows in Windows Forms by using Visual C# .NET or Visual C# 2005

    Subclassing in .NET - The pure .NET way - CodeProject



    Sam Hobbs
    SimpleSamples.Info


    Wednesday, September 27, 2017 8:24 PM
  • Once you have the overrided NativeWindow class like what you see in the example below the documentation, I think it's possible to just expose public method that calls .WinProc(ref Message) to send crafted message to the window. Since it requires you call the base.WinProc() inside the overridden method, I think unhandled message will probably to back to the window.


    Thursday, September 28, 2017 1:04 AM
    Answerer
  • Once you have the overrided NativeWindow class like what you see in the example below the documentation, I think it's possible to just expose public method that calls .WinProc(ref Message) to send crafted message to the window. Since it requires you call the base.WinProc() inside the overridden method, I think unhandled message will probably to back to the window.


    I think not. For one thing the message often needs to be executed in the thread that created the window. In this case, the window is in a different process. Calling the window procedure in the NativeWindow class would stay in your process and that would not have access to the other process where the data needs to be processed.

    I have never seen a case where the window procedure (WinProc) is called directly by an application. Messages always go through the message loop. For you and everyone else familiar with C# but unfamiliar with C++ and/or the way the Windows API works see my Windows API Window Using C#; it is for educational purposes only but you can use C# to get an idea of how a C++ program uses the Windows API.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, September 28, 2017 1:48 AM
  • Yup. I've done quick experiment on sending WM_PASTE to notepad and without success. Both attempt on trying to call base.WinProc() and base.DefWinProc() fails without error. I guess the message is not passed back to the window then.

    And back to the main topic, even if you use the C# native Form object, the calls to various method is still done by P/Invoking SendMessage() so it's hard to imagine if there will be more efficient way.

    Thursday, September 28, 2017 2:19 AM
    Answerer
  • Any .Net class that uses SendMessage would do the same thing as calling SendMessage with p/invoke except the member method would get the handle from a member of its class.


    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by cheong00Editor Thursday, September 28, 2017 2:41 AM
    • Marked as answer by cheong00Editor Monday, October 9, 2017 1:53 AM
    Thursday, September 28, 2017 2:38 AM