没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|其它|编辑:郝浩|2009-09-25 13:48:57.000|阅读 769 次
概述:Windows Mobile Sensors API库的一个重要的功能是自动发现(detect)和判断(determine)设备类型,调用相应设备(HTC或者Samsung)上的Sensor API,并返回统一的结果。因此该库不能依赖于任何设备的上的Sensor库。因此产生了动态加载各自设备上Sensor库的需求。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
Windows Mobile Sensors API库的一个重要的功能是自动发现(detect)和判断(determine)设备类型,调用相应设备(HTC或者Samsung)上的Sensor API,并返回统一的结果。因此该库不能依赖于任何设备的上的Sensor库。因此产生了动态加载各自设备上Sensor库的需求。关于Windows Mobile Sensors API库的开发可以参考下面的文章:
Mobile Sensors API - Native unified APIs for Windows Mobile Sensors
Windows Mobile下的重力感应器(Gravitational Sensor)开发
Windows Mobile下使用CppUnitLite输出测试结果
Windows Mobile Sensors API库的设计
本文以Windows Mobile Sensors API库为例子讲述在Windows Mobile下使用Native C++动态加载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可以直接调用头文件定义的函数,例如:
SmiAccelerometerCapabilities cap;
if( SmiAccelerometerGetCapabilities(&cap) != SMI_SUCCESS)
{
throw;
}
SmiAccelerometerHandler h = &GetVectorHandler;
if(SmiAccelerometerRegisterHandler(1000, h) != SMI_SUCCESS)
{
throw;
}
SmiAccelerometerGetCapabilities()和SmiAccelerometerRegisterHandler()是定义在头文件smiAccelerometer.h中的,可以直接调用,定义如下:
/**
* 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
* \n SMI_ERROR_INVALID_PARAMETER if the handler input parameter is NULL
* \n SMI_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);
使用静态加载的方法使用方面还是很方便的,可是在动态加载的时候就不能直接调用头文件的函数了,增加了复杂度,下面会讲到。
静态加载DLL是比较简单的开发方法,可是有个缺点是程序开始运行的时候就需要加载DLL,如果该DLL不存在,程序就不能启动了。由于Windows Mobile Sensors API库需要自适应具体的设备,也就是说Windows Mobile Sensors API库不能依赖于具体设备的Sensor库,所以不能使用静态加载的方法来引用DLL。下面讲述动态加载DLL的方法。
动态加载DLL,需要根据头文件来定义指向函数的指针,如下:
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;
这些指向函数的指针可以对应下面的在smiAccelerometer.h头文件的函数进行定义:
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();
定义是一一对应的。参数入口和返回值都必须完全一致。
初始化指向函数的指针的过程也就是动态加载DLL的过程,代码如下:
#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");
}
LoadLibrary()函数动态加载DLL,GetProcAddress()根据函数的名字 加载函数的入口地址 到指向函数的指针。有点绕口,sorry。如果地址不为空,那么可以根据这个地址调用相应的函数。
调用函数的方法和静态加载DLL的方法一样,但是不是直接调用函数的名字,而是使用指向函数的指针来调用,下面的例子可以和静态加载DLL函数调用的例子对比来看。
SmiAccelerometerCapabilities cap;
if( pfnSmiAccelerometerGetCapabilities(&cap) != SMI_SUCCESS)
{
throw;
}
SmiAccelerometerHandler h = &GetVectorHandler;
if(pfnSmiAccelerometerRegisterHandler(1000, h) != SMI_SUCCESS)
{
throw;
}
动态加载DLL的方法就完成了。
下面这段表述不对,请看下面的回复。.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++动态加载相对简单。
}
这个项目还是在起步阶段,当前实现了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
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:博客园面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号