将标准输出重定向到编辑控件 (Win32).马克二世 [英] Redirect stdout to an edit control (Win32). Mark II

查看:57
本文介绍了将标准输出重定向到编辑控件 (Win32).马克二世的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一个完全类似的问题已经存在:将标准输出重定向到一个编辑控件(Win32)

I've seen a question exactly like this already exists: Redirect stdout to an edit control (Win32)

然而,给出的解决方案要求程序员实现一个 my_printf 函数,该函数执行 {printf;从管道读取以编辑控件}.我不能这样做,因为我的 printf 位于外部库中.

However, the solution given requires the programmer to implement a my_printf function that does a {printf; read from pipe to edit control}. I cannot do that because my printf's are in a external library.

理想情况下,我在考虑:

Ideally, I'm thinking of:

  1. 将应用的标准输出重定向到编辑控件
  2. 运行应用程序,瞧

但是如果编辑控件的 API 只允许你向它写入一个字符串,我会想到这样的:

But if edit control's API only allows you to write a string to it, I would think of something like:

1 - 将标准输出复制到管道输出描述符
3 - 从描述符中的管道读取到缓冲区
4 - 从缓冲区写入编辑控件

1 - dup'ing stdout to a pipe out descriptor
3 - read from pipe in descriptor into a buffer
4 - write from buffer to edit control

但是,这里缺少第 2 步:

However, there is a missing step 2 there:

2 - 获取该管道输出描述符何时完成写入的信号.

2 - getting a signal of when a write to that pipe out descriptor is done.

我怎样才能自动化那部分.我可以在这里使用类似套接字 select 的东西吗?

How could I automate that part. Could I use something like a socket select here?

所以,根据 David Heffernan 的评论,我会有类似的内容:

So, according to David Heffernan's comments, I would have something like:

  #define MYPRINT      1
  #define WM_MYMESSAGE (WM_USER+1)

  INT_PTR CALLBACK Foo::DialogProc(
    ...
    case WM_COPYDATA:
      {
        PCOPYDATASTRUCT pMyCDS = (PCOPYDATASTRUCT) lParam;
        LPCSTR szString = (LPCSTR)(pMyCDS->lpData);
        AppendLog(szString);
      }
      break;
    ...
  }

  /* static */
  void Foo::MainThread()
  {
    // Create worker thread
    DWORD dwThreadId = 0;
    m_hRedirectStdoutThread = CreateThread(
      // default security
      NULL,
      // default stack size
      0,
      // routine to execute
      (LPTHREAD_START_ROUTINE) &CTracesConsole::RedirectStdoutThreadRun,
      // thread parameter
      NULL,
      // immediately run the thread
      0,
      // thread Id
      &dwThreadId);
    if (NULL == m_hRedirectStdoutThread)
    {
      printf("Error creating stdin thread\n");
      return;
    }

    // Loop forever
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  /* static */
  void Foo::RedirectStdoutThreadRun()
  {
    // Redirect stdout to pipe
    int fds[2];
    _pipe(fds, 1024, O_TEXT);
    _dup2(fds[1], 1); // 1 is stdout

    char buffer[1024];
    for (;;)
    {
      // Need to flush the pipe
      _flushall();
      // Read stdout from pipe
      DWORD dwNumberOfBytesRead = 0;
      dwNumberOfBytesRead = _read(fds[0], buffer, 1024 - 1);
      buffer[dwNumberOfBytesRead] = 0;

      // Send data as a message
      COPYDATASTRUCT myCDS;
      myCDS.dwData = MYPRINT;
      myCDS.cbData = dwNumberOfBytesRead + 1;
      myCDS.lpData = buffer;
      PostMessage(g_hWindow,
                  WM_MYMESSAGE,
                  0,
                  (LPARAM)(LPVOID) &myCDS);
    }
  }

其中 AppendLog 将字符串写入编辑控件.

Where AppendLog writes a string to the edit control.

此代码现在可以正常工作.有一点不便,当我从 libcurl 重定向标准输出跟踪时,libcurl 停止工作:) 但那是另一回事了...

This code works properly now. With the little inconvenience that, when I redirect stdout traces from libcurl, libcurl stops working :) But that's another story...

推荐答案

据我所知,您无法通过管道获得通知".如果您确实想这样做,也许您应该改用 WM_COPYDATA,这也将提供更简单的解决方案.当文本被发布到您的窗口时,您将收到一条消息,然后您可以将其附加到编辑控件中.

As far as I'm aware you can't get 'notifications' with a pipe. If you do want to do that maybe you should use WM_COPYDATA instead which would also provide a simpler solution. You will get a message when text is posted to your window which you can then append to the edit control.

这篇关于将标准输出重定向到编辑控件 (Win32).马克二世的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆