title: How to create a "Shell IDList Array" to support drag-and-drop of virtual files from C# to Windows Explorer?
body: <p>I started trying to implement drag-and-drop of virtual files (from a C# 4/WPF app) with <a href="http://www.codeproject.com/KB/dotnet/DataObjectEx.aspx?fid=987712&amp;df=90&amp;mpp=25&amp;noise=3&amp;sort=Position&amp;view=Quick&amp;fr=26#xx0xx" rel="nofollow noreferrer">this codeproject tutorial</a>. After spending some time <a href="https://stackoverflow.com/questions/2917988/drag-and-drop-to-explorer-causing-invalid-formatetc-dv-e-formatetc-error">trying to figure out a DV_E_FORMATETC</a> error, I figured out I need to support the "Shell IDList Array" data format. But there seems to be next to zero documentation on what this format actually does.</p>
  
  <p>After some searching, I found <a href="http://netez.com/2xExplorer/shellFAQ/adv_drag.html" rel="nofollow noreferrer">this page on advanced data transfer</a> which said that a Shell IDList Array was a pointer to a <a href="http://msdn.microsoft.com/en-us/library/bb773212.aspx" rel="nofollow noreferrer">CIDA structure</a>. This CIDA structure then contains the number of PIDLs, and a list of offsets to them. So what the hell is a PIDL? After some more searching, <a href="http://msdn.microsoft.com/en-us/library/cc144089.aspx" rel="nofollow noreferrer">this page</a> sort of implies it's a pointer to an <a href="http://msdn.microsoft.com/en-us/library/bb773321.aspx" rel="nofollow noreferrer">ITEMIDLIST</a>, which itself contains a single member that is a list of <a href="http://msdn.microsoft.com/en-us/library/bb759800.aspx" rel="nofollow noreferrer">SHITEMID</a>s.</p>
  
  <p>I did some more reading, I still can't tell what multiple PIDLs are for, but there's one SHITEMID for each "level" in the path. At least that's one mystery solved.</p>
  
  <p>My next idea was to try dragging a file from another application with virtual files (WinSCP). I just got a MemoryStream back for this format. At least I know what class to provide for the thing, but that doesn't help at all for explaining what to put in it. I tried examining this MemoryStream based on the formats in the links above, but I've had no success. I just got back garbage data, and one of the 'cb' fields told me it was 18000 bytes long when the whole stream was only 539 bytes.</p>
  
  <p>In addition, upon further reading, <a href="http://netez.com/2xExplorer/shellFAQ/bg_shell.html" rel="nofollow noreferrer">this page</a> seems to imply that the data contained in a PIDL actually winds up being a path, and examining the contents of said MemoryStream in a hex editor yielded a path inside my local Temp directory (split into parts, anyway). </p>
  
  <p>It seems that WinSCP just uses a shell extension to handle dropping on explorer, something I really don't want to resort to. However, it has an alternate mode where it aborts the dragdrop until after it transfers to a temp folder - this is acceptable to me, but I haven't the faintest idea how to do it.</p>
  
  <p>My questions are now:</p>
  
  <ol>
  <li>What is a valid "abID" member for a SHITEMID? These virtual files only exist with my program; will the thing they are dragged to pass the "abID" back later when it executes GetData? Do they have to be system-unique?</li>
  <li>How can I manage the abort-and-redo-later drag and drop thing that WinSCP does?</li>
  <li>If I have to implement a shell extension for this, how would I even go about that? I'm sure I can easily find explanations of shell extension theory but how would I support custom PIDLs or whatever?</li>
  </ol>
  
  <p>Any help or even links that explain what I should be doing would be greatly appreciated.</p>
  
  <p><strong>Edit:</strong> So here's how I actually did it in the end:</p>
  
  <p>I scrapped most of the code from the CodeProject tutorial above, keeping the functions for creating the FileGroupDescriptor. I then reimplemented the .Net IDataObject interface (and actually, I didn't have to use the COM IDataObject interface at all). I then am forced to synchronously download the file in the background, and pass a MemoryStream back out from GetData(). Conveniently, the actual <em>copying</em> is in the background, but the <em>waiting for data</em> is in the foreground. Thanks, explorer. Any reasonably large files are slow, but for now it "works" which is more than I can say for the past few weeks.</p>
  
  <p>I did try passing the <a href="http://www.codeproject.com/KB/threads/PipeStream.aspx" rel="nofollow noreferrer">PipeStream</a> class I use internally for transfers, but explorer isn't happy with this:</p>
  
  <ul>
  <li>It tries to Seek, despite this class having CanSeek be false.</li>
  <li>It ignores the size I send in the FileGroupDescriptor and just downloads whatever's in the stream <em>right now</em>.</li>
  </ul>
  
  <p>So, I'm not sure the ideal case is actually possible. Still, thanks for all of your help. I'm giving the bounty to JPW because his replies eventually set me on the right path.</p>