ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c – 是否会加载DLL动态协调其stderr到主应用程序?如果是这样,那怎么……?

2019-10-07 02:05:55  阅读:233  来源: 互联网

标签:c qt redirect dll stderr


我正在使用Qt编写GUI应用程序,该应用程序链接到第三方DLL,有时会向stderr发送错误消息.我希望这些错误消息显示在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通道不受影响.因此,我应该能够通过在设置重定向后动态加载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,qt,redirect,dll,stderr
来源: https://codeday.me/bug/20191007/1863698.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有