首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载DLL是否会动态调整其stderr与主应用程序的关系?如果是,那怎么.?

问加载DLL是否会动态调整其stderr与主应用程序的关系?如果是,那怎么.?
EN

Stack Overflow用户
提问于 2010-07-08 10:27:28
回答 2查看 1.5K关注 0票数 3

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

即使经过多次搜索,我仍然找不到一种既定的方法来重定向stderr (相对于std::cerr),所以我自己编写了以下类:

代码语言:javascript
复制
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;
    }
}

如果我从我的主窗口实例化它,使用:

代码语言:javascript
复制
errorLog = new QTextEdit;
redirector = new StdErrRedirect(errorLog);

..。然后我写给stderr的所有东西都会出现在窗口里。

到现在为止还好。问题是DLL的输出仍然没有。在调用发出错误的DLL函数时,如果我将代码:

代码语言:javascript
复制
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;
}

...then文本“应该打印一个错误”出现,但错误消息本身没有出现。

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

这是我的问题,那么:我如何做到这一点?我可以尝试在我的应用程序的开头放置以下代码:

代码语言:javascript
复制
QLibrary extlib;
extlib.setFileName("libname");
extlib.setLoadHints(QLibrary::ResolveAllSymbolsHint);
extlib.load();

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

很明显,我在这里做的事情有很大的错误。有人能帮忙吗?

谢谢,斯蒂芬。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-08 13:18:02

DLL真的写入stderr吗?还是写信给GetStdHandle(STD_ERROR_HANDLE)?第一个映射到第二个,最初。但是使用freopen(),您只需更改映射。写给STD_ERROR_HANDLE的任何东西都会送到那里。

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

票数 2
EN

Stack Overflow用户

发布于 2010-07-09 06:40:19

只有一个stderr,所以我猜问题是不是,您需要动态加载dll,而是在您的重定向代码中的某个位置。您的重定向代码是编写Linux风格的,在windows中,事情的工作方式不同。

如果您可以在Linux上测试您的应用程序,这将有助于解决问题。如果它在Linux上工作,那么它就是重定向代码。

无论如何,你应该多读一些关于重定向和窗口的内容,因为我不认为你现在想做的事情会对你有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3202654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档