Windows Portable Devices (WPD) and Events

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.
LVL 96
Bob LearnedAsked:
Who is Participating?
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
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.
Bob LearnedAuthor Commented:
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.
Bob LearnedAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.