Как отключить / включить консольный вывод из C ++ / CLI DLL в приложении C #? - PullRequest
0 голосов
/ 13 апреля 2019

DLL написана на C ++ / CIL, я хочу подавить вывод из нее.

// The returned value is checked and every call is success
// The C++/CLI dll
public ref class Class1
{
    static Class1()
    {
        printf("printf\n");
        puts("puts");
        fprintf(stdout, "fprintf\n");
        cout << "cout" << endl;
        Console::WriteLine("Console::WriteLine");
    }
public:
    void fun()
    {
        printf("fun printf\n");
        Console::WriteLine("fun Console::WriteLine");
    }
};
// C#
class App1
{
    const int STD_OUTPUT_HANDLE = -11;
    [DllImport("Kernel32.dll", SetLastError = true)]
    public static extern int SetStdHandle(int device, IntPtr handle);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr GetStdHandle(int nStdHandle);
    static void Main(string[] args)
    {
        SetStdHandle(STD_OUTPUT_HANDLE, IntPtr.Zero);
        new Class1(); // still output from printf/puts/...
    }
}
class App2
{
    // Skip some dllimport related code
    private static IntPtr stdout;
    static void Main(string[] args)
    {
        stdout = GetStdHandle(STD_OUTPUT_HANDLE);
        SetStdHandle(STD_OUTPUT_HANDLE, IntPtr.Zero);
        fun();
    }

    private static void fun()
    {
        Class1 c = new Class1(); // does not output
        SetStdHandle(STD_OUTPUT_HANDLE, stdout);
        c.fun(); // does not output
    }
}

App1 может только отключить Console :: WriteLine, но не что-то вроде printf.

App2 может отключить все выходные данные, но не может их снова включить.

Почему printf подавляется и не подавляется в App1 и App2?Разница лишь в вызове функции.А как отключить то включить выход с него?

...