COM初始化的问题(急,在线等,解决后马上给分)
syy64 2004-09-02 04:06:13 //-----------------------------------------------------------------------------
// 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初始化失败。请问是什么原因,有什么解决方法?