Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 );


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

It should be something like this:

C++
CComSafeArray<IUnknown*> pSafeArgs;

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


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

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.
 
Share this answer
 
For getting the internal SAFEARRAY you can use GetSafeArrayPtr( ) method or it's member m_psa.

Please try doing like this:
C++
hr = pmyInterface->GetData( &(pSafeArgs.m_psa) );
 
Share this answer
 
The problem is in the destruction of the CComSafeArray. When you assign the data directly to CComSafeArray.m_psa either by
C++
pmyInterface->GetData( ((SAFEARRAY**)&pSafeArgs )))

or
C++
pmyInterface->GetData( &(pSafeArgs.m_psa) );

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900