0

I want to make a program that opens a windows explorer window, waits for 5 seconds, and then closes the window. I've tried the following:

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

void _tmain( int argc, TCHAR *argv[] ) {

  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  ZeroMemory( &si, sizeof(si) );
  si.cb = sizeof(si);
  ZeroMemory( &pi, sizeof(pi) );

  if( argc != 2 ) {
    cout << "Usage: " << argv[0] << "<path>";
    return;
  }

  // Build the command string.
  wstring app = L"explorer.exe ";
  wstring str_command = app + argv[1];
  wchar_t* command = const_cast<wchar_t*>( str_command.c_str() );

  // Open the window. 
  if( !CreateProcess( NULL,   // No module name (use command line)
      command,        // Command line
      NULL,           // Process handle not inheritable
      NULL,           // Thread handle not inheritable
      FALSE,          // Set handle inheritance to FALSE
      0,              // No creation flags
      NULL,           // Use parent's environment block
      NULL,           // Use parent's starting directory 
      &si,            // Pointer to STARTUPINFO structure
      &pi )           // Pointer to PROCESS_INFORMATION structure
  ) {
    cout << "CreateProcess failed: " << GetLastError();
    return;
  }

  cout  <<  "Opened window!" << endl;

  // Wait for it.
  Sleep(5000);

  cout  <<  "Done waiting. Closing... ";

  // Close explorer.
  HANDLE explorer = OpenProcess(PROCESS_TERMINATE, false, pi.dwProcessId);
  if( !explorer ) {
    cout << "OpenProcess failed: " << GetLastError();
    return;
  }
  if( !TerminateProcess( explorer, 0 ) ) {
    cout << "TerminateProcess failed: " << GetLastError();
    return;
  }

  // Close process and thread handles. 
  CloseHandle( explorer );
  CloseHandle( pi.hProcess );
  CloseHandle( pi.hThread );

  cout  <<  "Done.";
}

I get it to open well enough, but I can't get it to close. TerminateProcess fails with error code 5. I've also tried posting a WM_CLOSE message to the window. I get a success value out of that, but the window stays open.

Please help!

aznan
  • 349
  • 1
  • 5
  • 13
  • What's the purpose of having the window open for only 5 seconds? – Cheers and hth. - Alf Aug 05 '12 at 22:57
  • error code is: Permission Denied – sehe Aug 05 '12 at 22:58
  • 2
    Have you tried with a more ordinary application, rather than explorer.exe? I would worry an explorer process might just signal the existing window manager process to create a new window or something like that. – aschepler Aug 05 '12 at 23:14
  • Are you sure you build your program with `UNICODE` defined? Otherwise you try to concatenate a wide character string with a non-wide character string when defining the variable `str_command`. A quick and easy way to check this is to run the program without the parameter, and see if the help message is displayed properly. This is because you use non-wide character output there, and if `UNICODE` is defined then `argv[0]` will not be displayed properly. – Some programmer dude Aug 06 '12 at 07:06
  • @aschepler You are quite right. Everything works perfectly if I open notepad.exe instead. Any ideas of how to make it work for explorer? – aznan Aug 07 '12 at 09:40

1 Answers1

0

I found this thread: Close all browser windows?

It says:

Use the InternetExplorer object to open each window and invoke the Quit method when done. This has the added benefit of closing only the windows you opened (so that windows opened by the user or other applications are unaffected).

https://msdn.microsoft.com/library/aa752127.aspx

I know that this does not help much (missing snippets), but at least something.

Community
  • 1
  • 1
Racky
  • 1,173
  • 18
  • 24