(WPD) Trouble with multiple calls to IPortableDeviceManager::GetDevices

Discussion in 'Visual C# .Net' started by DN Admin, May 1, 2012.

  1. DN Admin

    Staff Member

    Joined:
    Aug 7, 2010
    Location:
    Joliet, IL U.S.A.
    Hi all. I posted this in the CLR forum and didn't get any responses except a suggestion to post it here... Any help is appreciated.
    In any case, I am working on a C# application that uses WPD and am having trouble with the http://msdn.microsoft.com/en-us/library/windows/desktop/dd388693(v=vs.85).aspx GetDevices method mentioned in the title. The initial problem
    was that I was only able to get one device ID even if multiple devices were connected to the machine. I followed the discussion http://stackoverflow.com/questions/6162046/enumerating-windows-portable-devices-in-c-sharp here and http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx here and
    found that modifying the IL for the Interop wrapper from:
    <pre class="prettyprint" style="font-size:12px .method public hidebysig newslot abstract virtual instance void GetDevices([in][out] string& marshal(lpwstr) pPnPDeviceIDs, [in][out] uint32& pcPnPDeviceIDs) runtime managed internalcall { } // end of method IPortableDeviceManager::GetDevices </pre>
    to:
    <pre class="prettyprint" style="font-size:12px .method public hidebysig newslot abstract virtual instance void GetDevices([in][out] string[] marshal(lpwstr[]) pPnPDeviceIDs, [in][out] uint32& pcPnPDeviceIDs) runtime managed internalcall { } // end of method IPortableDeviceManager::GetDevices </pre>
    and found that this got me to a point where I was able to get device IDs for all of the devices connected to the machine (not just the first one). However, this only worked once. On subsequent calls, I could retrieve the count properly but only the first
    device ID string would be valid. The rest would be null. I even tried changing string[] to native int[] in the IL, and then marshalling the strings in manually and ended up with the same result.
    Anyone encountered this before? It seems like this must be an issue with the method itself. However, I'm still fairly new to C# Interop so it's possible I'm not properly marshaling the array of strings (actually the native type is LPWSTR*, a pointer to an
    array of string pointers). Any help is appreciated.

    View the full article
     
    #1

Share This Page