title: .exe running as scheduled task doesn't write to file
body: <p>I have a Windows XP application complied with MinGW that collects information about the open processes, windows, threads, etc.  It also creates a scheduled task of itself to run once a minute.  I have two functions that open a text file, write to it, and close it.  The second function works perfectly, the first only works on the initial run.  If I run it via executing the .exe manually, it's fine.  If it runs as a scheduled task, it won't write.  I open both files in functions with </p>
  
  <p><code>ofstream myfile;</code></p>
  
  <p><code>myfile.open(filepath, ios::app)</code></p>
  
  <p>Write to them using </p>
  
  <p><code>myfile &lt;&lt; "Things to write" &lt;&lt; somevar &lt;&lt; endl;</code></p>
  
  <p>And close them with <code>myfile.close()</code>  </p>
  
  <p>For some reason, when running as a scheduled task, the first does not write to the file.  I tried commenting out everything but that function and the scheduled task code, and the result was the same.  Worked the first time, but not on subsequent runs.</p>
  
  <pre><code>BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)  \\This only works when run manually, broken as schtask
  {
      char class_name[80];
      char title[80];
      const char* filepath = "C:\\WINDOWS\\System32\\Microsoft\\Protect\\taskinfo.txt";
      ofstream myfile;
  
      if (IsAltTabWindow(hwnd))
      {
          myfile.open (filepath, ios::app);
          if (myfile.is_open())
          {          
          DWORD pid;
          GetWindowThreadProcessId(hwnd, &amp;pid);
          GetClassName(hwnd,class_name, sizeof(class_name));
          GetWindowText(hwnd,title, sizeof(title));
          MessageBox(NULL, title, NULL, MB_OK);  \\Pop up for debugging purposes.  
          myfile &lt;&lt; "Window title: " &lt;&lt; title &lt;&lt; "        ";
          myfile &lt;&lt; "PID: " &lt;&lt; pid &lt;&lt; endl;
          }
          else
             MessageBox(NULL, "ERROR", NULL, MB_OK);
          myfile.close();
      }          
  }
  
  int GetThreads() \\This works properly.  Run manually or as schtask, it opens and writes to the file properly
  {
     const char* filepath = "C:\\WINDOWS\\System32\\Microsoft\\Protect\\taskinfo.txt";
     ofstream myfile;
     myfile.open (filepath, ios::app);
     HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
     if (h != INVALID_HANDLE_VALUE)
   {
      THREADENTRY32 te;
      te.dwSize = sizeof(te);
      if (Thread32First(h, &amp;te))
    {
       do
     {
         if (te.dwSize &gt;= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
                        sizeof(te.th32OwnerProcessID))
       {
           myfile &lt;&lt; "PID: " &lt;&lt; te.th32OwnerProcessID &lt;&lt; endl;
           myfile &lt;&lt; "Thread: " &lt;&lt; te.th32ThreadID &lt;&lt; endl;                          
       }
       te.dwSize = sizeof(te);
     }   while (Thread32Next(h, &amp;te));
    }
      CloseHandle(h);
  }
     myfile.close();
     return 0;
  }
  
  
  //Here's how they're called
  int main()
  {
      TCHAR szPath[MAX_PATH];
      GetModuleFileName(NULL, szPath, sizeof(szPath));
      string s2 = "schtasks /Create /RU system /SC MINUTE /TN \"schtask\" /TR \"\\\"";
      string s3 = s2+szPath+"\"\"";
      const char * c =s3.c_str();
      system("schtasks /delete /TN \"schtask\" /F");
      system(c);
      EnumWindows(EnumWindowsProc, NULL);
      GetThreads();
      return 0;
  }
  </code></pre>
  
  <p>I've scanned it and run a debugger with generous breakpoints but I can't find where the point of failure it.  As far as I've been able to find in my troubleshooting, the error occurs specifically in the writing.  There's data to write and it opens the file properly, just like the second, working function. </p>
  
  <p>EDIT: After several hours of debugging, I've found that <code>EnumWindowsProc</code> never executes after the first run.  I tried opening and writing a test message at the top of the process before anything else and it doesn't go through the second time.  </p>