Recommended way to access AutoCAD Interface Object using C++

By Augusto Goncalves

The common way to access AutoCAD interface object is GetActiveObject, but this may return null on certain scenarios, such as On_kInitAppMsg, and is not guaranteed to access the current AutoCAD.

Since the ObjectARX application is loaded into AutoCAD process space, there is no need to go to running object’s table to get the COM pointer. So we can get it directly using the following code snippet (which requires MFC support):

IAcadApplicationPtr pAcadApp;

IDispatch* pDispatch = acedGetAcadWinApp()->GetIDispatch(FALSE);

pDispatch->QueryInterface(__uuidof(IAcadApplication),

                         (void**)&pAcadApp);


Comments

4 responses to “Recommended way to access AutoCAD Interface Object using C++”

  1. daniel Avatar
    daniel

    how about acedGetIDispatch?

  2. It’s basically the same. The key here is use GetIDispatch instead GetObject. Thanks.

  3. Personally, I like this way – it’s very slick and succinct, best of all it automatically cleans up COM reference counts when the function returns…
    #import “acax18ENU.tlb”
    // get the dispatch driver from autocad
    CComQIPtr acad = acedGetAcadWinApp()->GetIDispatch(TRUE);
    // now, for exaple, get the menu bar
    CComQIPtr menuBar = acad->GetMenuBar();
    Also, passing FALSE for the GetIDispatch() is only used if you want the interface object to live past the end of the function, normally it would always be TRUE. Obviously, we can’t see the context that Augusto is using that code in so I just wanted to say.

  4. oops, the less that/greater than symbols are not there…
    // get the dispatch driver from autocad
    CComQIPtrLESSTHANAutoCAD::IAcadApplicationGREATERTHAN acad = acedGetAcadWinApp()->GetIDispatch(TRUE);
    // now get the menu bar
    CComQIPtrLESSTHANAutoCAD::IAcadMenuBarGREATERTHAN menuBar = acad->GetMenuBar();

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading