# Friday, October 09, 2009

Today I got a question from a customer, that essentially was "how can I detect when the network cable has been plugged in or unplugged from my CF application."  I knew I has solved this before, and after a little bit of digging with search engines I was reminded how obscrure finding the answer to this, even from native code, is.

So the general answer for how this is detected is that you have to call down into the NDIS driver via an IOCTL and tell it that you're interested in notifications.  This is done with the IOCTL_NDISUIO_REQUEST_NOTIFICATION value.  Of course receiving the notifications isn't so straightforward - you son't just get some nice callback.  Instead you have to spin up a point to point message queue and send that in to the IOCTL call, along with a mask of which specific notifications you want.  Then, when something changes (like the cable is pulled) you'll get an NDISUIO_DEVICE_NOTIFICATION structure on the queue, which you can then parse to find the adapter that had the event and what the exact event is.

From a managed code perspective, this is actually a lot of code to have to write - CreateFile to open NDIS, all of the queueing APIs, the structures for the notifications, etc.  Fortunately, I'd already been down this road and had added it to the Smart Device Framework already.  So if you're using the SDF, getting the notifications looks like this:

public partial class TestForm : Form
{
  public TestForm()
  {
    InitializeComponent();

    this.Disposed += new EventHandler(TestForm_Disposed);

    AdapterStatusMonitor.NDISMonitor.AdapterNotification += new AdapterNotificationEventHandler(NDISMonitor_AdapterNotification);
    AdapterStatusMonitor.NDISMonitor.StartStatusMonitoring();
  }

  void TestForm_Disposed(object sender, EventArgs e)
  {
    AdapterStatusMonitor.NDISMonitor.StopStatusMonitoring();
  }

  void NDISMonitor_AdapterNotification(object sender, AdapterNotificationArgs e)
  {
    string @event = string.Empty;

    switch (e.NotificationType)
    {
      case NdisNotificationType.NdisMediaConnect:
        @event = "Media Connected";
      break;
      case NdisNotificationType.NdisMediaDisconnect:
        @event = "Media Disconnected";
      break;
      case NdisNotificationType.NdisResetStart:
        @event = "Resetting";
      break;
      case NdisNotificationType.NdisResetEnd:
        @event = "Done resetting";
      break;
      case NdisNotificationType.NdisUnbind:
        @event = "Unbind";
      break;
      case NdisNotificationType.NdisBind:
        @event = "Bind";
      break;
      default:
        return;
    }

    if (this.InvokeRequired)
    {
      this.Invoke(new EventHandler(delegate
      {
        eventList.Items.Add(string.Format("Adapter '{0}' {1}", e.AdapterName, @event));
      }));
    }
    else
    {
      eventList.Items.Add(string.Format("Adapter '{0}' {1}", e.AdapterName, @event));
    }
  }
}

 

Friday, October 09, 2009 10:08:32 AM (Central Daylight Time, UTC-05:00)  #     | 
# Wednesday, October 07, 2009

It's been a while since I did any check-ins to the OpenNETCF.IoC framework.  It's not due to lack of interest or lack of thought on my part.  I've just been very, very busy lately.  We've now using the IoC project in a few commercial applications with very good results, so there isn't a whole lot more to add (though I do want to add some sort of lifecycle management capabilities).

In the hope of getting a little more adoption of the framework, I've added an adapter to OpenNETCF.IoC for the Microsoft Patterns and Practices CommonServiceLocator (CSL).  You can use CSL code to extract object instances from the Items collection.  My hope is that this makes it easier for development teams using CSL to integrate OpenNETCF.IoC into their mobile and embedded projects.

If you have any other ideas or feature requests for OpenNETCF.IoC, by all means submit a request or start a discussion on the project site.  And if you're using it, I'm always interested to see how it's working out.

Wednesday, October 07, 2009 1:13:45 PM (Central Daylight Time, UTC-05:00)  #     | 
# Tuesday, October 06, 2009

When you hire a consultant you're not simply getting someone to sling code (if that's what you want, hire a contractor).  Sure we do that, but we bring along a lot of experience, and quite often a plethora of code libraries.  We tend to see a wide variety of applications and tend to build up wide repositories of knowledge.  For example we're just putting finishing touches on a C# wrapper for using the Anviz SM2000 fingerprint reader.  It's a low-cost OEM module that seems to perform pretty well.  So if you're building a custom Windows CE (now Windows Embedded Compact to be true to Microsoft's marketing engine) device where you need fingerprint authentication, we could have you enrolling and authenticating fingerprints from a managed application very, very quickly instead of you spending a week or more trying to build up code from a not-so-clear spec document.

Tuesday, October 06, 2009 9:34:25 AM (Central Daylight Time, UTC-05:00)  #     | 
# Friday, October 02, 2009

Very often we here at OpenNETCF work on projects that are internal to companies that is seen by relatively few people or we do infrastructure work that helps an app succeed, but it isn't the app itself. Sometimes, though, we do get an opportunity to write public, commercial applications.  We've just finished version 1 of Trapster for Windows Mobile, which chould hit the Windows Marketplace this month.  For more info on what Trapster is and how it works visit their website, or read one of their reviews such as that on PocketNow or CNET.

The pieces of note here are that we used the OpenNETCF IoC Framework for dependency injection and the Bing Maps web services for all of the mapping and navigation work.

Friday, October 02, 2009 7:58:57 AM (Central Daylight Time, UTC-05:00)  #     | 
# Monday, August 31, 2009

Today I came back to a project that I hadn't worked on in a few months.  I got latest from source control and tried to build, and the linker complained "Error 1 fatal error LNK1104: cannot open file 'secchk.lib' AudioControl AudioControl".

Now WTF?  I knew this was working before.  The error remindinded me vaguely of a previous problem I had, so I searched my blog and found this one.  The problem that time was that I was building for PPC03 and the project for some reason had not added secchk.lib to my platform.  THis time, however, I am building for a generic Windows CE device, and for some reason it *is* adding it.  Well CE doesn't have this lib, and I don't want it.  A quick scan through the project settings didn't turn up anything obvious, so I went with the more heavy-handed approach of adding the following to a common header file:

#pragma comment(linker, "/nodefaultlib:secchk.lib")
#pragma comment(linker, "/nodefaultlib:ccrtrtti.lib")

Done and done.  (the second is required because when you add the first and rebuild, the linker will then complain about not finding ccrtrtti.lib). No idea why I get the error (I'd love to know the root cause) but I don't have time to dig around.  Fortunately the #pragma directive overrides whatever project settings you have, which can be handy at times.

Monday, August 31, 2009 9:11:28 PM (Central Daylight Time, UTC-05:00)  #     |