Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-30
5
Medium Priority
?
512 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

885 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