newer 发表于 2021-1-11 20:36:39

ActiveX client with MFC ClassWizard or #import

问题:
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.


IAcadApplication m_IApp;


void CSampActiveXClientDlg::OnStartacad()
{
// TODO: Add your control notification handler code here
HRESULT hr = NOERROR;
CLSID clsid;
LPUNKNOWN pUnk = NULL;
LPDISPATCH pDisp = NULL;

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

if (SUCCEEDED(hr))
{
if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)
{
VERIFY(pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &pDisp) == S_OK);
m_IApp.AttachDispatch(pDisp);
pUnk->Release();
}
else
VERIFY(m_IApp.CreateDispatch(clsid) == TRUE);

m_IApp.SetVisible(TRUE);

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

EndWaitCursor();
}


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


CComQIPtr<IAcadApplication, &IID_IAcadApplication> gpApp;

// start Acad
HRESULT InitAcad()
{
HRESULT hr = S_OK;
CLSID clsid;
// get acad running first
hr = CLSIDFromProgID(L"AutoCAD.Application", &clsid);
if(FAILED(hr))
return hr;
CComPtr<IUnknown> pIUnk;
hr = GetActiveObject(clsid, NULL, pIUnk);
if(!pIUnk)
{
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
IID_IAcadApplication, (void**)&gpApp);
}
else
gpApp = pIUnk;
if(FAILED(hr) || !gpApp)
return hr;
gpApp->put_Visible(VARIANT_TRUE);

return hr;
}


/db_自贡黄明儒_ 发表于 2021-1-11 20:59:45

N版,改成C十十了?
页: [1]
查看完整版本: ActiveX client with MFC ClassWizard or #import