Hi guys,

I need to create two separate applications in C++ , where the first application
is a window , and the second application has a button and it needs to close the first

Both applications must be two separate programs !

I can't use FindWindow() therefore , I don't know how to find the first window.
how can I find it without FindWindow() ? maybe look for the .EXE file ?

*I've written the first program for the window .
*I've written the second program and created a button , but I don't know
how to "find" the the first window and close it . I think I need to use
"PostQuitMessage( 0 ) ;" but I'm not sure .

Can you please explain ?

Perhaps take a look at this:

Discusses getting the PID's of any running instances of a particular program.

There is a post in the comments about closing process here:

Might be what you're looking for!

My friend , I don't understand how do I use the function "RegisterWindowMessage" in order to locate the windows that I want to close .

I'll give you the code that does it using FindWindow:

The following code is inside to application that wants to close the other application , and
it works fine , the thing is that I need to do to it without the "pHwnd = FindWindow("TestWin",NULL);" line

HWND pHwnd;
           pHwnd = FindWindow("TestWin",NULL);

The application that I want to close called "TestWin" .
Can you please explain to me how to do that without "pHwnd = FindWindow("TestWin",NULL);"

thanks a lot

You asked for a way of closing another window without using FindWindow and I posted a couple of links to code that would help you to achieve this. The first link shows you how to get the handles/PIDs of all running processes with a particular name. The second link shows some code which will shut down a named process.

So in relation to your program; when the button is clicked, you could get the process ID's of any running instances of "TestWin" and then close them down. All you'd need to do is take a look at the code in the links and then write something similar to meet your needs.

And regarding RegisterWindowMessage, you made no reference to this in your original post. I hadn't considered passing Windows messages, but now that you mention it; it sounds like a more elegant solution than attempting to find and close named processes as I suggested!

However, I've not done anything with the Windows API or MFC for a long time now, and I've never had the need to mess around with custom windows messages either. So I'm not sure I can be of much help.

Taking a quick look at MSDN it looks like you need to do something like this:
1. Create a new/custom Windows message using RegisterWindowsMessage
2. When the button is clicked in your main application, it sends the relevant windows message using SendMessage passing the HWND_BROADCAST handle to broadcast the message to all top-level windows.
3. The 2nd application "TestWin" will need handlers set up for the custom message (either in the message map if using MFC, or in the message loop of your winmain if using Win API). So when it recieves the custom message, it shuts down.

I haven't got the time to dig too deep on this ATM, but I'm sure somebody else here will be able to help further.

Otherwise, you'll have to take a look at MSDN and work it out for yourself:

Sorry I can't be of any more help than that ATM!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.