[英]C++ Interop behaves differently when called from windows service
我有一個 C# 可執行文件,它對 C++ 庫進行互操作調用:
[DllImport(DLL_NAME, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int Check(string name, string version, bool debug);
在 C++ 代碼中,我將 std::cout 重定向到一個文件:
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();
}
當我將 C# 可執行文件作為控制台程序運行時,這工作正常,但是當我將它作為 Windows 服務運行時,該文件不是由 C++ 代碼創建的。
什么會導致這種差異?
該文件實際上正在創建,但作為服務運行時的工作目錄不同: 如解釋here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.