9/14/2007

模擬 Drag and Drop 卡關之一, 即使用了 SHCreateDataObject 也不行嗎...

Ok, finally I found some time to update this post, let me tell what happen to me last week.

Story started when I'd need to write a unite test for the printers folder. One of the test case was to programmatically simulate drag and drop printing. This task really doesn't look difficult: You call IShellFolder::GetUIObjectOf to get the IDropTarget for the printer you'd like to use; then feed the object with the IDataObject that contains the list of files you'd like it to print. As for creating the IDataObject, you can use Shell API SHCreateDataObject, which takes an array of pidl and put that in the IDataObject. I expected that this would be pretty much what it takes to simulate drag and drop. Well, apparetnly there is no cheap lunch or this post won't ever be written. ;-)

With my assumption I implemented the code: Get the IDropTarget from the printer is pretty trivial and IDataObject was created alright. I fed the IDataObject to IDropTarget::Drop and the returning hr was succceeded as been verified by SUCCESS() macro. I ran my test, waited a bit, and wait for another bit, and wondering why nothing happened at all. Soon, I realized that the returning hr was actually S_FALSE instead of S_OK, I knew that the IDropTarget::Drop call never actually went through.

After sleeping over the problem for one night, I soon learned that it is the CF_HDROP data type is missing. Internally, CF_HDROP is a merely a DROPFILES data structure. It wasn't complex to fill it, and after feeding CF_HDROP to printer's IDropTarget, the document prints. Superb!!

沒有留言: