CComSafeArray的使用方法 [英] CComSafeArray ways of use

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

问题描述

你好,

我有一个Com功能:

GetData(SAFEARRAY ** pRetVal)



我有以下代码:



Hello,
I have a Com function:
GetData (SAFEARRAY ** pRetVal)

I have the following piece of code:

SAFEARRAY *ppData = NULL;
hr = pmyInterface->GetData( &ppData );

CComSafeArray<IUnknown*> pSafeArgs;

pSafeArgs.Attach( ppData );





我必须更改它,删除ppData,还删除附加命令。



它应该是这样的:





I have to change it, and remove ppData, and also remove the Attach command.

It should be something like this:

CComSafeArray<IUnknown*> pSafeArgs;

hr = pmyInterface->GetData( ((SAFEARRAY**)&pSafeArgs )))





但这不起作用。可能是CComArray发布的问题。

我该怎么办?



But this doesn't work. Probably a problem with the release of the CComArray.
How can I do it?

推荐答案

你应该使用SafeArrayAccessData和SafeArrayUnaccessData:



http://msdn.microsoft.com/ en-us / library / ms891243.aspx [ ^ ]



在本文中,有一个使用安全阵列的好例子:



C#ATLCOM Interop代码段 - 第1部分 [< a href =http://www.codeproject.com/Articles/31927/C-ATLCOM-Interop-code-snipperts-Part-1target =_ blanktitle =New Window> ^ ]



希望它有所帮助。
You should use SafeArrayAccessData and SafeArrayUnaccessData:

http://msdn.microsoft.com/en-us/library/ms891243.aspx[^]

In this article, there is a good sample of using safearrays:

C# ATLCOM Interop code snipperts - Part 1[^]

Hope it helps.


要获得内部SAFEARRAY,你可以使用GetSafeArrayPtr()方法或它的成员m_psa。 />

请尝试这样做:

For getting the internal SAFEARRAY you can use GetSafeArrayPtr( ) method or it's member m_psa.

Please try doing like this:
hr = pmyInterface->GetData( &(pSafeArgs.m_psa) );


问题在于破坏CComSafeArray。当您通过

The problem is in the destruction of the CComSafeArray. When you assign the data directly to CComSafeArray.m_psa either by
pmyInterface->GetData( ((SAFEARRAY**)&pSafeArgs )))






or

pmyInterface->GetData( &(pSafeArgs.m_psa) );



然后该类不会调用Lock()。当析构函数触发时,它失败,因为它试图调用UnLock()并且尚未调用Lock()。当您通过Attach()或赋值运算符分配数据时,将调用Lock()并且析构函数很高兴。不幸的是,他们使Lock()和Unlock()方法受到保护,因此你不能自己调用​​Lock()。


then the class does not call Lock(). When the destructor fires it fails because it tries to call UnLock() and Lock() hasn't been called. When you assign the data through Attach() or an assignment operator then Lock() is called and the destructor is happy. Unfortunately they made the Lock() and Unlock() methods protected so you can't call Lock() on your own either.


这篇关于CComSafeArray的使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆