Updated: Added missing code snippets.

Hey, I’m excited! I hope you are too. I’ve cracked the code for building C# apps directly with the OS build. That is, as an OS Subproject of a Windows Embedded Compact OS project the Managed Code project can be built and run as with Native Code apps. No SDK needed! No second instance of Visual Studio. There are actually a few ways for doing this but shouldn't we be able to do it like native code subprojects.

Topics:

  • The Compact Framework
  • CE.NET
  • Corecon and SDK app development
  • Managed Code apps in the OS Build
  • Partial Solutions for so doing

A Little History

The .NET Compact Framework is essentially a subset of the Managed Code classes and APIs with respect to the full framework available for the desktop and above. Originally it also contained same features specific to embedded devices such as the SQL Server CE database classes which morphed into Compact SQL. This is now “owned” by the desktop and not available in the current version of (3.9) of the Compact Framework, as available with Windows Embedded Compact 2013. Another set of classes specific to embedded devices, which only made sense with them, provided the IrDA Infrared API. Earlier versions of CE made much of use of Infrared for device to device communication whereas desktop devices did not. The Infrared APIs were also dropped from the CF in version 3.9 and so the Compact Framework V3.9 is truly just a subset of the desktop Framework. (It seems though that the Compact Framework may never break through to version 4 to include such features as async.)

clip_image001

A comparison of the desktop and Compact Framework circa Compact Framework 2.0

With the inclusion of the Compact Framework in Windows CE 4, an ability to build Managed Code with the OS build engine was enabled. (The CF wasn’t actually included until version 4.1) But it was never seriously documented. You were encouraged to develop and build Managed Code apps in a separate instance of Visual Studio and connect to the target device using Corecon. .. Wasn’t Corecon flaky then!.. How it has improved. Windows CE 4 was actually named Windows CE.NET but it never really lived up to that title.

clip_image003

CoreCon 101

Whilst not the focus of this article, it would be remiss to not at least provide a summary, for comparison, of using Corecon for developing CF applications on an embedded device. Corecon (core connectivity) is the connectivity transport between the application development system and the target embedded device when the Windows CE/Compact OS of the device is under development suing Platform Builder. The key steps are:

  • Develop the OS.
    • Include the Corecon components in the OS
  • Build an SDK for the OS or get a generic one
    • Include the Managed Code options in the SDK build
    • There isn’t a generic SDK for Compact 2013
  • Install the SDK on that or another development workstation.
  • Create software components on that development system using the templates installed with the SDK
    • Managed Code templates for C# or VB
    • C++ for native code apps (an alternative to Subprojects)
  • Start Corecon on the target
  • Connect the development system to the target
    • This was always the flakey bit in earlier versions
  • Deploy, test and debug the application.

Our book, discusses this in detail for Compact 7.

SDK application development is discussed in detail (in a proposed ebook on embedded101.com) for Compact 2013:

The Situation

You can create Native Code apps and modules as Subprojects of the OS using the “Add New Subproject” wizard. Why can’t that wizard also generate Managed Code OS Subprojects?[1] Note though that if you right-click on an OS Subproject and choose properties you do get a Managed Code tab. Surely … ????

clip_image005

clip_image007

Managed Code Tab on OS Subproject property sheets

There are only a couple of web pages that discuss this feature circa CE 5 (and reprinted for CE 6). There is no detailed discussion of using this tab. The suggestion is that it’s only for building OS Managed Code components

So the issue is: How do you create and build Managed Code apps as part of the OS build?

 

 

Partial Solutions to the issue

There is a solution of sorts with Windows Embedded Compact 7 as discussed in chapter 45 of our book,Professional Windows Embedded Compact 7 (Phung, Jones and Joubert, Wrox Press) . Firstly, armed with a suitable SDK you could add a Smart Device Managed Code project to the OS project’s solution (not in the OS project). You can then add it directly as as OS Subproject (Add-Existing-Subproject). Note that the Smartdevice project is in the root of the OS project, not where the Subprojects are. It will then pop up a message saying:

image Dialog Text:

The selected Smart Device project cannot be converted to a Platform Builder subproject because it is already present in the solution. Do you want to have Platform Builder remove the Smart Device project from the solution, and then convert and add it as a Platform Builder subproject?



Wala!
The Smartdevice Sources File
  1. PBSUBPROJECT=1
  2. RELEASETYPE=LOCAL
  3. TARGETTYPE=MANAGED_EXE
  4. MANAGED_PROJ_FILE=SmartDeviceProject2.csproj
  5. TARGETNAME=SmartDeviceProject2.exe
  6. SOURCES= \
The generated source file for the Smartdevice project.

The following is automatically inserted into to the (now) subproject's .csproj file at the bottom

Insertion to .csproj
  1. <Import Condition="'$(_WINCEROOT)' != ''" Project="$(_WINCEROOT)\public\common\oak\misc\Microsoft.PlatformBuilder.Managed.Targets" />

Hey presto, your Managed Code app now builds directly with the OS! This last action worked with WEC7 because it implemented a unified build. In previous CE versions of the OS, the OS build engine was different to the Subproject build engine. These became the same, MSBuild, with WEC7.But alas with the C++ compiler update in WEC2013, this undocumented feature is now broken! Great while it lasted. Back to the drawing board.

Another approach is again to include the Compact Framework project in the OS solution. You create a content only OS Subproject that consumes the Managed Code build output, as generated by CEComponentWiz. With the CF project configured to be built before the OS, you build the solution. The subproject’s postlink.bat will be configured to copy the CF project’s output to the OS release directory and to include it via a BIB file entry.

Code Snippet from postlink.bat
  1. @echo copying Content Files from Resource Files folder to Targeted Debug Directory
  2. copy "..\..\DeviceApplication1\bin\Release\Smartdevice1.exe" "%SG_OUTPUT_ROOT%\oak\target\%_TGTCPU%\%WINCEDEBUG%"  /Y
  3.  
  4. @echo copying Content Files from Resource Files folder to FlatRelease Directory
  5. copy "..\..\DeviceApplication1\bin\Release\Smartdevice1.exe" %_FLATRELEASEDIR%  /Y

Postlink.bat is generated by CEComponentWiz

A tighter approach, is to action the build of the CF subproject by some calls in the subproject’s prelink.bat. You need to separately create the Managed Code project as an SDK project then refer to it in the batch file. Prelink.bat jumps to the project's directory, sets up the Managed Code build environment, then builds the project. Postlink.bat can then get it.

Typical prelink.bat
  1. cd "C:\WINCE800\OSDesigns\OSDesign2\DeviceApplication1"
  2. call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat"
  3. msbuild /t:Rebuild /p:Configuration=Release

Prelink.bat: Calls MSBuild environment then builds the project

But what if you wanted to just directly create a Managed Code app by Platform Builder? Let’s say a simple .Net Console or Forms app that makes uses of a .NET class for which you know the correct API syntax? That’s what WECSharpWiz on Codeplex is for.


[1] Another point is why isn’t that wizard extensible?


This series: 

Previous: