博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个COM示例程序
阅读量:6980 次
发布时间:2019-06-27

本文共 3095 字,大约阅读时间需要 10 分钟。

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,如需转载请自行联系原作者
你可能感兴趣的文章
AM335x kernel 4.4.12 i2c eeprom AT24c02驱动移植
查看>>
How To Tune or Test PLSQL Code Performance in Oracle D2k Forms
查看>>
使用maven的profile切换项目各环境的参数
查看>>
Android APP 性能优化的一些思考
查看>>
elasticsearch控制台中文乱码和jvm内存大小调整。 解决办法:
查看>>
百度地图隐藏LOGO显示
查看>>
poj 3630 简单Trie树的应用
查看>>
[Jobdu] 题目1391:顺时针打印矩阵
查看>>
DWG TrueView 2010 下载地址
查看>>
之前玩GAE收藏夹里的链接
查看>>
迅速找出重要程序问题 - 来自《软件测试经验和教训总结》
查看>>
XML基础知识
查看>>
如何用C#写一个简单的Login窗口
查看>>
1057. Stack (30) (浙大13年机试题)
查看>>
WAS 6.1 jsp不支持泛型解决办法
查看>>
批处理学习笔记4 - 通配符* ?学习
查看>>
学生管理系统调试——实时错误(实时错误“424”“5”“91”)
查看>>
开闭原则(设计模式6)
查看>>
Sharepoint学习笔记—习题系列--70-576习题解析 -(Q141-Q143)
查看>>
sudo配置文件/etc/sudoers格式
查看>>