Link to home
Start Free TrialLog in
Avatar of Bob Learned
Bob LearnedFlag for United States of America

asked on

Windows Portable Devices (WPD) and Events

I came across this reference during my research into the WPD:

Listening to WPD Events
http://blogs.msdn.com/b/dimeby8/archive/2006/10/06/listening-to-wpd-events.aspx

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;

            this.Register();
        }

        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");

            _device.Unadvise(_cookie);

            Marshal.ReleaseComObject(this);
        }

    }
}

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.
Avatar of Wardy_01
Wardy_01

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.
Avatar of Bob Learned

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Wardy_01
Wardy_01

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.