如何从C#代码将C ++ std :: cout写入文件-使用C ++ dll的C#Windows窗体应用程序

问题描述

我正在使用一个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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...