WinCE平台的程序编译到Win32平台下运行

最近做的项目中,有一个在WinCE平台上跑的程序,后来随着项目的发展,要求此程序在PC上也能跑。
感谢VS 2005提供的多平台支持,只需要几分钟就可以解决这个问题,方法很简单,下面是我处理的过程。

1.在配置管理中增加新平台

1.1 创建新平台

程序最初是基于Pocket PC 2003(ARMV4)平台创建的,我在此平台的配置基础上,创建新的Win32平台。
打开菜单Build->Configuration Manager,在Active Solution Platform下拉选择框中选择New,选择new platform为Win32,Copy settings from选择原有配置Pocket PC 2003(ARMV4),按OK为应用程序创建新的Win32运行平台。

1.2 修改Debug|Win32的配置

Win32平台的配置由PPC复制而来,有些参数需要进行更改。可以在项目属性中进行修改,但直接修改工程文件可能更为直观和简单,我是直接修改此文件。

1.2.1 修改工具VCMIDLTool中的预定义宏PreprocessorDefinitions,只保留"_DEBUG"
1.2.2 修改工具VCMIDLTool中的预定义宏PreprocessorDefinitions,改为"_DEBUG;WIN32;_WINDOWS"
1.2.3 修改工具VCResourceCompilerTool中的预定义宏PreprocessorDefinitions,只保留"_DEBUG"
1.2.4 修改工具VCLinkerTool的配置,改为

  1. <Tool
  2. Name="VCLinkerTool"
  3. LinkIncremental="2"
  4. GenerateDebugInformation="true"
  5. SubSystem="2"
  6. TargetMachine="1"
  7. />
1.3 修改Release|Win32的配置

1.3.1 修改工具VCMIDLTool中的预定义宏PreprocessorDefinitions,只保留"NDEBUG"
1.3.2 修改工具VCMIDLTool中的预定义宏PreprocessorDefinitions,改为"NDEBUG;WIN32;_WINDOWS"
1.3.3 修改工具VCResourceCompilerTool中的预定义宏PreprocessorDefinitions,只保留"NDEBUG"
1.3.4 修改工具VCLinkerTool的配置,改为

  1. <Tool
  2. Name="VCLinkerTool"
  3. LinkIncremental="1"
  4. GenerateDebugInformation="true"
  5. SubSystem="2"
  6. OptimizeReferences="2"
  7. EnableCOMDATFolding="2"
  8. TargetMachine="1"
  9. />

2. 修正平台差异带来的编译错误

2.1 stdafx.h

首先是WINVER的定义,WinCE下被定义为_WIN32_WCE,改之:

  1. #if defined(WINCE)
  2. #define WINVER _WIN32_WCE
  3. #else
  4. #define WINVER 0x0501 // 允许使用特定于 Windows XP 或更高版本的功能。
  5. #endif

然后是一些WinCE特有的头文件和库,改为仅当WINCE宏被定义时编译(可能分散在不同位置):

  1. #if defined(WINCE)
  2. #include <ceconfig.h>
  3. #include <altcecrt.h>
  4. #include <aygshell.h>
  5. #pragma comment(lib, "aygshell.lib")
  6. #endif

2.2 App类

在xxxApp::InitInstance中,SHInitExtraControls()的调用加上宏检测,仅当WINCE宏被定义时编译。

  1. #if defined(WINCE)
  2. // SHInitExtraControls should be called once during your application's initialization to initialize any
  3. // of the Windows Mobile specific controls such as CAPEDIT and SIPPREF.
  4. SHInitExtraControls();
  5. #endif

另外App类的头文件中包含了资源文件的定义,但被#ifdef POCKETPC2003_UI_MODEL宏包了起来,我的程序在两个平台都使用同样的资源,因此把这个宏注释掉即可。

参考链接:http://blog.sina.com.cn/s/blog_7ffab91701016o5o.html

http://wenku.baidu.com/link?url=hHgo7JTPVeDK3prTYofXxJeanbIjK8G6K57CoQaNvRDU2EVyErzu7OM3FoCzQo63Bg3ds3B28ij420k1PJIatH7OJOVJNiN6mosK6P3WXdK