Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why do this message not go through?
[DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, uint wMsg, int wParam, int lParam);

IntPtr PID = FindWindow(CLASSNAME,WINNAME);
                    SendMessage(PID, 0x0002, 0,0);

I am trying to send this message to an already open app.

Handle is correct according to spy++
SendMessage returns 0

What I have tried:

I've used Spy++ and noticed that it wasn't going through and the app want closing either. I've tried other message numbers(which is what I actually want to do but this is a good example). I also got the numbers from Spy++, so what am I doing wrong? Is there a way to send this to an app that is already open(Like terminating google chrome)?

I've also tried using PostMessage
Posted
Updated 14-May-23 4:24am
v8
Comments
Peter_in_2780 14-May-23 2:21am    
Two obvious questions:
1. Does FindWindow return a good window handle to PID?
2. What is the return value from SendMessage? That will probably tell you what your error is.

1 solution

You have your parameters the wrong way round in your call to FindWindow function (winuser.h) - Win32 apps | Microsoft Learn[^]. As a result it is likely that the return value was not a Windows handle; use the debugger to check.
 
Share this answer
 
Comments
G Schulz 14-May-23 10:15am    
Whoops sorry, I just put it in wrong here, in my code it is correct, and I have checked with Spy++ to see that the correct handle is given. Also SendMessage returns 0 so I looked up how to fix that and it didn't help me much, Whats going on if the handle is correct? Is there a way to fix it?
Richard MacCutchan 14-May-23 10:28am    
If you use Copy & Paste, you can be certain that we see exactly what you have coded. As to the result of SendMessage function (winuser.h) - Win32 apps | Microsoft Learn[^], you need to check the message that you are sending to see what value it returns. In most cases you should send WM_CLOSE (0x0010) to tell an application to terminate.
G Schulz 14-May-23 10:39am    
I've changed it to 0x0010, but SendMessage still returns 0 and nothing happens.
Richard MacCutchan 14-May-23 10:48am    
Can you updtate your question with the complete code that you are using?
G Schulz 14-May-23 10:49am    
I fixed it, you were right, In a different section of my code I have the parameters switched. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900