DEVBUZZ Homepage Communicating w/ the Desktop
 
Web www.devbuzz.com
  HOME PAGE
  All Articles
  Advertise
  Consulting

 Development
  Discuss - Forums
  Still in the box?
  .Compact Framework
  Code Snippets
  SQL Server CE
  Database
  MS Resources
 Stores
  Developer Controls
  Pocket PC Hardware
  Pocket PC Software
  Pocket PC Books
  .NET CF Books
  Book Reviews
  SPB SW Discounts
  RESCO SW Discounts
 DEVBUZZ Info
  About Us
  Help
  Join our email list
  Links & Ratings
  Press & Comments
  Pocket PC version
  Software Reviews
  Hardware Reviews
 Authors
  Authors
  Article Guide
  Competitions
 Resources
  Developers
  Register
  Login

  SPB Discounts!
 Columnists
  Rick Winscot
 Past Blast
  Personal Media Ctr
  Gizmobility
  eVB Legacy
  Old news
  Hosted Software
  Wireless
  Newsletters
  Carl Davis
  Upton Au

 Pocket PC Registry
  Join the registry
  View current list
 Current Poll
Are you converting to .NET Compact Framework?
Yes, it has changed my life!
No, I'm sticking with eVB
.NET CF what's that`?

Current results
3431 votes so far
 Recent Forum Threads [goto forums]

Get Computername
read... (67 hits)


Great aid to development productivity
read... (82 hits)


ThreadingTimer sample code
read... (143 hits)


Multithreading with .NET CF
read... (194 hits)


Moving from eMbedded Visual Basic to Visual Basic .NET
read... (166 hits)


.NET Compact Framework 2.0 Service Pack 2
read... (226 hits)


Transfer Data from SQL Server 2000 to SQL Server Compact Edition
read... (298 hits)


This protocol version is not supported
read... (236 hits)


Converting Lowercase to uppercase wont work
read... (203 hits)


Direct access to MS SQL Server 2000
read... (374 hits)


Creating SDF file in Desktop
read... (513 hits)


Winsock in CF.NET
read... (316 hits)


Using Pocket Outlook to submit HTML page form with MAILTO action
read... (420 hits)


Missing file "System.Data.PocketPC.asmmeta.dll"
read... (268 hits)


HP iPAQ hw6915 Serial Port Issue
read... (309 hits)


Info on the recent forum changes
read... (341 hits)


SqlServer tools from Redgate
read... (383 hits)


Arrow keys and Hardware navigation button
read... (393 hits)


O2 XDA lls pin sync cable to comport
read... (322 hits)


Creating dynamic folders on Pocket PC OS
read... (299 hits)

Custom Windows Mobile software development.
LBS Challenge 2007
LBS Challenge Eight previous NAVTEQ Global LBS Challenge® participants have received venture capital funding and nine past LBS Challenge winners have launched commercial applications on major wireless carriers. Register your non-commercial LBS application in the 2007 NAVTEQ Global LBS Challenge in one of three regions: Americas, Europe-Middle East-Africa (EMEA) or Asia-Pacific(APAC). You could win a share of $2 million in prizes. This could be your year.
Dream. Develop. Win.

Development | .NET Compact Framework

Communicating w/ the Desktop
Written by W.G. Ryan  [author's bio]  [read 28225 times]
Edited by Derek

.NET Compact Framework   

Page 1 

Communicating with a connected device is quite distinct from communicating with other desktop macines or a server. Considering that PDA's use a different OS and a different processor, this revelation is hardly surprising. In this regard, there's some good news and some bad news. The bad news is that it takes a little more work than you may acustomed to in order to communicate with a device. The good news is that 1) it's possible 2) Once you understand what's going on, it's fairly straightforward.

The primary mechanism for desktop to device (d to d) communication is knows as RAPI. RAPI is code for using any of the functionality exposed in RAPI.dll which resides on the desktop machine. Currently, there's no native managed provider to access RAPI, so you are going to have to delve into the unmanaged world. Opennetcf.org has an implementation which wraps the functionality of RAPI in an easy-to-use library and if you aren't familiar with their work, I highly encourage your to visit their site. But how does one use RAPI and what can you do with it?

Let's say that you wanted to programatically activate myProgram which resides on the PDA from the desktop. A natural assumption might be that you somehow make a connection with the PDA and then use a mechanism like Shell.Execute(@"MyPath\MyApp") or Process.Start or some similar mechanism. When I first started CF programming, this was my assumption. But I quickly learned that there is no Shell.Execute functionality provided in the CF. As such, it idn't take long for me to realize that any career in Compact Framework will be riddled with P/Invoke. So before I go any further into the mechanics of RAPI, let's take a look at how we'd start an application on a PDA.

The first thing you'll need to do is include a reference to System.Runtime.InteropServices. This is the namespace that provides the fundamental interface for dealing with unmanaged code. And without striking up the all too belabored debate on VB.NET vs C#, I actively use both C# and VB.NET but greatly prefer C# when dealing with unmanaged code. C# Supports execution of unsafe code blocks and VB.NET doesn't (which isn't due to the language as much as the implementation) hence making it much better suited for Interop tasks (at least from my point of view). Next, we need to take a look at the CreateProcess API. A typical invokation looks something like the following:

[DllImport("coredll.dll")]
public static Int32( string lpApplicationName, string lpCmdLine, IntPtr lpProcessAttribute,
IntPtr lpThreadAttributes, Int32 boolInheritHandles, Int32 dwCreationFlags, IntPtr
lpEnvironment, IntPtr lpCurrentDir, Byte[] si, ProcessInfo pi);

However, we are going to use it a little differently. Here's the actual invokation:

DllImport("rapi.dll", CharSet=CharSet.Unicode)]
public static extern Int32 CeCreateProcess(string lpApplicationName, string lpCmdLine,
Int32 lpProcessAttribute, Int32 lpThreadAttribute, Int32 boolInheritsHandles, Int32
lwCreationFlags, Int32 lpEnvironment, Int32 lpCurrentDir, Int32 misc, ref ProcessInfo
lpProcessInfo);

This is essentially the point where the rubber hits the road, but to make this work, we have some more P/Invoking to do:


[DllImport("rapi.dll", CharSet=CharSet.Unicode)]
public static extern Int32 CeRapiInit();

[DllImport("rapi.dll", CharSet=CharSet.Unicode)]
public static extern Int32 CeRapiUninit();

[DllImport("rapi.dll", CharSet=CharSet.Unicode)]
public static extern int CeCloseHandle(IntPtr Handle);

public enum SafeToContinue: uint{
Yes = 0
,No = 1
}

Now, we'll need to define a structure to accomdate the last argument of the API Call:

[StructLayout(LayoutKind.Sequential, Pack=4)]
public struct ProcessInfo{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

At this point, we've done all of our ground work and we are ready to test things out. Here's a shell of a C# desktop program which assumes the above code has already been incorporated:

const string ProgramName = @"\Program Files\TargetProgram.exe";

[STAThread]
static void Main(string[] args){
ProcessInfo Information = new ProcessInfo();

try{
if(CeRapiInit() == SafeToContinue.Yes){

CeCreateProcess(ProgramName, string.Empty, 0,0,0,0,0,0,0, ref Information);

}
}
catch(System.Exception ex){
Debug.Assert(false, ex.ToString());
}
finally{
CeCloseHandle(Information.hProcess);
CeCloseHandle(Information.hThread);
CeRapiUninit();

}
}

At this point, it should be fairly evident that Rapi can do some pretty cool things for you but this type of implementation isn't intended as a solution for many problems. After all, how many times do you really need to programatically invoke a program on a connected PDA? In most instances it might be a lot easier to grab the damned thing and just start the program. But that's not really the point. Mainly, what I hope I've conveyed is that Desktop to Device communication is not only possible but not all that complex. Sure, you'll be P/Invoking up a storm, but that's the nature of Compact Framework development these days.
RELATED Resources:  Opennetcf

Back to .NET Compact Framework | [Article Index]

 

Back to the top of the page.
Chris De Herrera's Windows CE Website Windows CE News & Information Source
Copyright ©2000-2007 by DEVBUZZ.COM, Inc., NJ. USA.MSDEVELOP