Close IE programmatically

This is a discussion on Close IE programmatically within the Windows Programming forums, part of the Platform Specific Boards category; I am able to open a website with IE using the ShellExecute function but after doing some stuff, I need ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Close IE programmatically

    I am able to open a website with IE using the ShellExecute function but after doing some stuff, I need to close it. How can I do that programmatically? I tried passing the verb "close" to ShellExecute but it does not work. Thank you.

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    My suggestion is to find its process and close it..

    See process and threads at msdn
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    First try sending the IE window a WM_CLOSE message. If that doesn't work, then call TerminateProcess().

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    This was frustrating the hell out of me for a good long while. This is what I just came up with. I used ShellExecuteEx to retrieve extended information about the application, enumerated windows with Find/GetWindow, and closed it using WM_SYSCOMMAND and SC_CLOSE as the param. This was my "homework" as much as yours, needed to be done! Sorry if it seems like a spoiler

    Code:
    	SHELLEXECUTEINFO eI;	
    	DWORD pIdExec, pTId;
    	HWND hWnd;
    	
        
    	ZeroMemory(&eI, sizeof(eI));
    	eI.cbSize       = sizeof(eI);           
    	eI.lpVerb       = "open";
    	eI.lpFile       = "iexplore";
    	eI.nShow        = SW_SHOW;	
    	eI.fMask		= SEE_MASK_NOCLOSEPROCESS;
        
    	if(ShellExecuteEx(&eI) != FALSE)
    	{
    		WaitForSingleObject(eI.hProcess, 1000);
    
    		hWnd = FindWindow(NULL, 0);
    		while(hWnd != NULL)
    		{
    			GetWindowThreadProcessId(hWnd, &pTId);
    			pIdExec = GetProcessId(eI.hProcess);
    
    			if(pTId == pIdExec)
    			{
    				PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
    			}
    			hWnd = GetWindow(hWnd, GW_HWNDNEXT);
    			
    		}
    	}
    There are probably much much easier ways.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Tonto, thanks a lot man!

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Wouldn't this have worked?
    Code:
    HWND hwnd = FindWindow("IEFrame",0);
    PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
    [QUOTE=bithub]
    First try sending the IE window a WM_CLOSE message. If that doesn't work, then call TerminateProcess().
    [QUOTE]
    WM_CLOSE would not have worked.

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I would think that would risk closing an unwanted internet explorer window that is out of your posession. You could find all the IE windows, GetWindowThreadProcessId each, make sure it's the same, and then skip going through every top-level window. You could also use EnumWindows and the callback instead of Find/GetWindow enumeration. Whatever. Glad it was of some help anyways

    >> WM_CLOSE would not have worked.

    Yes, that is what I experienced too, kinda wierd.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Cool-August, that was exactly what I used
    I know about the Spy++ tool that comes with Visual C++

    Speaking of that, I have another (big) problem. That a look at this picture, please:
    http://www.geocities.com/caduardo21/ie.JPG

    How would I go about getting a handle to the highlighted "window"? Thanks.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I tried this without success
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(void)
    {
        ShellExecute(NULL, "open", "www.google.com", NULL, NULL, SW_MAXIMIZE);
        HWND IEHwnd = FindWindow("IEFrame", NULL);
        
        HWND hwnd = FindWindowEx(IEHwnd, NULL, "Shell DocObject View", NULL);
        if (!hwnd)
            printf("blablabla hwnd\n");
    
        HWND hwnd1 = FindWindowEx(IEHwnd, NULL, "Internet Explorer_Server", NULL);
        if (!hwnd1)
            printf("blablabla hwnd1\n");
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programmatically declare vectors
    By franse in forum C++ Programming
    Replies: 15
    Last Post: 11-10-2008, 03:17 PM
  2. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-24-2008, 12:00 AM
  3. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 02:16 PM
  4. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 10:39 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21