设为首页收藏本站

晓东CAD家园-论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 37|回复: 0

ActiveX client with MFC ClassWizard or #import

[复制链接]

已领礼包: 145个

财富等级: 日进斗金

发表于 2021-1-11 23:32:46 | 显示全部楼层 |阅读模式

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

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

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.

  1. // This is declared as a member variable in the CSampActiveXClientDlg.
  2. // Given that you've use ClassWizard to import IAcadApplication interface
  3. // from acad.tlb.
  4. IAcadApplication m_IApp;

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

  12. BeginWaitCursor();
  13. CoInitialize(NULL);
  14. hr = ::CLSIDFromProgID(L"AutoCAD.Application", &clsid);

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

  25. m_IApp.SetVisible(TRUE);

  26. }
  27. else
  28. AfxMessageBox("Acad.Application is not registered!");

  29. EndWaitCursor();
  30. }
  31. This code snippet is to be used with #import.
  32. // your location of acad.tlb may vary
  33. #import "c:\acad2k\acad.tlb" raw_interfaces_only, raw_native_types,
  34. no_namespace, named_guids  

  35. CComQIPtr<IAcadApplication, &IID_IAcadApplication> gpApp;

  36. // start Acad
  37. HRESULT InitAcad()
  38. {
  39. HRESULT hr = S_OK;
  40. CLSID clsid;
  41. // get acad running first
  42. hr = CLSIDFromProgID(L"AutoCAD.Application", &clsid);
  43. if(FAILED(hr))
  44. return hr;
  45. CComPtr<IUnknown> pIUnk;
  46. hr = GetActiveObject(clsid, NULL, pIUnk);
  47. if(!pIUnk)
  48. {
  49.   hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
  50.   IID_IAcadApplication, (void**)&gpApp);[
  51. }
  52. else
  53.   gpApp = pIUnk;
  54. if(FAILED(hr) || !gpApp)
  55. return hr;
  56. gpApp->put_Visible(VARIANT_TRUE);

  57. return hr;
  58. }


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

本版积分规则

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

GMT+8, 2021-1-22 08:17 , Processed in 0.103233 second(s), 12 queries , Gzip On, WinCache On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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