title: C++ Interop behaves differently when called from windows service
body: <p>I have a C# executable that makes an interop call to a C++ library:</p>
  
  <pre><code>[DllImport(DLL_NAME, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  [return: MarshalAs(UnmanagedType.I4)]
  public static extern int Check(string name, string version, bool debug);
  </code></pre>
  
  <p>In the C++ code, I redirect std::cout to a file:</p>
  
  <pre><code>std::streambuf* psbuf, * backup;
  std::ofstream filestr;
  
  ...
  
  if (debug)
  {
      filestr.open("debug.txt");
      backup = std::cout.rdbuf();
      psbuf = filestr.rdbuf();
      std::cout.rdbuf(psbuf);
  
      std::cout &lt;&lt; "some debug message" &lt;&lt; std::endl; 
  
      std::cout.rdbuf(backup);
      filestr.close();
  }
  </code></pre>
  
  <p>This works fine when I run the C# executable as a console program, but when I run it as a Windows Service, the file is not created by the C++ code.</p>
  
  <p>What would account for this difference?</p>