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初始化失败。请问是什么原因,有什么解决方法?

...全文
459 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
halfdream 2004-09-03
  • 打赏
  • 举报
回复
因此,楼主代码应该象
titilima(李马) 说的那样写.


另外,用过CoInitialize,别忘了调用CoUnInitialize
halfdream 2004-09-03
  • 打赏
  • 举报
回复
不要大惊小怪.
有两个返回码应该都视为成为

返回为0,也就是S_OK

返回为1,也就是S_FLASE,它的含义是已经调用过CoInitialize


在BCB程序中,APPLICATION对象会主动在主线程调用CoInitialize



syy64 2004-09-03
  • 打赏
  • 举报
回复
问题基本解决,不用初始化COM,C++builder也能用,结帖。
halfdream 2004-09-03
  • 打赏
  • 举报
回复
晕,最近我错别字越来越多。。

当0,1两个返回码应该都视为成功!


if( hr = CoInitialize( NULL ) )

if (SUCCEEDED(hr = CoInitialize( NULL )))
是有本质区别的,在网上可以查到HRESULT的详细约定。


SUCCEEDED判断,只要HRESULT非负,都是调用上的成功。
wangweixing2000 2004-09-03
  • 打赏
  • 举报
回复
我在bcb下测试了没有问题!初始化com其实应该放在工程的开始部分CoUnInitialize就放在整个项目的结束!
wangweixing2000 2004-09-03
  • 打赏
  • 举报
回复
syy64(太平洋) 是的!
syy64 2004-09-02
  • 打赏
  • 举报
回复
titilima(李马) ( ) :应该没有本质区别,成功返回是0,否则不是0。
syy64 2004-09-02
  • 打赏
  • 举报
回复
wangweixing2000(星(幸福的蜜月中)) :是否将你说的头文件加在C++Builder程序里?
李马 2004-09-02
  • 打赏
  • 举报
回复
为什么是
if( hr = CoInitialize( NULL ) )
而不是
if (SUCCEEDED(hr = CoInitialize( NULL )))
内存泄漏 2004-09-02
  • 打赏
  • 举报
回复
这种现象很奇怪,你另外再单独在CB里面写一个程序,调用CoInitialize成功吗?
wangweixing2000 2004-09-02
  • 打赏
  • 举报
回复
加上:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <objbase.h> //这个头文件
#pragma hdrstop
syy64 2004-09-02
  • 打赏
  • 举报
回复
to:DentistryDoctor(牙科医生) :错误代码是1,也就是返回值是1。
DentistryDoctor 2004-09-02
  • 打赏
  • 举报
回复
检查一下错误代码和错误信息。

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧