//-----------------------------------------------------------------------------
// 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初始化失败。请问是什么原因,有什么解决方法?
// 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初始化失败。请问是什么原因,有什么解决方法?
//---------------------------------------------------------------------------#include <vcl.h>
#include <objbase.h> //这个头文件
#pragma hdrstop
if( hr = CoInitialize( NULL ) )
而不是
if (SUCCEEDED(hr = CoInitialize( NULL )))
有两个返回码应该都视为成为返回为0,也就是S_OK返回为1,也就是S_FLASE,它的含义是已经调用过CoInitialize
在BCB程序中,APPLICATION对象会主动在主线程调用CoInitialize
titilima(李马) 说的那样写.
另外,用过CoInitialize,别忘了调用CoUnInitialize
if( hr = CoInitialize( NULL ) )
与
if (SUCCEEDED(hr = CoInitialize( NULL )))
是有本质区别的,在网上可以查到HRESULT的详细约定。
SUCCEEDED判断,只要HRESULT非负,都是调用上的成功。