<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx</link><description>Let's take a look at how we can enumerate WPD devices in C#. This post assumes that you already have a project set up using these instructions . (Update: You will also need to follow the disassembly/reassembly instructions below for this to work correctly</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#10378259</link><pubDate>Fri, 14 Dec 2012 22:02:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10378259</guid><dc:creator>Matthias Muenzner</dc:creator><description>&lt;p&gt;I just wanted to use that disassemble -&amp;gt; change -&amp;gt; assemble trick and it worked fine but&lt;/p&gt;
&lt;p&gt;if you want to marshal long strings it will not work. &lt;/p&gt;
&lt;p&gt;The compiler throws a DirectiveException (Marshaler restriction: Excessively long string)&lt;/p&gt;
&lt;p&gt;So i changed the lines: &lt;/p&gt;
&lt;p&gt; &amp;nbsp;instance void &amp;nbsp;GetDevices([in][out] string[] &amp;nbsp;marshal([]) pPnPDeviceIDs,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[in][out] uint32&amp;amp; pcPnPDeviceIDs) runtime managed internalcall&lt;/p&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;p&gt;	 &amp;nbsp;instance void &amp;nbsp;GetDevices([in][out] string[] &amp;nbsp;marshal( lpwstr[]) pPnPDeviceIDs,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[in][out] uint32&amp;amp; pcPnPDeviceIDs) runtime managed internalcall	&lt;/p&gt;
&lt;p&gt;the changed c# code is: &lt;/p&gt;
&lt;p&gt;void GetDevices([In][Out][MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] string[] pPnPDeviceIDs, [In, Out] ref uint pcPnPDeviceIDs);&lt;/p&gt;
&lt;p&gt;Greetings from a fellow explorer ^^&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10378259" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#9271229</link><pubDate>Sun, 04 Jan 2009 15:27:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9271229</guid><dc:creator>viewon01</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I try to use the &amp;quot;GetStream&amp;quot; method but I got an exception &amp;quot;E_ACCESSDENIED&amp;quot;.&lt;/p&gt;
&lt;p&gt;It is on a &amp;quot;Zune&amp;quot; !&lt;/p&gt;
&lt;p&gt;Here is the code :&lt;/p&gt;
&lt;p&gt;uint optimalTransferSize = 0;&lt;/p&gt;
&lt;p&gt;PortableDeviceApiLib.IStream wpdStream;&lt;/p&gt;
&lt;p&gt;deviceResources.GetStream((string)music.Tag,&lt;/p&gt;
&lt;p&gt;ref PropertyKeys.WPD_RESOURCE_DEFAULT,0, ref optimalTransferSize, out wpdStream);&lt;/p&gt;
&lt;p&gt;Can you help me, I have no idea to solve this problem ?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9271229" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#8884235</link><pubDate>Thu, 21 Aug 2008 12:42:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8884235</guid><dc:creator>Tree</dc:creator><description>&lt;p&gt;Thanks Chris, I'm glad there is a cleaner solution! I'll pass that on to the guys who maintain my device code now.&lt;/p&gt;
&lt;p&gt;Tree&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8884235" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#8882049</link><pubDate>Wed, 20 Aug 2008 22:27:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8882049</guid><dc:creator>Ociter</dc:creator><description>&lt;p&gt;Tree,&lt;/p&gt;
&lt;p&gt;I figured out how to get at the returned pReadBytes for the sourceStream.Read function in case you are interested.&lt;/p&gt;
&lt;p&gt;Chris&lt;/p&gt;
&lt;p&gt;GCHandle hBytesRead = GCHandle.Alloc(new ulong(), GCHandleType.Pinned);&lt;/p&gt;
&lt;p&gt;do&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;sourceStream.Read(streamBuffer, (int)optimalBufferSize, hBytesRead.AddrOfPinnedObject());&lt;/p&gt;
&lt;p&gt;totalBytesRead += (ulong)hBytesRead.Target;&lt;/p&gt;
&lt;p&gt;} while ((ulong)hBytesRead.Target &amp;gt; 0);&lt;/p&gt;
&lt;p&gt;hBytesRead.Free();&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8882049" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#5263281</link><pubDate>Wed, 03 Oct 2007 17:24:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5263281</guid><dc:creator>Tree</dc:creator><description>&lt;p&gt;jtackabury: Just digging out the contants and helper classes I use. I'll mail them to you shortly.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5263281" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#5262847</link><pubDate>Wed, 03 Oct 2007 16:44:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5262847</guid><dc:creator>jtackabury</dc:creator><description>&lt;p&gt;Tree: Would you be able to send me your WPDPropertyKey class? &amp;nbsp;I seem to be having a hard time find the required header files and creating this class. &amp;nbsp;If you can, please send it to jtackabury (AT) binaryfortress.com.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5262847" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#4803586</link><pubDate>Fri, 07 Sep 2007 10:27:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4803586</guid><dc:creator>Tree</dc:creator><description>&lt;p&gt;The way I rebuild the interop for .net 2 is simply to use the .NET 2.0 ildasm disassembler and ilasm assembler, You'll find them in different places. This is literally what I do:&lt;/p&gt;
&lt;p&gt;1. Generate the initial interop in VS 2005 (or on the command line I guess)&lt;/p&gt;
&lt;p&gt;2. Run &amp;quot;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm&amp;quot; input\Interop.PortableDeviceApiLib.dll /out:pdapi2_0.il&lt;/p&gt;
&lt;p&gt;3. Make interop tweaks&lt;/p&gt;
&lt;p&gt;4. Reassemly the interop with: &amp;quot;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ilasm&amp;quot; pdapi2_0.il /dll /output=output\Interop.PortableDeviceApiLib.dll&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4803586" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#4790762</link><pubDate>Thu, 06 Sep 2007 21:18:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4790762</guid><dc:creator>Ociter</dc:creator><description>&lt;p&gt;Tree,&lt;/p&gt;
&lt;p&gt;I'm pretty new at .NET and C# and would really appriciate your help.&lt;/p&gt;
&lt;p&gt;Regarding your comment:&lt;/p&gt;
&lt;p&gt;# re: Enumerating WPD devices in C# &lt;/p&gt;
&lt;p&gt;Thursday, February 15, 2007 5:53 AM by Tree &lt;/p&gt;
&lt;p&gt;I tested the interop mod mentioned in the blog update, and while it works fine with .NET 2.0, a runtime MissingMethodException is thrown when attempting to run code which calls GetDevices using .NET 1.1. I guess this is just a limitation in 1.1 when defining method signatures.&lt;/p&gt;
&lt;p&gt;I am getting the MissingMethodException when calling GetDevices. I modified the il as specified by dimeby8 and thus it compiles. I have .NET 2.0 as well as .NET 1.1 installed on my machine. How do I specify .NET2.0?&lt;/p&gt;
&lt;p&gt;Thanks a bunch,&lt;/p&gt;
&lt;p&gt;Chris&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4790762" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#2374086</link><pubDate>Wed, 02 May 2007 16:36:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2374086</guid><dc:creator>Tree</dc:creator><description>&lt;p&gt;Here's a bit of code which I use to read an xml doc. Some of it is specific to our code, but you should get the idea. I have to admit it's not perfect. The problem exists because the pReadBytes from the read call always returns 0, because the interop can't do anything else, so you can't tell if you've finished reading without a slow check for a \0 the buffer. I tried fixing the interop a number of ways, but I'm no expert and had no success.&lt;/p&gt;
&lt;p&gt;My mods to the interop are fairly minimal, and not related to streams, although as I mentioned earlier I tweaks the remoteRead and remotewrite, but I don't use them anymore.&lt;/p&gt;
&lt;p&gt;Here's the code I use which works:&lt;/p&gt;
&lt;p&gt;XmlDocument GetXMLDocument(string fileId)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	//need a contents object to get the propeties&lt;/p&gt;
&lt;p&gt;	IPortableDeviceContent content;&lt;/p&gt;
&lt;p&gt;	PortableDevice.Content(out content);&lt;/p&gt;
&lt;p&gt;	//get transfer interface to this device&lt;/p&gt;
&lt;p&gt;	IPortableDeviceResources resources;&lt;/p&gt;
&lt;p&gt;	content.Transfer(out resources);&lt;/p&gt;
&lt;p&gt;	IStream wpdStream;&lt;/p&gt;
&lt;p&gt;	uint optimalTransferSize = 0;&lt;/p&gt;
&lt;p&gt;	resources.GetStream(fileId, ref WPDPropertyKey.WPD_RESOURCE_DEFAULT, 0, ref optimalTransferSize, out wpdStream);&lt;/p&gt;
&lt;p&gt;	//convert to a useful stream object&lt;/p&gt;
&lt;p&gt;	System.Runtime.InteropServices.ComTypes.IStream sourceStream =&lt;/p&gt;
&lt;p&gt;		(System.Runtime.InteropServices.ComTypes.IStream)wpdStream;&lt;/p&gt;
&lt;p&gt;	XmlDocument document = new XmlDocument();&lt;/p&gt;
&lt;p&gt;	////////Stream bit&lt;/p&gt;
&lt;p&gt;	using (MemoryStream mStream = new MemoryStream(1000))&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		int totalBytesRead = 0;&lt;/p&gt;
&lt;p&gt;		byte[] streamBuffer = new byte[optimalTransferSize];&lt;/p&gt;
&lt;p&gt;		IntPtr pBytesRead = new IntPtr();&lt;/p&gt;
&lt;p&gt;		int writeBytes = 0;&lt;/p&gt;
&lt;p&gt;		do&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			sourceStream.Read(streamBuffer, (int)optimalTransferSize, pBytesRead);&lt;/p&gt;
&lt;p&gt;			if (pBytesRead.ToInt32() == 0)&lt;/p&gt;
&lt;p&gt;				writeBytes = Array.FindIndex(streamBuffer, CheckEndBuffer);&lt;/p&gt;
&lt;p&gt;			else&lt;/p&gt;
&lt;p&gt;				writeBytes = pBytesRead.ToInt32();&lt;/p&gt;
&lt;p&gt;			totalBytesRead += writeBytes;&lt;/p&gt;
&lt;p&gt;			mStream.Write(streamBuffer, 0, writeBytes);&lt;/p&gt;
&lt;p&gt;		} while ((writeBytes &amp;gt; 0) &amp;amp;&amp;amp; (optimalTransferSize == writeBytes));&lt;/p&gt;
&lt;p&gt;		mStream.Flush();&lt;/p&gt;
&lt;p&gt;		mStream.Seek(0, SeekOrigin.Begin);&lt;/p&gt;
&lt;p&gt;		//load and interpret&lt;/p&gt;
&lt;p&gt;		if (sourceStream != null) document.Load(mStream);&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	return document;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2374086" width="1" height="1"&gt;</description></item><item><title>re: Enumerating WPD devices in C#</title><link>http://blogs.msdn.com/b/dimeby8/archive/2006/12/05/enumerating-wpd-devices-in-c.aspx#2372682</link><pubDate>Wed, 02 May 2007 14:33:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2372682</guid><dc:creator>jdawes</dc:creator><description>&lt;p&gt;Hello Tree !&lt;/p&gt;
&lt;p&gt;I have tried with ...ComTypes.IStream, but I get the same result.&lt;/p&gt;
&lt;p&gt;Have you done any changes to the IPortableDeviceResources.GetStream in the interop ?&lt;/p&gt;
&lt;p&gt;Could you please give me an example of how you do this ?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2372682" width="1" height="1"&gt;</description></item></channel></rss>