一个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.");
}
// interface.h

struct IMotion : public IUnknown
{
    STDMETHOD_(void, Fly) () = 0;
    STDMETHOD_(int&, GetPosition) () = 0;
};

struct IVisual : public IUnknown
{
    STDMETHOD_(void, Display) () = 0;
};
// Spaceship.h : header file

Code
// Spaceship.cpp : implementation file

Code
正规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;
}


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/11/17/962867.html,如需转载请自行联系原作者
目录
相关文章
|
ice 网络协议
二 示例程序一
示例程序一 1) ICE文件 Printer.ice module demo {     interface Printer     {         void printString(string s);     }; }; 2) Server端开发 选择Win32控制台空白项目 添加Iced.
906 0
|
缓存 Ubuntu Linux
安装HElib并运行示例程序
安装HElib并运行示例程序
安装HElib并运行示例程序
|
存储 分布式计算 Ubuntu
示例程序WordCount的执行
示例程序WordCount的执行
CrystalSideBar控件用法示例程序
【CrystalSideaBr教程】CrystalSideBar控件用法示例程序(怎么实现分组显示即仿QQ功能) 什么是“CrystalSideaBar”? CrystalSideaBar是一个能将信息分组显示的控件,仿DevComponents公司的SideBar控件的Office2007效果。
707 0
QTP测试示例程序
本文为使用QTP测试自带例子航班订票系统的测试: 下面为录制脚本的截屏:   对应的脚本为 Dialog("Login").Check CheckPoint("Login") Dialog("Login").
830 0
|
存储 网络协议 API
Socket编程指南及示例程序
例子代码就在我的博客中,包括六个UDP和TCP发送接受的cpp文件,一个基于MFC的局域网聊天小工具工程,和此小工具的所有运行时库、资源和执行程序。代码的压缩包位置是http://www.blogjava.net/Files/wxb_nudt/socket_src.rar。
1424 0
|
存储 Go
如何使用go遍历目录获取文件列表
如何使用go遍历目录获取文件列表
763 0
|
Web App开发 存储 JSON
.NetCore上传多文件的几种示例
本章和大家分享的是.NetCore的MVC框架上传文件的示例,主要讲的内容有:form方式提交上传,ajax上传,ajax提交+上传进度效果,Task并行处理+ajax提交+上传进度,相信当你读完文章内容后能后好的收获,如果可以不妨点个赞;由于昨天电脑没电了,快要写完的内容没有保存,今天早上提前来公...
1623 0

热门文章

最新文章