Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Windows Portable Devices (WPD) and Events

Posted on 2011-05-03
Medium Priority
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
  • 2
  • 2

Expert Comment

ID: 35706829
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
ID: 35706982
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.

Accepted Solution

Wardy_01 earned 2000 total points
ID: 35708088
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
ID: 35708751
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.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

572 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