问题描述
我正在使用一个C#Windows窗体应用程序,该应用程序正在从C ++ dll调用一些代码。
C ++ dll通过std::cout
将内容记录到控制台。
我还将C#代码中的一些内容记录到日志文件中。
我想做的是将C ++的std::cout
重定向到同一日志文件。
我已经在网络上尝试了几种解决方案,但都没有解决我的问题。
我还观察到了一些东西:我使用Visual Studio进行编码,并且在调试模式下运行程序时只能看到C ++ dll的输出,否则因为我没有控制台,所以我不知道dll在说话
C ++ Dll代码编译为共享库lib_my_c ++。dll
#pragma once
#include <iostream>
extern "C" {
__declspec(dllexport) void Hello()
{
std::cout << "Hello from c++" << std::endl;
}
}
C#包装程序代码
using System;
using System.Runtime.InteropServices;
public class Wrapper
{
// This is the method I call from my Windows form app
public void DoStuff()
{
/* ... */
NativeMethods.Hello(); // this prints stuff to std::cout in C++
/* ... */
}
internal static class NativeMethods
{
[DllImport(@"lib_my_c++.dll",CallingConvention = CallingConvention.Cdecl)]
public static extern void Hello();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)