c++ - DLL中的Rtaudio使程序无响应

标签 c++ vb.net audio dll

我想用C++制作一个DLL,该DLL将声音传送到扬声器,并且可以从Visual Basic程序中调用它。创建dll并从VB程序中调用其功能即可。但是,一旦我在DLL中声明了Rtaudio对象,它就会使Visual Basic程序无响应(并且没有声音)。如果我在控制台应用程序项目中而不是在DLL中使用完全相同的代码,那么一切正常,我会听到声音。由于我对DLL编程没有经验,所以我完全不知道是什么引起了这样的问题。你能给我任何提示可能是什么问题吗?

这是我的代码的一部分。仍然会产生相同的问题:
DLL:

//C and C++ libraries
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <valarray>
#include <fstream>
#include <stdlib.h>
#include <map>
#include <math.h>
#include <stdint.h>
#include <algorithm>
#include <complex>
#include <cmath>
#include <iomanip>
#include <limits>
#include <time.h>
#include <fcntl.h>
#include <ctype.h>
#include <Windows.h>

//BOOST thread library for multi-threading
#include <boost\thread.hpp>

//Include RtAudio
#include <RtAudio.h>

#ifdef AUDIODLL1_EXPORTS
    #define AUDIODLL1_API __declspec(dllexport) 
#else
    #define AUDIODLL1_API __declspec(dllimport) 
#endif

RtAudio dac;       //If I leave this line out, everything works well

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{

    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            break;

        case DLL_THREAD_ATTACH:
            break;

        case DLL_THREAD_DETACH:          
            break;

        case DLL_PROCESS_DETACH:
            break;
    }


    return TRUE;
}


AUDIODLL1_API int __stdcall getNumber()
{
    return 5;
}

这是.def文件:
LIBRARY AudioDll1
DESCRIPTION 'Audio DLL Test 1'

EXPORTS
    getNumber

这是VB示例代码:
Public Class Form1
    Private Declare Function getNumber Lib "AudioDll1.dll" () As Integer

    Dim myNum As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.Text = 10

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = 1
        myNum = getNumber()
        Label1.Text = 2
        Label1.Text = myNum
    End Sub
End Class

正如我所说,唯一的问题是
RtAudio dac;

我尝试在本地声明它,将其放入线程等中。但是,如果我只是创建一个win32控制台应用程序,则包括相同的 header ,链接相同的库,一切都将正常运行。这些是我链接到的库:
dsound.lib
rtaudiolib.lib
kernel32.lib
user32.lib
advapi32.lib
ole32.lib

我不从父项或项目默认值继承。

哦,我知道即使可以使用,但是使用上面的代码我听不到声音。正如我所说,我尝试删除部分代码以查找问题。和RtAudio dac;是造成它的全部原因。

编辑:我还应该补充一点,rtaudiolib.lib是我使用Rtaudio 4.0.12代码制作的静态库
#define __WINDOWS_DS__ 
#define __WINDOWS_ASIO__

我将不胜感激任何帮助。

最好的祝福

最佳答案

RtAudio构造函数正在调用RtApiDs::getDeviceCount(),而后者又在调用DirectSoundEnumerate(),后者是dsound.dll的功能。

我一直试图在DLL_PROCESS_ATTACH期间(或在全局空间中,似乎引起相同的问题)声明RtAudio对象。

Microsoft声明不应在DLL_PROCESS_ATTACH期间加载其他库。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583.aspx

The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary) during process termination, because this can result in a DLL being used after the system has executed its termination code.



因此,我决定不对DLL_PROCESS_ATTACH做任何事情,并为我的DLL提供一个单独的自定义init()函数。这样就解决了所有问题。

关于c++ - DLL中的Rtaudio使程序无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22220269/

相关文章:

c++ - 将 *pointer 元素转换为一维数组

regex - 从字符串中分离字符和数字,vb.net

ASP.Net MissingMethodException - "ctor"方法未找到

c++ - 发散颜色映射可视化的建议算法

c++ - 为什么我的图书馆管理程序没有写入文件

c++ - 将 Windows 编译的 QT 应用程序移植到 Linux?

vb.net - GetPrivateProfileString() 返回空字符串,尽管有默认值

audio - 识别HE-AAC音频采样频率

c# - 计算 MP3 帧的长度(以毫秒为单位)

android - 拆分长度不正确的音轨 - FFMPEG