Mingw,XAudio2和GetProcAddress失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mingw,XAudio2和GetProcAddress失败相关的知识,希望对你有一定的参考价值。

我正在尝试将XAudio2与Mingw一起使用。由于我无法弄清楚我想要让Mingw链接XAudio2,我想我会尝试纯粹通过dll加载来使用XAudio2。但是以下代码无法使用GetProcAddress找到XAudio2Create。

#include <windows.h>

static HMODULE xaudio2 = NULL;

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) {
    xaudio2 = LoadLibraryA("XAudio2_2.dll");
    if (xaudio2 == NULL) {
        MessageBox(NULL, L"LoadLibrary failed", 0, 0);
    }

    void* fnc = GetProcAddress(xaudio2, "XAudio2Create");
    if (fnc == NULL) {
        MessageBox(NULL, L"GetProcAddress failed", 0, 0);
    }

    return 0;
}
答案

我想你必须通过COM和CoCreateInstance()。就像是

#include <windows.h>
#include <stdio.h>
#include <XAudio2.h>

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) {
    WAVEFORMATEX format;
    format.wFormatTag = 1;
    format.nChannels = 1;
    format.nSamplesPerSec = 44100;
    format.nAvgBytesPerSec = 44100;
    format.nBlockAlign = 1;
    format.wBitsPerSample = 8;
    format.cbSize = 0;

    CoInitializeEx(NULL, COINIT_MULTITHREADED);

    IXAudio2* pXAudio2 = NULL;
    IXAudio2SourceVoice* source = NULL;
    IXAudio2MasteringVoice* master = NULL;

    HRESULT hr;
    if ( FAILED(hr = XAudio2Create( &pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) ) {
        MessageBox(0, L"hi", L"hi", 0);
    }

    if ( FAILED(hr = pXAudio2->CreateMasteringVoice(&master ) ) ) {
        MessageBox(0, L"hi", L"hi", 0);
    }

    if ( FAILED(hr = pXAudio2->CreateSourceVoice(&source, &format) ) ) {
        MessageBox(0, L"hi", L"hi", 0);
    }

    char str[30];

    float freq = 0.0;
    source->GetFrequencyRatio(&freq);
    sprintf(str, "%f", freq);
        MessageBoxA(0, str, str, 0);

    UINT32 OperationSetCounter = 0;
    UINT32 OperationID = UINT32(InterlockedIncrement(LPLONG(&OperationSetCounter)));

    if ( FAILED(hr = source->SetFrequencyRatio(1.1,OperationID ) ) ) {
        MessageBox(0, L"hi", L"hi", 0);
    }

    if ( FAILED(hr = pXAudio2->CommitChanges(OperationID ) ) ) {
        MessageBox(0, L"hi", L"hi", 0);
    }



    float freq2 = 0.0;
    source->GetFrequencyRatio(&freq2);
    sprintf(str, "%f", freq2);
    MessageBoxA(0, str, str, 0);

    Sleep(2000);
    float freq3 = 0.0;
    source->GetFrequencyRatio(&freq3);
    sprintf(str, "%f", freq3);
    if (freq3 != 1.0) {
        MessageBoxA(0, str, str, 0);
    }

    source->Start(0);
    return 0;
}

以上是关于Mingw,XAudio2和GetProcAddress失败的主要内容,如果未能解决你的问题,请参考以下文章

XAudio2学习之调节音量

XAudio2播放PCM

XAudio2 是不是有最大声音数?

在 xaudio2 中集成 3D 音频

XAudio2学习之采样率转换

Windows 8 xaudio2 在多点触控应用程序中播放 wav 文件