zoukankan      html  css  js  c++  java
  • com学习笔记(4)动态链接

        之前写的com组件与主程序是一起的。虽然都可以称为组件。但为了分发方便,可重用性强。以动态性链接库的方式则更会一些,也即将com组件写成以dll的动态链接形式。

    一.从dll中输出函数

    常用的 extern "C" 是把导出函数声明为C编译。由于C++编译器在编译的时候会造成其函数名的改变,在其他应用程序中导致函数不可调用,而C编译器则不会在编译后改变其函数名。这样如果用C编译的程序来调用该dll中的函数时,可能会造成找不到该函数。

    http://baike.baidu.com/view/963932.htm

    //
    // Creation function
    //
    extern "C" IUnknown* CreateInstance()
    {
        IUnknown* pI = static_cast<IX*>(new CA) ;
        pI->AddRef() ;
        return pI ;
    }
    

    可用DUMPBIN.EXE输出函数

    二.DLL的装载

    1.LoadLibrary方法载入dll,获取句柄

    2.GetProcAddress获取dll句柄和函数名称返回函数指针

    typedef IUnknown* (*CREATEFUNCPTR)() ;
    
    IUnknown* CallCreateInstance(char* name)
    {
        // Load dynamic link library into process.
        HINSTANCE hComponent = ::LoadLibrary(name) ;
        if (hComponent == NULL)
        {
            cout << "CallCreateInstance:\tError: Cannot load component." << endl ;
            return NULL ;
        }
    
        // Get address for CreateInstance function.
        CREATEFUNCPTR CreateInstance 
            = (CREATEFUNCPTR)::GetProcAddress(hComponent, "CreateInstance") ;
        if (CreateInstance == NULL)
        {
            cout  << "CallCreateInstance:\tError: "
                  << "Cannot find CreateInstance function."
                  << endl ;
            return NULL ;
        }
    
        return CreateInstance() ;
    }
    

    三.概念

    dll是组件的一种形式,可看成是组建的接口集

    组件非dll

    这种设计有利于松散耦合

    可共享地址空间

    dll又称为进程中的服务器

  • 相关阅读:
    Git 远程操作详解
    Golang io标准库
    Golang strings标准库
    Go WebSocket 实现
    Golang Gorm零值数据更新小坑
    [Linux] 分区扩容
    即截即贴,推荐一个提升截图对比效率的工具Snipaste
    POI 替换 word 关键字并保留样式
    前端图片压缩与 zip 压缩
    ubuntu20更换内核
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1517365.html
Copyright © 2011-2022 走看看