当前位置:编程学习 > wap >>

qt加载dll,运行一次后出错

由于qt的beep函数不能提供频率和时间这两个参数,使用QApplication::beep();后发现声音特别小,于是决定直接加载Windows的Beep函数

Beep函数在msdn的定义是:
Beep Function
Generates simple tones on the speaker. The function is synchronous; it performs an alertable wait and does not return control to its caller until the sound finishes.

BOOL WINAPI Beep(
  __in          DWORD dwFreq,
  __in          DWORD dwDuration
);


Requirements


Header
Declared in Winbase.h; include Windows.h.

Library
Use Kernel32.lib.

DLL
Requires Kernel32.dll.

于是我就打算加载Kernel32.dll这个dll调用Beep这个函数

代码如下

bool Beep(int nfre,int nts)
{
#ifdef Q_OS_WIN
    qDebug()<<"win";
    typedef int (*winBeep)(unsigned long ,unsigned long); //定义函数指针,以备调用 unsigned long  == dword
    QLibrary winlib("Kernel32.dll");
    if (winlib.load())              //判断是否正确加载
    {
        winBeep pBeep = (winBeep)winlib.resolve("Beep");    //援引 Beep() 函数
        if (pBeep)                  //是否成功连接上 add() 函数
        {
            bool r = pBeep(nfre,nts);
            return r;
        }
    }
#else
    QApplication::beep();
    return true;
#endif
    return false;
}


运行时能听到滴一声,但是响完就死了,是什么原因?

qtc输出:
程序异常结束。
D:\V.....it-Release\release\Alert.exe 退出,退出代码: -1073740791
还有
退出代码: -1073741819 dll --------------------编程问答-------------------- 我后来直接用隐式加载就没问题
pro:
LIBS+= -L C:/Program Files/Microsoft SDKs/Windows/v7.0A/Lib -l Kernel32

extern "C"    //由于是C版的dll文件,在C++中引入其头文件要加extern "C" {},注意
{
        #include "C:/Program Files/Microsoft SDKs/Windows/v7.0A/Include/Windows.h"
}


bool AlertItemFrame::BeepEx(int nfre,int nts)
{
#ifdef Q_OS_WIN
//    qDebug()<<"win";
//    typedef int (*winBeep)(unsigned long ,unsigned long); //定义函数指针,以备调用 unsigned long  == dword
//    QLibrary winlib("Kernel32.dll");
//    if (winlib.load())              //判断是否正确加载
//    {
//        winBeep pBeep = (winBeep)winlib.resolve("Beep");    //援引 Beep() 函数
//        if (pBeep)                  //是否成功连接上 add() 函数
//        {
//            bool r = pBeep(nfre,nts);
//            return r;
//        }
//    }
    Beep(nfre,nts);
#else
    QApplication::beep();
    return true;
#endif
    return false;
}
--------------------编程问答-------------------- 其实Qt解析dll也是调用的系统函数
QTDIR/src/corelib/plugin/plugin.pri
win32 {
 SOURCES += plugin/qlibrary_win.cpp
}

unix {
 SOURCES += plugin/qlibrary_unix.cpp
}

你第一种方法用完后加个unload看看 --------------------编程问答--------------------
引用 2 楼 xiuxianshen 的回复:
其实Qt解析dll也是调用的系统函数
QTDIR/src/corelib/plugin/plugin.pri
win32 {
 SOURCES += plugin/qlibrary_win.cpp
}

unix {
 SOURCES += plugin/qlibrary_unix.cpp
}

你第一种方法用完后加个unload看看

试过了,也是响一次后就死了!
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,