Mingw,XAudio2和GetProcAddress失败

问题描述 投票:0回答:1

我正在尝试将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;
}
dll mingw xaudio2
1个回答
1
投票

我想你必须通过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;
}
© www.soinside.com 2019 - 2024. All rights reserved.