I have a C# executable that makes an interop call to a C++ library:
[DllImport(DLL_NAME, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int Check(string name, string version, bool debug);
In the C++ code, I redirect std::cout to a file:
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 << "some debug message" << std::endl;
std::cout.rdbuf(backup);
filestr.close();
}
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.
What would account for this difference?