VS2008下使用托管代码控制Windows Mobile Device Emulators
在听黎波老师的Webcast-Visual Studio 2008 移动开发新体验时,听他提到过可以使用代码来控制Device Emulator。今天在msdn上看到了Jim Wilson写的” How To: Programmatically Control the Windows Mobile Device Emulators from .NET”一文,觉得很有必要和大家分享一下。
大家知道,device emulator在调试windows mobile应用程序的时候非常有用。Device Emulator Manager可以执行包括启动device emulator、保存device emulator状态、cradle(和Activesync进行同步)、关闭device emulator等功能。现在我们面临的challenge就是:必须通过Device Emulator Manager的UI,手动控制device emulator。这样就给测试自动化带来了困难。
Device Emulator Manager 和 Device Emulator 3.0是VS2008默认安装的部分,它们引入了Device Emulator Manager API,从而在一定程度上解决了上面的问题。Device Emulator Manager API提供了访问Device Emulator的编程接口,我们可以在desktop应用中调用Device Emulator Manager API,来执行和Device Emulator Manager同样的功能。
l The Device Emulator Manager API
Device Emulator Manager API是通过组件对象模型(COM) In-Process库实现的,即在.NET托管代码中,使用Microsoft.DeviceEmulatorManager.Interop.9.0.dll提供接口。该dll文件位于”%Program Files%\Microsoft Device Emulator\1.0”下,使用API必须添加"Microsoft.DeviceEmulatorManager.Interop"命名空间。同时,为了使用P/F,我们最好加入"System.Runtime.InteropServices"。
l Controlling the Device Emulator
最常用的Device Emulator API就是IDeviceEmulatorManagerVMID接口。下表列出了该Interface的方法和描述。
Method |
Description |
BringToFront |
Moves the Device Emulator to the foreground of the desktop display and makes the Device Emulator the active application. |
ClearSaveState |
Clears the Device Emulator's saved state file (.dess). The next time you start the Device Emulator, the Device Emulator will have no existing state and will start from the ROM image. The Device Emulator will therefore appear as a new device. Any software you have installed or setting changes you have made are lost. |
Connect |
Starts the Device Emulator. |
Cradle |
Puts the Device Emulator in the virtual cradle and causes the Device Emulator to connect to the desktop using Microsoft ActiveSync® (Windows XP) or Windows Mobile Device Center (Windows Vista®). |
get_Name |
Returns the display name of the emulator such as Windows Mobile 6 Professional Emulator or Windows Mobile 5.0 Smartphone. The display name is the same value that appears in the Device Emulator Manager user interface's list of Device Emulators. |
get_State |
Returns the EMULATOR_STATE enumeration value corresponding to the Device Emulator's current state. Possible values are EMU_NOT_RUNNING, EMU_RUNNING, and EMU_CRADLED. A Device Emulator is in only one state at a time. |
get_VMID |
Returns the Device Emulator's Virtual Machine Identifier (VMID), which is a globally unique identifier (GUID) unique to each emulator image. |
GetConfiguration |
Returns an XML string describing the Device Emulator's current configuration. |
Reset |
Performs a soft-reset or hard-reset of the Device Emulator as specified by the passed integer parameter. A non-zero parameter value specifies a soft-reset. |
SetConfiguration |
Sets the Device Emulator to the configuration described in the provided XML string parameter. |
Shutdown |
Shuts down the Device Emulator, optionally saving the Device Emulator's state as specified by the passed integer parameter. A non-zero parameter value specifies that the state should be saved. |
UnCradle |
Removes the Device Emulator from the virtual cradle and causes the Device Emulator to disconnect from ActiveSync (Windows XP) or Windows Mobile Device Center (Windows Vista). |
l A Device Emulator Manager Wrapper
包装(Wrapper)这些API给.NET托管代码来用,可以为许多工程带来方便。示例工程提供的封装包括以下几个类,同时给出了描述:
Type |
Description |
DeviceEmulatorManagerEx |
Represents the Device Emulator Manager. Encapsulates the Device Emulator Manager features of the IDeviceEmulatorManager interface. |
DeviceEmulatorCategory |
Represents an individual category. Encapsulates those features of the IDeviceEmulatorManager interface that represent a single category. |
DeviceEmulatorSdk |
Represents an individual SDK. Encapsulates those features of the IEnumManagerSDKs interface that represent a single SDK. |
DeviceEmulator |
Represents an individual Device Emulator. Provides the same features as the IDeviceEmulatorManagerVMID interface. |
恩,就啰嗦到这里吧。具体的代码大家可以直接去MSDN上下载,或者通过下面这个链接来获得:https://files.cnblogs.com/dearsj001/DEMAutomationWrapperExample.rar。
Good Luck!