设为首页收藏本站

晓东CAD家园-论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 25|回复: 1

ActiveX client with MFC ClassWizard or #import

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-11 20:36:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
问题:
Are there two ways of creating an AutoCAD ActiveX client using C++?


解答:
There are two ways of creating an AutoCAD Active X client using C++.

Method #1
Use MFC ClassWizard to import the interfaces (from acad.tlb) that you need for
your client application.

Method #2
Use #import with acad.tlb.

NOTE:  Although #import is something new from VC++ 5.0, it previously existed in
ClassWizard.

The following code segments demonstrate both methods, and is to be used with a
MFC ClassWizard-generated IAcadApplication interface.
// This is declared as a member variable in the CSampActiveXClientDlg.
// Given that you've use ClassWizard to import IAcadApplication interface
// from acad.tlb.

  1. IAcadApplication m_IApp;


  2. void CSampActiveXClientDlg::OnStartacad()
  3. {
  4. // TODO: Add your control notification handler code here
  5. HRESULT hr = NOERROR;
  6. CLSID clsid;
  7. LPUNKNOWN pUnk = NULL;
  8. LPDISPATCH pDisp = NULL;

  9. BeginWaitCursor();
  10. CoInitialize(NULL);
  11. hr = ::CLSIDFromProgID(L"AutoCAD.Application", &clsid);

  12. if (SUCCEEDED(hr))
  13. {
  14. if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)
  15. {
  16.   VERIFY(pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &pDisp) == S_OK);
  17.   m_IApp.AttachDispatch(pDisp);
  18.   pUnk->Release();
  19. }
  20. else
  21.   VERIFY(m_IApp.CreateDispatch(clsid) == TRUE);

  22. m_IApp.SetVisible(TRUE);

  23. }
  24. else
  25. AfxMessageBox("Acad.Application is not registered!");

  26. EndWaitCursor();
  27. }


This code snippet is to be used with #import.
// your location of acad.tlb may vary
#import "c:\acad2k\acad.tlb" raw_interfaces_only, raw_native_types,
no_namespace, named_guids  

  1. CComQIPtr<IAcadApplication, &IID_IAcadApplication> gpApp;

  2. // start Acad
  3. HRESULT InitAcad()
  4. {
  5. HRESULT hr = S_OK;
  6. CLSID clsid;
  7. // get acad running first
  8. hr = CLSIDFromProgID(L"AutoCAD.Application", &clsid);
  9. if(FAILED(hr))
  10. return hr;
  11. CComPtr<IUnknown> pIUnk;
  12. hr = GetActiveObject(clsid, NULL, pIUnk);
  13. if(!pIUnk)
  14. {
  15.   hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
  16.   IID_IAcadApplication, (void**)&gpApp);
  17. }
  18. else
  19.   gpApp = pIUnk;
  20. if(FAILED(hr) || !gpApp)
  21. return hr;
  22. gpApp->put_Visible(VARIANT_TRUE);

  23. return hr;
  24. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 604个

财富等级: 财运亨通

发表于 2021-1-11 20:59:45 | 显示全部楼层
N版,改成C十十了?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )  

GMT+8, 2021-1-22 08:10 , Processed in 0.102957 second(s), 19 queries , Gzip On, WinCache On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表