Solved

How to get a pointer to an object from a IDispatch pointer ?

Posted on 2004-04-30
5
490 Views
Last Modified: 2013-11-18
Hope i post in the correct topic: I'm programming in C++ with COM to control Autocad.
--------------------------------------------
For those not familar with Autocad programming, here is what is, i believe, a COM problem : at some part of my code, I have to transform a IAcadObjectPtr (derived from IDispatch) to a IAcadAttributeReferencePtr (derived from IAcadObject). This line does the job :
pAcadAttribRef = pAcadObject;
I have to execute this line very often in my program, and it's very slow : 1500 executions of that line take about 30 seconds on a P4 2.8 (with debug code, but release is not much faster) !
When i enter into that line, i see that's just a call to QueryInterface(). Is there a way to accelerate it ?

--------------------------------------------
For those familar with Autocad programming, here is the all story :
Given a BlockReference, i need to get one of its AttributeReference from the Attribute tag (its name). I wrote this function :

IAcadAttributeReferencePtr DrvAcad::FindAttributeRefByTag(IAcadBlockReferencePtr pAcadBlockRef, const CString& strTag)
{
_variant_t arrayAttrib; // array of AttributeRef objects
long lbound, ubound;
IAcadAttributeReferencePtr AttribRef = NULL;
IAcadObjectPtr pObject;
arrayAttrib = pAcadBlockRef->GetAttributes();
SafeArrayGetLBound(arrayAttrib.parray, 1, &lbound);
SafeArrayGetUBound(arrayAttrib.parray, 1, &ubound);
for (long i = lbound; i <= ubound; i++)
{
SafeArrayGetElement(arrayAttrib.parray, &i, &pObject);
AttribRef = pObject; // very slow !
if (AttribRef->GetTagString() == strTag)
break;
}
return AttribRef;
}

GetAttributes() returns an array, but the array elements are not directly AttributeReferences; they are AcadObjects that i have to transform in AttributeReferences to finally be able to call AttribRef->GetTagString(). Anyway that's what i have been told, and i didn't find another way to get an attributeRef.

In some part of my program, i process about 300 block references and i have to call this function 5 times for each blockref (i need to set the values for 5 attributes). On a P4 2.8Ghz it takes almost 1 minute (with debug code, but release is not much faster). Much too slow because clients may have thousands of blocks to process !
By commenting some parts of the code and timing, i get to the conclusion that one of the main source of 'slowness' is the line AttribRef = pObject : it's responsible for about 30 secondes. When i enter into that line, i see that's just a QueryInterface().
I have to say i don't really understand the fine lines of COM programming (that would take me weeks of learning ...), i just try to pilot Autocad.
If someone has an explanation, or better a faster way for getting an AttributeRef ...
0
Comment
Question by:JP_Goblet
5 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 10958681
Nope - you have to QueryInterface.

Best option for speeding up is to cache the result, if you're going to do the conversion on the same pointer lots of times.

Cross-process COM is actually pretty slow. (As we now know, to our cost!)

0
 

Author Comment

by:JP_Goblet
ID: 10959183
I'm not sure that caching that sort of  pointer is a good idea : this pointer is created and managed by Autocad itself, i have no garantee it will stay the same during the lifespan of my application.

Anyway my problem is now resolved ... by rebooting : after reboot the debug code is still relatively slow (times have been halved) but release code is now fast enough : just a few seconds.  I can think of a possible explication for this salvating reboot : in debug mode i frequently stop my application brutally (stop debugging in the visual studio) --> probably all the stuff associated with COM was not cleanly removed; after several of these stops, the COM processing was frozen in all that garbage ? ...

I consider the question is closed.

JPG
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10989765
Closed, 250 points refunded.

modulo
Community Support Moderator
Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now