• C++批量加载动态库函数方法


    1、枚举定义
    enum
      {
        // 0 - GigE DLL (implicitly called)
        Func_isVersionCompliantDLL,
        Func_isDriverAvailable,

      }                                                  
      SVGigE_FUNCTION;


    2、函数管理器:定义函数指针、ID(使用枚举)、函数名
    struct _GigEFunc
    {
      FARPROC function_pointer;
      SVGigE_FUNCTION function_id;
      char *function_name;
    }
    GigEFunc[] =
    {
      // 0 - GigE DLL (implicitly called)
      NULL, Func_isVersionCompliantDLL,                     "isVersionCompliantDLL",
      NULL, Func_isDriverAvailable,                        "isDriverAvailable",
    }

    3、加载动态库,初始化函数指针
    HINSTANCE GigEDLL = NULL;

    bool
    isLoadedGigEDLL() { if( NULL == GigEDLL ) { // Try to load GigE DLL GigEDLL = LoadLibrary(SVGigE_DLL); // Check DLL availability if( NULL == GigEDLL ) return false; } // Check if size of function table matches the number of imported functions int FunctionCount = sizeof(GigEFunc) / sizeof(struct _GigEFunc); if( FunctionCount != Func_isVersionCompliantDLL_consistency_check + 1 ) return false; // Obtain CameraContainer procedure addresses bool function_loaded = true; for( int function_index = Func_isVersionCompliantDLL; function_index < (sizeof(GigEFunc) / sizeof(struct _GigEFunc)); function_index++ ) { GigEFunc[function_index].function_pointer = GetProcAddress(GigEDLL, GigEFunc[function_index].function_name); // Check if function was found if( NULL == GigEFunc[function_index].function_pointer ) function_loaded = false; } // Check if all function pointers could successfully be obtained from the DLL if( function_loaded == false ) return false; else return true; }

    4、定义函数指针

    typedef SVGigE_RETURN
    (*TFunc_isVersionCompliantDLL)(SVGigE_VERSION *DllVersion,
                                   SVGigE_VERSION *ExpectedVersion);

    typedef SVGigE_RETURN(*TFunc_isDriverAvailable)();
    5、外部访问函数接口
    SVGigE_RETURN
    isVersionCompliantDLL(SVGigE_VERSION *DllVersion, 
                          SVGigE_VERSION *ExpectedVersion)
    {
      // Check DLL availability
      if( NULL == GigEDLL )    //HINSTANCE  GigEDLL = NULL  hInstance是操作系统分配给实例的指针. 程序根据hInstance访问其相应的内存空间
      {
        // Try to load SVGigE DLL
        if( !isLoadedGigEDLL() )
          return SVGigE_DLL_NOT_LOADED;
      }
    
      // Pass through function call to DLL
     //
     // 2011-08-22/EKantz: check consistency of the whole function pointer 
     //                    table by calling the last function in that table.
     //
      return ((TFunc_isVersionCompliantDLL)
      GigEFunc[Func_isVersionCompliantDLL_consistency_check].function_pointer)(DllVersion, ExpectedVersion);
    }
    
    SVGigE_RETURN
    isDriverAvailable()
    {
      // Check DLL availability
      if( NULL == GigEDLL )
      {
        // Try to load SVGigE DLL
        if( !isLoadedGigEDLL() )
          return SVGigE_DLL_NOT_LOADED;
      }
    
      // Pass through function call to DLL
      return ((TFunc_isDriverAvailable)
      GigEFunc[Func_isDriverAvailable].function_pointer)();
    }
    
    
    
     

     

  • 相关阅读:
    学习vim命令:“:w !sudo tee %”
    mac下安装和卸载软件
    很好用的在线markdown编辑器
    doc2vec 利用gensim 生成文档向量
    C语言经典算法100例-024-求数列的前20 项和,2/1,3/2,5/3,8/5...
    C语言经典算法100例-023-打印菱形
    C语言经典算法100例-022-乒乓球比赛名单问题
    C语言经典算法100例-021-猴子吃桃问题
    C语言经典算法100例-020-小球自由下落问题
    C语言经典算法100例-019-求完数
  • 原文地址:https://www.cnblogs.com/profession/p/10251589.html
Copyright © 2020-2023  润新知