In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze C++ dynamic loading DLL in Windows Mobile implementation, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
The method of loading DLL statically
Using Native C++ development, generally use static loading method to load DLL, the so-called static loading is in the program compilation time (Compile Time) directly calls the function defined in the DLL header file, link time (Link Time) link * .lib file points to the interface of DLL, in the program start to run (Run Time Start up) load DLL.
Here's how to use the method of statically loading DLL. You need to specify the loaded * .lib file in the program for Link.
# pragma comment (lib, "SamsungMobileSDK_1.lib")
Using statically loaded DLL, you can directly call functions defined in the header file, such as:
SmiAccelerometerCapabilities cap;if (SmiAccelerometerGetCapabilities (& cap)! = SMI_SUCCESS) {throw;} SmiAccelerometerHandler h = & GetVectorHandler;if (SmiAccelerometerRegisterHandler (1000, h)! = SMI_SUCCESS) {throw;}
SmiAccelerometerGetCapabilities () and SmiAccelerometerRegisterHandler () are defined in the header file smiAccelerometer.h and can be called directly, as defined as follows:
The use of static loading method to use the aspect is still very convenient, but in the dynamic loading time can not directly call the header file function, increasing the complexity, which will be discussed below.
/ * Start receiving accelerometer data periodically. * The period interval must be a multiple of the callbackPeriod specified * in SmiAccelerometerCapabilities. If it is less than the callbackPeriod, it will be * set to the callbackPeriod. If it is not a multiple of the callbackPeriod, it will be * truncated to fit the value. ((period / callbackPeriod) * callbackPeriod) * * Only one handler per process is allowed. Successive calls per process will replace the previous handler * function and period. * * @ param period [in] callback interval. * @ param handler [in] callback function for every period interval. * * @ return * SMI_SUCCESS on success *\ nSMI_ERROR_INVALID_PARAMETER if the handler input parameter is NULL *\ nSMI_ERROR_DEVICE_NOT_FOUND if the device is not present or supported *\ n SMI_ERROR_CANNOT_ACTIVATE_SERVER if the sensor server cannot be started * / SMI_API SMI_RESULT SmiAccelerometerRegisterHandler (UINT period SmiAccelerometerHandler handler)
The method of dynamic loading DLL on C++
Statically loading DLL is a relatively simple development method, but there is a disadvantage that the program needs to load DLL when it starts to run, and if the DLL does not exist, the program cannot be started. Because the Windows Mobile Sensors API library needs to be adaptive to a specific device, that is, the Windows Mobile Sensors API library cannot rely on the device-specific Sensor library, you cannot use the statically loaded method to reference DLL. Here's how to load DLL dynamically.
Define a pointer to a function
To dynamically load DLL, you need to define a pointer to the function according to the header file, as follows:
Typedef UINT (WINAPI * PFN_SmiAccelerometerGetVector) (SmiAccelerometerVector*); typedef UINT (WINAPI * PFN_SmiAccelerometerGetCapabilities) (SmiAccelerometerCapabilities*); typedef UINT (WINAPI * PFN_SmiAccelerometerRegisterHandler) (UINT, SmiAccelerometerHandler); typedef UINT (WINAPI * PFN_SmiAccelerometerUnregisterHandler) (); PFN_SmiAccelerometerGetVector pfnSmiAccelerometerGetVector;PFN_SmiAccelerometerGetCapabilities pfnSmiAccelerometerGetCapabilities;PFN_SmiAccelerometerRegisterHandler pfnSmiAccelerometerRegisterHandler;PFN_SmiAccelerometerUnregisterHandler pfnSmiAccelerometerUnregisterHandler
These pointers to functions can be defined corresponding to the following functions in the smiAccelerometer.hheader file:
SMI_API SMI_RESULT SmiAccelerometerGetVector (SmiAccelerometerVector * accel); SMI_API SMI_RESULT SmiAccelerometerGetCapabilities (SmiAccelerometerCapabilities * capabilities); SMI_API SMI_RESULT SmiAccelerometerRegisterHandler (UINT period, SmiAccelerometerHandler handler); SMI_API SMI_RESULT SmiAccelerometerUnregisterHandler ()
The definition is one-to-one. The parameter entry and return values must be exactly the same.
Initialize a pointer to a function
The process of initializing a pointer to a function is the process of dynamically loading DLL. The code is as follows:
# define SAMSUNG_SENSOR_DLL L "SamsungMobilesdk_1.dll" HMODULE hSensorLib = LoadLibrary (SAMSUNG_SENSOR_DLL); if (NULL = = hSensorLib) {printf ("Unable to load Samsung Sensor DLL\ n"); throw std::runtime_error ("Unable to load Samsung Sensor DLL");} pfnSmiAccelerometerGetVector = (PFN_SmiAccelerometerGetVector) GetProcAddress (hSensorLib, L "SmiAccelerometerGetVector"); pfnSmiAccelerometerGetCapabilities = (PFN_SmiAccelerometerGetCapabilities) GetProcAddress (hSensorLib, L "SmiAccelerometerGetCapabilities") PfnSmiAccelerometerRegisterHandler = (PFN_SmiAccelerometerRegisterHandler) GetProcAddress (hSensorLib, L "SmiAccelerometerRegisterHandler"); pfnSmiAccelerometerUnregisterHandler = (PFN_SmiAccelerometerUnregisterHandler) GetProcAddress (hSensorLib, L "SmiAccelerometerUnregisterHandler"); if (NULL = = pfnSmiAccelerometerGetVector) {printf ("Unable to find entry point of SmiAccelerometerGetVector\ n"); throw std::runtime_error ("Unable to find entry point of SmiAccelerometerGetVector");} if (NULL = = pfnSmiAccelerometerGetCapabilities) {printf ("Unable to find entry point of SmiAccelerometerGetCapabilities\ n") Throw std::runtime_error ("Unable to find entry point of SmiAccelerometerGetCapabilities");} if (NULL = = pfnSmiAccelerometerRegisterHandler) {printf ("Unable to find entry point of SmiAccelerometerRegisterHandler\ n"); throw std::runtime_error ("Unable to find entry point of SmiAccelerometerRegisterHandler");} if (NULL = = pfnSmiAccelerometerUnregisterHandler) {printf ("Unable to find entry point of SmiAccelerometerUnregisterHandler\ n"); throw std::runtime_error ("Unable to find entry point of SmiAccelerometerUnregisterHandler");}
The LoadLibrary () function dynamically loads DLL,GetProcAddress () loads the function's entry address to a pointer to the function based on the function's name. It's a little roundabout, sorry. If the address is not empty, you can call the corresponding function based on this address.
Call function
The method of calling a function is the same as statically loading DLL, but instead of calling the name of the function directly, it is called with a pointer to the function. The following example can be compared with the example of statically loading a DLL function call.
SmiAccelerometerCapabilities cap; if (pfnSmiAccelerometerGetCapabilities (& cap)! = SMI_SUCCESS) {throw;} SmiAccelerometerHandler h = & GetVectorHandler; if (pfnSmiAccelerometerRegisterHandler (1000, h)! = SMI_SUCCESS) {throw;}
The method of dynamically loading DLL on C++ is completed.
The world of. Net
The following paragraph is not correct. Please see the reply below. .net using the DllImport property for P/Invoke should not be called dynamic loading, because it cannot be unloaded, it should be called demand loading, which is loaded when the call function is used, not when the program starts. The difference between demand loading and static loading is that the loading time is different.
{
In .NET, all the functions in P/Invoke a DLL are dynamically loaded (this is wrong, thank you Wuya pointed out, here should be called demand loading, dynamic loading method can be seen in Wuya to reply), it is defined by the DllImport attribute. If the SmiAccelerometerUnregisterHandler () function is used in .NET, it will be defined as follows:
[DllImport ("SamsungMobileSDK_1.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern uint SmiAccelerometerUnregisterHandler (); using .NET is relatively easier than dynamic loading using Native C++.
}
About the Mobile Sensors API project
This project is still in its infancy and currently implements samsung's gravity sensor. I have host the project to Mobile Sensors API-Native unified APIs for Windows Mobile Sensors, and I will continue to improve and implement all kinds of sensors into this project.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.