c++ - 加载 DLL 是否会动态协调其 stderr 到主应用程序?如果是这样,那么如何......?

标签 c++ qt dll redirect stderr

我正在使用 Qt 编写一个 GUI 应用程序,它链接到有时会向 stderr 发送错误消息的第三方 DLL。我希望这些错误消息显示在我的 GUI 中的一个窗口中。

即使经过大量搜索,我也找不到重定向 stderr(与 std::cerr 相对)的既定方法,所以我自己编写了以下类:

class StdErrRedirect : public QObject
{
    Q_OBJECT

public:
    // Constructor
    StdErrRedirect(QTextEdit *errorLog,
                   QObject   *parent = NULL);

    // Destructor
    ~StdErrRedirect();

private slots:
    void fileChanged(const QString &filename);

private:    
    QFile              tmp;
    QFileSystemWatcher watcher;
    QString            tmpFileNameQtFormat;
    QString            tmpFileNameNativeFormat;

    QTextEdit *m_errorLog;
    QString   oldContent;
};

StdErrRedirect::StdErrRedirect(QTextEdit *errorLog,
                               QObject   *parent)
    : QObject(parent)
{
    // Store the pointer to the error log window
    m_errorLog = errorLog;

    // Create a temporary filename: first find the path:
    tmpFileNameQtFormat = QDir::tempPath();

    // Make sure the closing slash is present:
    if (!tmpFileNameQtFormat.endsWith(QChar('/')))
        tmpFileNameQtFormat.append(QChar('/'));

    // Add the file name itself:
    tmpFileNameQtFormat.append("nb_stderrlog");

    // Obtain a version of the filename in the operating system's native format:
    tmpFileNameNativeFormat = QDir::toNativeSeparators(tmpFileNameQtFormat);

    // Set up redirection to this file:
    freopen(tmpFileNameNativeFormat.toAscii().constData(), "a+", stderr);

    // Initialise the QFileSystemWatcher:
    connect(&watcher, SIGNAL(fileChanged(const QString &)),
            this,     SLOT(fileChanged(const QString &)));
    watcher.addPath(tmpFileNameQtFormat);

    tmp.setFileName(tmpFileNameQtFormat);
}

StdErrRedirect::~StdErrRedirect()
{
    // Ensure the temporary file is properly deleted:
    fclose(stderr);
    tmp.close();
    tmp.open(QIODevice::ReadWrite);
    tmp.remove();
}

void StdErrRedirect::fileChanged(const QString &filename)
{
    tmp.open(QIODevice::ReadOnly);
    QTextStream stream(&tmp);
    QString content = stream.readAll();
    tmp.close();

    // Identify what's new, and just send this to the window:
    int newchars = content.size() - oldContent.size();
    if (newchars)
    {
        m_errorLog -> append(content.right(newchars));
        oldContent = content;
    }
}

如果我使用以下方法从我的主窗口实例化它:

errorLog = new QTextEdit;
redirector = new StdErrRedirect(errorLog);

...然后我写入 stderr 的所有内容都会出现在窗口中。

到目前为止,还不错。问题是 DLL 的输出仍然没有。在对发出错误的 DLL 函数的调用中,如果我输入代码:

if (error != _OK)
{
    error.PrintErrorTrace();
    fprintf(stderr, "Should have printed an error \r\n");
    fflush(stderr);
    //fsync(_fileno(stderr));    Linux version
    _commit(_fileno(stderr));
    return;
}

...然后出现文本“应该打印错误”,但错误消息本身不会出现。

现在,我在某处读到这可能是因为在应用程序开始时加载 DLL 之后正在设置重定向,因此它自己的 stderr channel 不受影响。因此,我应该能够通过动态加载 DLL 来修复此问题,设置重定向之后。

那么这是我的问题:我该怎么做?我可以尝试将以下代码放在我的应用程序的开头:

QLibrary extlib;
extlib.setFileName("libname");
extlib.setLoadHints(QLibrary::ResolveAllSymbolsHint);
extlib.load();

...但它本身没有任何效果。我认为这是因为链接器仍在将库设置为自动打开。但是,如果我从链接器中删除 DLL(我使用的是 VS2008,因此我从依赖项列表中删除了 extlib.lib),那么应用程序将无法编译,因为编译器无法从 DLL 中找到符号。

所以我在这里尝试做的事情显然存在严重错误。有人可以帮忙吗?

谢谢, 斯蒂芬。

最佳答案

DLL 是否真的写入了stderr?还是写入 GetStdHandle(STD_ERROR_HANDLE) ?第一个映射到第二个,最初。但是使用 freopen() 您只需更改映射。写入 STD_ERROR_HANDLE 的任何内容仍会保留在那里。

要重定向每个人的错误输出,您需要 SetStdHandle .

关于c++ - 加载 DLL 是否会动态协调其 stderr 到主应用程序?如果是这样,那么如何......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3202654/

相关文章:

javascript - NPAPI:修改插件对象的 CSS 属性

c++ - 如何对卡诺图中的单元格进行分类

c++ - 自定义类型、类型转换导致运算符冲突?

c++ - dll 注入(inject) - 简单的主要

c++ - 在 C++ 中使用数组参数访问单个元素的字符

c++ - Qt 调试器中的 reinterpret_cast 错误值

c - Qt Creator 中的 Gstreamer rtsp 服务器链接

c++ - Qt4 到 Qt5 链接错误

C# 多个控制台应用程序运行和隔离

c++ - 未在 MFC dll 中调用 COMMAND 和 UPDATE_COMMAND_UI 处理程序