本文介绍了接口,结构的CComSafeArray类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友!
使用ATL(VS2008,C ++)时遇到问题.
我必须在idl文件中使用SAFEARRAY(IMyObject *)*参数.
如果使用SAFEARRAY(double)*,则使用CComSafeArray< double> ...,但是对于接口或我的结构,我不知道如何解决.
谢谢您.

idl文件是...

Dear Friends!
I have problem while working with ATL(VS2008, C++).
I must use SAFEARRAY(IMyObject*)* parameters in idl file.
If SAFEARRAY(double)*, then use the CComSafeArray<double>..., but for interfaces or my structures, I don''t know how to solve.
Thank you in advanced.

The idl file is...

...

interface IMyObj2;
...

typedef struct SMyObj2{
    IMyObj2 * iobject;
}SMyObj2;

...

//.IMyObj2
[
	object,
	uuid(3454B81A-65A9-4F3E-8678-1D5F82B25837),
	oleautomation,
	nonextensible,
	helpstring("IMyObj2 Interface"),
	pointer_default(unique)
]
interface IMyObj2 : IUnknown{
	[propget, helpstring("property ID")] 
	HRESULT ID([out, retval] LONG* pVal);
	[propput, helpstring("property ID")] 
	HRESULT ID([in] LONG newVal);
};

...


[
	object,
	uuid(EF532C8B-12A9-4FEC-B91C-D7502FD68874),
	oleautomation,
	nonextensible,
	helpstring("IArrayMan3 Interface"),
	pointer_default(unique)
]

interface IArrayMan3 : IUnknown{
	[helpstring("method GetArray")] 
	HRESULT _stdcall GetArray2(SAFEARRAY(SMyObj2)* pArray);
	[helpstring("method PutArray")] 
	HRESULT _stdcall PutArray2(SAFEARRAY(SMyObj2) pArray);
};



C ++文件...



C++ file...

STDMETHOD(GetArray2)(SAFEARRAY** pArray)
{
	CComSafeArray<IUnknown*> ppp;//.Compile Okay.
//	CComSafeArray<SMyObj2, VT_VARIANT> arr;//.Compile Error
//      CComSafeArray<IMyObj2*> ttt;//.Compile Error
	return S_OK;
}

推荐答案

GUID GUID_SMyObj2 = __uuidof(SMyObj2);
HRESULT hr;
CComPtr<IRecordInfo> srecnfo;
hr = GetRecordInfoFromGuids(LIBID_MyLib /*here is your type lib identifier*/, 1, 0, 0, GUID_SMyObj2, &srecnfo);
if(FAILED(hr))
  _com_issue_errorex(hr, NULL, IID_NULL);


如果这部分将成功,则可以创建一个安全数组或自定义类型:


If this part will be successful, then it is possible to create a safearray or custom type:

const LONG l_bound = 0, u_bound = 2;
LPSAFEARRAY lpSMyObj2 = SafeArrayCreateVectorEx(VT_RECORD, l_bound,
  u_bound - l_bound + 1, srecnfo);
if(!lpSMyObj2)
  _com_issue_errorex(E_FAIL, NULL, IID_NULL);


或访问并使用现有的内容:


or access and use existing:

//LPSAFEARRAY lpSMyObj2 got from somewhere
//getting bounds of array
  LONG l_bound = 0, u_bound = 0;
  HRESULT hr1 = SafeArrayGetLBound(lpSMyObj2, 1, &l_bound);
  HRESULT hr2 = SafeArrayGetUBound(lpSMyObj2, 1, &u_bound);
  if(FAILED(hr1) || FAILED(hr2) )
  {
      // handle an error
  }
  else
  { 
     ...
     SMyObj2 * pSMyObj2 = NULL;
     HRESULT hr = SafeArrayAccessData(lpSMyObj2, (void**)&pSMyObj2);
     if(FAILED(hr))
       _com_issue_errorex(hr, NULL, IID_NULL);
     ...
  }
  // modifying data if needed
  for(int i = l_bound; i <= u_bound - l_bound; i++)
  {
    // do something with pSMyObj2[i].iobject
  }
  ...
  // Releasing an array
  hr = SafeArrayUnaccessData(lpSMyObj2);
  if(FAILED(hr))
    _com_issue_errorex(hr, NULL, IID_NULL);


请参阅 http://msdn.microsoft.com/en-us/library/aa148975.aspx [ ^ ]以获取更多详细信息


Please see http://msdn.microsoft.com/en-us/library/aa148975.aspx[^] for more details


这篇关于接口,结构的CComSafeArray类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 14:35