Analysis on How to dynamically load DLL in C ++ on Windows Mobile

Source: Internet
Author: User

We will explain the implementation of Native C ++ dynamic DLL loading in Windows Mobile. Static Loading is generally very simple and will be mentioned in this article. This article mainly explains how to dynamically load DLL in C ++.

Introduction

This document uses the Windows Mobile Sensors API library as an example to describe how to use Native C ++ to dynamically load DLL in Windows Mobile.

Static DLL Loading Method

Native C ++ development is generally used to load the DLL using the static loading method. The so-called static loading is to directly call the function defined by the DLL header file during program compilation (Compile Time, link Time Link *. the lib file points to the DLL interface and loads the DLL when the program starts running.

The following describes how to use static DLL loading. in the program, you must specify the loaded *. lib file for Link ).

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

Static DLL can be used to directly call the function defined by the header file, for example:

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

SmiAccelerometerGetCapabilities () and SmiAccelerometerRegisterHandler () are defined in the header file smiAccelerometer. h and can be called directly. The definition is as follows:

Static Loading is easy to use. However, during dynamic loading, you cannot directly call the function of the header file. This increases the complexity.

 
 
  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 ++ dynamic DLL Loading Method

Static DLL loading is a relatively simple development method, but there is a drawback that when the program starts running, it needs to load the DLL. If the DLL does not exist, the program cannot start. Because the Windows Mobile Sensors API library needs to adapt to specific devices, that is to say, the Windows Mobile Sensors API library cannot depend on the Sensor library of specific devices, so you cannot use static loading methods to reference DLL. The following describes how to dynamically load a DLL.

Define pointer to function

To dynamically load a DLL, define the pointer to the function according to the header file, as shown below:

 
 
  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; 

These pointers to functions can be defined according to the following functions in the smiAccelerometer. h header file:

 
 
  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(); 

The definitions are one-to-one. The parameter entries and return values must be exactly the same.

Initialize pointer to function

The initialization process of the pointer to the function is the process of dynamically loading the DLL. The Code is as follows:

 
 
  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");} 

The LoadLibrary () function dynamically loads DLL. GetProcAddress () loads the function entry address based on the function name to the pointer to the function. A little detour, sorry. If the address is not blank, you can call the corresponding function based on the address.

Call a function

The method for calling a function is the same as that for loading a DLL statically, but instead of calling the function name directly, it is called using a pointer to the function, the following example can be compared with the static loading DLL function call example.

 
 
  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 ++ completes the dynamic DLL loading method.

. NET World

The following paragraph is incorrect. Please refer to the following reply .. NET using the DllImport attribute for P/Invoke should not be called dynamic loading, because it cannot be uninstalled, it should be called on-demand loading, that is, loading only when calling this function, instead of loading it when the program starts. The difference between on-demand loading and static loading is that the loading time is different.

{

In. NET, P/Invoke. All functions in a DLL are dynamically loaded. This is wrong. Thanks to Wuya, it should be called On-Demand Loading. For the dynamic loading method, see Wuya to reply) is defined using the DllImport attribute. If the SmiAccelerometerUnregisterHandler () function is used in. NET, it is defined as follows:

[DllImport ("SamsungMobileSDK_1.dll", CharSet = CharSet. Auto, SetLastError = true)] private static extern uint evaluate (); Using. NET is easier than using Native C ++ for dynamic loading.

}

Mobile Sensors API Project

This project is still in its infancy. Currently, samsung's gravity sensor is implemented. I host the project to Mobile Sensors API-Native uniied APIs for Windows Mobile Sensors, and I will continue to improve it, implement various sensors in this project.

Source code: http://mobilesensor.codeplex.com/SourceControl/ListDownloadableCommits.aspx

Environment: VS2008 + WM 6 professional SDK + Samsung Windows Mobile SDK

Original article title: Native C ++ dynamic DLL loading in Windows Mobile

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.