一个COM示例程序
void CEx24cView::OnTestSpaceship()
{
CLSID clsid;
LPCLASSFACTORY pClf;
LPUNKNOWN pUnk;
IMotion* pMot;
IVisual* pVis;
HRESULT hr;
if ((hr = ::CLSIDFromProgID(L"Spaceship", &clsid)) != NOERROR)
{
TRACE("unable to find Program ID -- error = %x\n", hr);
return;
}
if ((hr = ::CoGetClassObject(clsid, CLSCTX_INPROC_SERVER,
NULL, IID_IClassFactory, (void **) &pClf)) != NOERROR) {;
TRACE("unable to find CLSID -- error = %x\n", hr);
return;
}
pClf->CreateInstance(NULL, IID_IUnknown, (void**) &pUnk);
pUnk->QueryInterface(IID_IMotion, (void**) &pMot); // All three
pMot->QueryInterface(IID_IVisual, (void**) &pVis); // pointers
// should work
TRACE("main: pUnk = %p, pMot = %p, pDis = %p\n", pUnk, pMot, pVis);
// Test all the interface virtual functions
pMot->Fly();
int nPos = pMot->GetPosition();
TRACE("nPos = %d\n", nPos);
pVis->Display();
pClf->Release();
pUnk->Release();
pMot->Release();
pVis->Release();
AfxMessageBox("Test succeeded. See Debug window for output.");
}
{
CLSID clsid;
LPCLASSFACTORY pClf;
LPUNKNOWN pUnk;
IMotion* pMot;
IVisual* pVis;
HRESULT hr;
if ((hr = ::CLSIDFromProgID(L"Spaceship", &clsid)) != NOERROR)
{
TRACE("unable to find Program ID -- error = %x\n", hr);
return;
}
if ((hr = ::CoGetClassObject(clsid, CLSCTX_INPROC_SERVER,
NULL, IID_IClassFactory, (void **) &pClf)) != NOERROR) {;
TRACE("unable to find CLSID -- error = %x\n", hr);
return;
}
pClf->CreateInstance(NULL, IID_IUnknown, (void**) &pUnk);
pUnk->QueryInterface(IID_IMotion, (void**) &pMot); // All three
pMot->QueryInterface(IID_IVisual, (void**) &pVis); // pointers
// should work
TRACE("main: pUnk = %p, pMot = %p, pDis = %p\n", pUnk, pMot, pVis);
// Test all the interface virtual functions
pMot->Fly();
int nPos = pMot->GetPosition();
TRACE("nPos = %d\n", nPos);
pVis->Display();
pClf->Release();
pUnk->Release();
pMot->Release();
pVis->Release();
AfxMessageBox("Test succeeded. See Debug window for output.");
}
// interface.h
struct IMotion : public IUnknown
{
STDMETHOD_(void, Fly) () = 0;
STDMETHOD_(int&, GetPosition) () = 0;
};
struct IVisual : public IUnknown
{
STDMETHOD_(void, Display) () = 0;
};
{
STDMETHOD_(void, Fly) () = 0;
STDMETHOD_(int&, GetPosition) () = 0;
};
struct IVisual : public IUnknown
{
STDMETHOD_(void, Display) () = 0;
};
// Spaceship.h : header file

// Spaceship.cpp : implementation file

正规DLL部分代码
/////////////////////////////////////////////////////////////////////////////
// CEx24bApp initialization
BOOL CEx24bApp::InitInstance()
{
// Register all OLE server (factories) as running. This enables the
// OLE libraries to create objects from other applications.
COleObjectFactory::RegisterAll();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Special entry points required for inproc servers
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllGetClassObject(rclsid, riid, ppv);
}
STDAPI DllCanUnloadNow(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return AfxDllCanUnloadNow();
}
// by exporting DllRegisterServer, you can use regsvr.exe
STDAPI DllRegisterServer(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleObjectFactory::UpdateRegistryAll();
return S_OK;
}
要想运行客户端和组件程序,必须先注册组件来更新注册表,可以使用如下的代码:
BOOL CRegCompApp::InitInstance()
{
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// make sure to set Explorer options to allow DLLs to be visible
CSpecialFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"OCX files (*.ocx)|*.ocx|DLL files (*.dll)|*.dll||");
dlgFile.m_ofn.lpstrTitle = "注册OCX/DLL File";
if(dlgFile.DoModal() != IDOK) return FALSE;
CString strDllPath = dlgFile.GetPathName();//获取文件名
// this wouldn't work for a dynamically linked Regular DLL
HINSTANCE h = ::LoadLibrary(strDllPath);//加载dll
if(h == NULL)
{
CString msg;
msg.Format("Failed to find server %s", strDllPath);
AfxMessageBox(msg);
return FALSE;
}
FARPROC pFunc = ::GetProcAddress((HMODULE) h, "DllRegisterServer");
if(pFunc == NULL) {
AfxMessageBox("Failed to find DllRegisterServer function");
return FALSE;
}
(*pFunc)(); // call the function to register the server 注册
AfxMessageBox("Server registered OK");
return FALSE;
}
{
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// make sure to set Explorer options to allow DLLs to be visible
CSpecialFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"OCX files (*.ocx)|*.ocx|DLL files (*.dll)|*.dll||");
dlgFile.m_ofn.lpstrTitle = "注册OCX/DLL File";
if(dlgFile.DoModal() != IDOK) return FALSE;
CString strDllPath = dlgFile.GetPathName();//获取文件名
// this wouldn't work for a dynamically linked Regular DLL
HINSTANCE h = ::LoadLibrary(strDllPath);//加载dll
if(h == NULL)
{
CString msg;
msg.Format("Failed to find server %s", strDllPath);
AfxMessageBox(msg);
return FALSE;
}
FARPROC pFunc = ::GetProcAddress((HMODULE) h, "DllRegisterServer");
if(pFunc == NULL) {
AfxMessageBox("Failed to find DllRegisterServer function");
return FALSE;
}
(*pFunc)(); // call the function to register the server 注册
AfxMessageBox("Server registered OK");
return FALSE;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2007-11-17 22:12 Phinecos(洞庭散人) 阅读(1215) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述