Windows Portable Devices (WPD) and Events

Posted on 2011-05-03
Last Modified: 2013-12-16
I came across this reference during my research into the WPD:

Listening to WPD Events

I am trying to convert this to C#, and I came up with this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Wpd
    internal class PortableDeviceEventCallback : IPortableDeviceEventCallback

        public PortableDeviceEventCallback(PortableDevice device)
            this._device = device;


        private PortableDevice _device;
        private string _cookie;

        private Guid _eventId;
        public Guid EventId
            get { return _eventId; }

        private string _eventDescription;
        public string EventDescription
            get { return _eventDescription; }

        private string _objectId;
        public string ObjectId
            get { return _objectId; }

        // IPortableDeviceEventCallback Members
        public void OnEvent(IPortableDeviceValues eventParameters)
            eventParameters.GetGuidValue(PropertyKey.WPD_EVENT_PARAMETER_EVENT_ID, out _eventId);

            // Convert the CLSID to a string.
            _eventDescription = NativeMethods.StringFromCLSID(_eventId);

            eventParameters.GetStringValue(PropertyKey.WPD_OBJECT_ID, out _objectId);
        /// <summary>
        /// IPortableDevice.Advise is used to register for event notifications
        /// This returns a cookie string that is needed while unregistering.
        /// </summary>
        public void Register()
            PortableDeviceValues eventParameters = new PortableDeviceValues();

            _device.Advise(0, this, eventParameters, out _cookie);

        public void Unregister()
            if (string.IsNullOrEmpty(_cookie))
                throw new NullReferenceException("Invalid event cookie");




Open in new window

Since C++ is not my primary language, I am trying to get someone else's opinion on this implementation, to see if I missed something important.
Question by:Bob Learned
    LVL 4

    Expert Comment

    Doesn't seem to overkill.

    I think you cracked it ... Since you're using C# are you programming for a windows environment ... seemingly .Net ... that begs the question ... is there not a similar API in the silverlight framework that microsoft is pushing for windows mobile and presumably all portable programming now ???

    It looks like this should do what you intend though.
    LVL 96

    Author Comment

    by:Bob Learned
    If there is something for the managed world (I haven't found it yet), I would be most interested.  Until such time, I move forward on converting C++ code to C#, and understanding the Windows Portable Device API.  I had a thought the other day, that a managed VC++.NET wrapper might be a good choice, but unfortunately, I am not up on the syntax differences between plain ol' C++ and C++.NET.
    LVL 4

    Accepted Solution

    The benefits are huge ... the most obvious being time ... in a managed environment most of the grunt work is taken away from you leaving you focus on the business logic / other problem you might be having.

    These days it's arguably possible to say that C++ code doesn't really gain you huge amounts of performance over that of managed code which used to be the main argument for sticking to unmanaged code for many.

    My advice would be ... write the same block of code in both and benchmark it ... basic maths there's no difference since the compiler optimses in such a way that they often generate the same machine code anyway ... i've seen scenarios where the compiler can spot patterns and actually make code faster than a C++ programmer would typically code it ...

    The thing is ... in managed world you're facing a different set of problems to the unmanaged world.

    Unmanaged your problems tend to be that of performance, memory management , and often just plain "where can i find information on this API". With the managed world, everything o that nature is already handled so you simply start thinking "how can i do ... ".

    I'm sure i'm about to start a flame war if i haven't already but I would say that the main drawback of the managed world is that you can only go where the virtual machine you live in has been deployed ... meaning i can only write code that works in microsofts pre thought out platform list whereas in the unmanaged world i can go where i like ... but my argument to that is ... why would i want to ? 99% of the world runs windows ... why not just live there :)

    Having said that ... its nice to know I could if i wanted to ... isn't it ? lol
    LVL 96

    Author Comment

    by:Bob Learned
    The advantages of a managed C++.NET wrapper, in my case (because I am relegated to the Microsoft's "managed" world), would be that it could translate the "language" of the Windows Portable Device API into something that easier to use in C#.  The other side of the coin is translating the C++ code into C#, where there are a lot of differences, and places for issues.

    I am that place where I just need someone to look over my shoulder to make sure that I am translating correctly, and getting the proper intent.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C# Help from Top Experts

    Solve your toughest problems, fast.
    C# experts are online now and ready to help you.

    Suggested Solutions

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now