成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

淺析C++動態加載DLL在Windows Mobile下實現

開發 后端 移動開發
這里我們將簡單介紹Windows Mobile下Native C++動態加載DLL的實現,希望能對大家有所幫助。

我們將在這里為大家講解Windows Mobile下Native C++動態加載DLL的實現,一般來說靜態加載是很簡單的事情,我們在本文中也將會提到。主要還是講解C++動態加載DLL。

簡介

本文以Windows Mobile Sensors API庫為例子講述在Windows Mobile下使用Native C++動態加載DLL的方法。

靜態加載DLL的方法

使用Native C++的開發,一般使用靜態加載的方法加載DLL,所謂靜態加載就是在程序編譯時(Compile Time)直接調用DLL的頭文件定義的函數,鏈接時(Link Time)鏈接*.lib文件指向DLL的接口,在程序開始運行時(Run Time Start up)加載DLL。

下面講述使用靜態加載DLL的方法,在程序中需要指定加載的*.lib文件,用于鏈接(Link)。

#pragma comment(lib, "SamsungMobileSDK_1.lib")

使用靜態加載的DLL可以直接調用頭文件定義的函數,例如:

  1. SmiAccelerometerCapabilities cap;
  2. if( SmiAccelerometerGetCapabilities(&cap) != SMI_SUCCESS){    throw;}
  3. SmiAccelerometerHandler h = &GetVectorHandler;if(SmiAccelerometerRegisterHandler(1000, h) != SMI_SUCCESS)
  4. {    throw;} 

SmiAccelerometerGetCapabilities()和SmiAccelerometerRegisterHandler()是定義在頭文件smiAccelerometer.h中的,可以直接調用,定義如下:

使用靜態加載的方法使用方面還是很方便的,可是在動態加載的時候就不能直接調用頭文件的函數了,增加了復雜度,下面會講到。

  1. /**  
  2. *  Start receiving accelerometer data periodically.   
  3.  
  4. *  The period interval must be a multiple of the callbackPeriod specified   
  5. *  in SmiAccelerometerCapabilities. If it is less than the callbackPeriod, it will be   
  6. *  set to the callbackPeriod. If it is not a multiple of the callbackPeriod, it will be   
  7. *  truncated to fit the value. ( (period / callbackPeriod)   
  8. * callbackPeriod )   
  9. *     
  10. *  Only one handler per process is allowed. Successive calls per process will replace the previous handler   
  11. *  function and period.   
  12. *  
  13. *  @param    period    [in]    callback interval.      
  14. *  @param    handler   [in]    callback function for every period interval.     
  15.  
  16. *  @return                      
  17. *                              SMI_SUCCESS on success   
  18. *  \n                          SMI_ERROR_INVALID_PARAMETER if the handler input parameter is NULL   
  19. *  \n                          SMI_ERROR_DEVICE_NOT_FOUND if the device is not present or supported   
  20. *  \n                          SMI_ERROR_CANNOT_ACTIVATE_SERVER if the sensor server cannot be started   
  21. */ SMI_API SMI_RESULT SmiAccelerometerRegisterHandler(UINT period, SmiAccelerometerHandler handler); 

C++動態加載DLL的方法

靜態加載DLL是比較簡單的開發方法,可是有個缺點是程序開始運行的時候就需要加載DLL,如果該DLL不存在,程序就不能啟動了。由于Windows Mobile Sensors API庫需要自適應具體的設備,也就是說Windows Mobile Sensors API庫不能依賴于具體設備的Sensor庫,所以不能使用靜態加載的方法來引用DLL。下面講述動態加載DLL的方法。

定義指向函數的指針

動態加載DLL,需要根據頭文件來定義指向函數的指針,如下:

  1. typedef UINT (WINAPI * PFN_SmiAccelerometerGetVector)(SmiAccelerometerVector*);  
  2. typedef UINT (WINAPI * PFN_SmiAccelerometerGetCapabilities)(SmiAccelerometerCapabilities*);  
  3. typedef UINT (WINAPI * PFN_SmiAccelerometerRegisterHandler)(UINT, SmiAccelerometerHandler);typedef UINT (WINAPI * PFN_SmiAccelerometerUnregisterHandler)();  
  4. PFN_SmiAccelerometerGetVector pfnSmiAccelerometerGetVector;PFN_SmiAccelerometerGetCapabilities   
  5. pfnSmiAccelerometerGetCapabilities;PFN_SmiAccelerometerRegisterHandler   
  6. pfnSmiAccelerometerRegisterHandler;PFN_SmiAccelerometerUnregisterHandler pfnSmiAccelerometerUnregisterHandler; 

這些指向函數的指針可以對應下面的在smiAccelerometer.h頭文件的函數進行定義:

  1. SMI_API SMI_RESULT SmiAccelerometerGetVector(SmiAccelerometerVector *accel);  
  2. SMI_API SMI_RESULT SmiAccelerometerGetCapabilities(SmiAccelerometerCapabilities *capabilities);  
  3. SMI_API SMI_RESULT SmiAccelerometerRegisterHandler(UINT period, SmiAccelerometerHandler handler);  
  4. SMI_API SMI_RESULT SmiAccelerometerUnregisterHandler(); 

定義是一一對應的。參數入口和返回值都必須完全一致。

初始化指向函數的指針

初始化指向函數的指針的過程也就是動態加載DLL的過程,代碼如下:

  1. #define SAMSUNG_SENSOR_DLL  
  2. L"SamsungMobilesdk_1.dll"HMODULE hSensorLib = LoadLibrary (SAMSUNG_SENSOR_DLL);if (NULL == hSensorLib){      
  3. printf("Unable to load Samsung Sensor DLL\n");      
  4. throw std::runtime_error("Unable to load Samsung Sensor DLL");}  
  5. pfnSmiAccelerometerGetVector = (PFN_SmiAccelerometerGetVector)      
  6. GetProcAddress(hSensorLib, L"SmiAccelerometerGetVector");pfnSmiAccelerometerGetCapabilities = (PFN_SmiAccelerometerGetCapabilities)      
  7. GetProcAddress(hSensorLib, L"SmiAccelerometerGetCapabilities");pfnSmiAccelerometerRegisterHandler = (PFN_SmiAccelerometerRegisterHandler)      
  8. GetProcAddress(hSensorLib, L"SmiAccelerometerRegisterHandler");pfnSmiAccelerometerUnregisterHandler = (PFN_SmiAccelerometerUnregisterHandler)     
  9. GetProcAddress(hSensorLib, L"SmiAccelerometerUnregisterHandler");if (NULL == pfnSmiAccelerometerGetVector){    printf("Unable to find entry point of SmiAccelerometerGetVector\n");      
  10. throw std::runtime_error("Unable to find entry point of SmiAccelerometerGetVector");}if (NULL == pfnSmiAccelerometerGetCapabilities){      
  11. printf("Unable to find entry point of SmiAccelerometerGetCapabilities\n");      
  12. throw std::runtime_error("Unable to find entry point of SmiAccelerometerGetCapabilities");}if (NULL == pfnSmiAccelerometerRegisterHandler){      
  13. printf("Unable to find entry point of SmiAccelerometerRegisterHandler\n");      
  14. throw std::runtime_error("Unable to find entry point of SmiAccelerometerRegisterHandler");}if (NULL == pfnSmiAccelerometerUnregisterHandler){      
  15. printf("Unable to find entry point of SmiAccelerometerUnregisterHandler\n");      
  16. throw std::runtime_error("Unable to find entry point of SmiAccelerometerUnregisterHandler");} 

LoadLibrary()函數動態加載DLL,GetProcAddress()根據函數的名字 加載函數的入口地址 到指向函數的指針。有點繞口,sorry。如果地址不為空,那么可以根據這個地址調用相應的函數。

調用函數

調用函數的方法和靜態加載DLL的方法一樣,但是不是直接調用函數的名字,而是使用指向函數的指針來調用,下面的例子可以和靜態加載DLL函數調用的例子對比來看。

  1. SmiAccelerometerCapabilities cap;   
  2. if( pfnSmiAccelerometerGetCapabilities(&cap) != SMI_SUCCESS)  
  3.  {      
  4. throw;}SmiAccelerometerHandler h = &GetVectorHandler;  
  5. if(pfnSmiAccelerometerRegisterHandler(1000, h) != SMI_SUCCESS)  
  6. {    throw;} 

C++動態加載DLL的方法就完成了。

.NET的世界

下面這段表述不對,請看下面的回復。.NET使用DllImport屬性進行P/Invoke不應該叫做動態加載,因為不能卸載,應該叫做按需加載,就是在call這個函數的時候才加載,而不是在程序啟動的時候加載。按需加載和靜態加載的區別是加載的時間不一樣。

{

在.NET里面P/Invoke一個DLL里面的函數全部都是動態加載(這是錯的,謝謝Wuya指出,這里應該叫做按需加載,動態加載的方法可以見Wuya到回復)的,使用DllImport屬性來定義。如果SmiAccelerometerUnregisterHandler()函數使用在.NET下會定義如下:

[DllImport("SamsungMobileSDK_1.dll", CharSet = CharSet.Auto, SetLastError = true)]private static extern uint SmiAccelerometerUnregisterHandler();使用.NET比使用Native C++動態加載相對簡單。

}

關于Mobile Sensors API項目

這個項目還是在起步階段,當前實現了samsung的重力感應器,我把項目host到 Mobile Sensors API - Native unified APIs for Windows Mobile Sensors 了,我會持續改進,把各種sensors的實現到這個項目中。

源代碼:http://mobilesensor.codeplex.com/SourceControl/ListDownloadableCommits.aspx

環境:VS2008 + WM 6 professional SDK + Samsung Windows Mobile SDK

原文標題:Windows Mobile下Native C++動態加載DLL

鏈接:http://www.cnblogs.com/procoder/archive/2009/09/25/1573712.html

【編輯推薦】

  1. C++之運算符重載,輸入輸出流詳細介紹
  2. C++、Java與C#的命名規范總結
  3. C++類成員函數的重載、覆蓋與隱藏
  4. Eclipse下C/C++環境搭建
  5. Java與C++語言在作用域上的差異淺析
責任編輯:彭凡 來源: 博客園
相關推薦

2009-09-09 13:47:38

C++訪問SqlCe

2010-01-26 14:10:22

Visual C++

2011-06-24 09:01:20

Qt QLibrary dll

2010-01-13 14:18:52

Windows Emb

2010-01-14 17:13:53

C++接口

2011-06-08 14:42:33

C++多維數組

2010-01-18 14:41:52

Visual C++開

2010-01-26 09:50:30

C++接口

2010-01-25 13:31:27

C++程序

2009-07-31 17:28:35

C#語言調用DLL

2009-08-18 16:31:19

Visual C#編寫

2011-06-16 10:09:25

QT Windows DLL

2009-09-03 17:31:34

C#發送郵件

2009-09-18 13:28:41

Mobile Sens

2010-01-21 09:34:57

C++語法

2010-01-27 16:05:06

C++堆棧

2010-01-20 10:19:55

C++數組

2010-01-27 17:16:52

C++構造函數

2010-01-22 13:59:34

Visual C++應

2009-08-28 16:14:26

C#實現加載動態庫
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品欧美一区二区精品久久久 | 亚洲欧洲国产视频 | 久久精品91久久久久久再现 | 九色 在线 | 91高清视频在线观看 | 欧美日韩视频在线第一区 | 91国产视频在线 | 色爱av| 亚洲 欧美 日韩 精品 | 国产亚洲精品成人av久久ww | 一区二区三区四区不卡视频 | 夜夜艹| 91精品久久久久久久久99蜜臂 | aacc678成免费人电影网站 | 国产欧美精品一区二区 | 青青草在线视频免费观看 | 国产精品亚洲视频 | 九九久久久 | 欧美日韩国产一区二区 | 日韩欧美三区 | 日本一区二区三区四区 | 久久av影院| 久久尤物免费一区二区三区 | 亚洲欧美中文日韩在线v日本 | 国产一区二区三区精品久久久 | 国产成人精品久久二区二区91 | 精品毛片 | 久久久久一区二区三区 | 韩日在线视频 | 国产精品a久久久久 | 亚洲人精品午夜 | jav成人av免费播放 | 精品成人免费视频 | 一区二区三区四区av | 国产视频第一页 | 高清国产一区二区 | 日韩欧美一区在线 | 久久久久久久久久久91 | 亚洲第1页 | 日韩成人中文字幕 | 一区二区免费 |