//-----------------------------------------------------------------------------
// Name: InitDirectSound()
// Desc: Initilizes DirectSound
//-----------------------------------------------------------------------------
HRESULT CSoundObj::InitDirectSound()
{
int i;
    HRESULT  hr;
    LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;    // Initialize COM
    if( hr = CoInitialize( NULL ) )//VC++作的应用程序调用初始化成功,没问题;C++Builder作的应用程序,调用,初始化失败。
        return hr;    // Create IDirectSound using the primary sound device
    if( FAILED( hr = DirectSoundCreate( NULL, &g_pDS, NULL ) ) )
        return hr;    // Set coop level to DSSCL_PRIORITY
    if( FAILED( hr = g_pDS->SetCooperativeLevel( GetForegroundWindow(), DSSCL_NORMAL  ) ) )
        return hr; g_pDSBuffer.SetSize(m_lNumWaveFile);
m_strFileNameArray.SetSize(m_lNumWaveFile);
lpDsb3dBufferarray.SetSize(m_lNumWaveFile);// Create the Direct 3D Sound Buffer
lpDsb3dbuffer = CreateSoundBuffer3D();
// Query interface for Direct 3D Sound Listener object
    if(DS_OK != lpDsb3dbuffer->QueryInterface(IID_IDirectSound3DListener, (void**)&g_lpDs3dListener))
    {
    //          RegError("Not able to create Direct 3D Sound Listener object");
return false;
}
// Set the Direct 3D Sound Rolloff Factor
    g_lpDs3dListener->SetRolloffFactor((FLOAT)0.01,DS3D_DEFERRED);//设置衰减系数 2004.4.1.
// Change listener's orientation
g_lpDs3dListener->SetOrientation(D3DVAL(0), D3DVAL(0), D3DVAL(1), D3DVAL(0), D3DVAL(1), D3DVAL(0),DS3D_DEFERRED);
// Commit the changes to Rolloff Factor and orientation
g_lpDs3dListener->CommitDeferredSettings();
for(i=0;i<m_lNumWaveFile;i++)
{
g_pDSBuffer[i]   = NULL;
lpDsb3dBufferarray[i] = NULL;
}
    // Get the primary buffer 
/*    DSBUFFERDESC dsbd;
    ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
    dsbd.dwSize        = sizeof(DSBUFFERDESC);
    dsbd.dwFlags       = DSBCAPS_CTRL3D|DSBCAPS_PRIMARYBUFFER;
    dsbd.dwBufferBytes = 0;
    dsbd.lpwfxFormat   = NULL;
       
    if( FAILED( hr = g_pDS->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL ) ) )
        return hr;    // Set primary buffer format to 22kHz and 16-bit output.
    WAVEFORMATEX wfx;
    ZeroMemory( &wfx, sizeof(WAVEFORMATEX) ); 
    wfx.wFormatTag      = WAVE_FORMAT_PCM; 
    wfx.nChannels       = 2; 
    wfx.nSamplesPerSec  = 22050; 
    wfx.wBitsPerSample  = 16; 
    wfx.nBlockAlign     = wfx.wBitsPerSample / 8 * wfx.nChannels;
    wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;    if( FAILED( hr = pDSBPrimary->SetFormat(&wfx) ) )
        return hr;    SAFE_RELEASE( pDSBPrimary );*/    return S_OK;
}我将上面的程序作成VC++规则DLL,用VC++应用程序调用,COM初始化成功,没问题;用C++Builder作的应用程序,调用,COM初始化失败。请问是什么原因,有什么解决方法?

解决方案 »

  1.   

    to:DentistryDoctor(牙科医生) :错误代码是1,也就是返回值是1。
      

  2.   

    加上:
    //---------------------------------------------------------------------------#include <vcl.h>
    #include <objbase.h>    //这个头文件
    #pragma hdrstop
      

  3.   

    这种现象很奇怪,你另外再单独在CB里面写一个程序,调用CoInitialize成功吗?
      

  4.   

    为什么是
    if( hr = CoInitialize( NULL ) )
    而不是
    if (SUCCEEDED(hr = CoInitialize( NULL )))
      

  5.   

    wangweixing2000(星(幸福的蜜月中)) :是否将你说的头文件加在C++Builder程序里?
      

  6.   

    titilima(李马) ( ) :应该没有本质区别,成功返回是0,否则不是0。
      

  7.   

    不要大惊小怪.
    有两个返回码应该都视为成为返回为0,也就是S_OK返回为1,也就是S_FLASE,它的含义是已经调用过CoInitialize
    在BCB程序中,APPLICATION对象会主动在主线程调用CoInitialize
      

  8.   

    因此,楼主代码应该象
    titilima(李马) 说的那样写.
    另外,用过CoInitialize,别忘了调用CoUnInitialize
      

  9.   

    我在bcb下测试了没有问题!初始化com其实应该放在工程的开始部分CoUnInitialize就放在整个项目的结束!
      

  10.   

    晕,最近我错别字越来越多。。当0,1两个返回码应该都视为成功!
    if( hr = CoInitialize( NULL ) )

    if (SUCCEEDED(hr = CoInitialize( NULL )))
    是有本质区别的,在网上可以查到HRESULT的详细约定。
    SUCCEEDED判断,只要HRESULT非负,都是调用上的成功。
      

  11.   

    问题基本解决,不用初始化COM,C++builder也能用,结帖。