使用 IStorage/IStream 从 C# 拖放到 Windows 资源管理器
我已经在听起来简单的功能上工作了太久了。 我的想法是我有一个带有 TreeView 的应用程序。 此树视图表示组织为文件和文件夹的数据库内容,与 Windows 资源管理器非常相似。 因此,用户应该能够将这些文件/文件夹从我的应用程序拖到 Windows 资源管理器中,这是有道理的。
我决定,如果他们拖动文件,我应该使用 IStream
;如果他们拖动文件夹,我应该使用 IStorage
。 经过很长一段时间并学习了比我想了解的更多有关 Win32/COM 的知识(以及这里一位非常乐于助人的人的推动),我终于有了 IStream
或 IStorage已创建。
然后我发现要将这个 IStream
或 IStorage
传递给 Windows 资源管理器,我必须将其放入 STGMEDIUM
中。 当然,medium.tymed 将是 IStream
或 IStorage
,具体取决于我使用的,但据我了解,medium.unionmember
code> 必须是指向 IStream/IStorage
的指针。
我尝试过使用fixed关键字(但它当然不是blittable),我尝试过将被破坏的东西转换为字节数组,但无法想出一种方法来实现这一点,我尝试过使用 GCHandle 来固定它(但它仍然不可 blittable),以及其他一些不起作用的东西。
所以我的问题是这样的:给定 IStorage s
和 STGMEDIUM m
,我如何填充 m.unionmember
以便 Windows 资源管理器会高兴并接受下降?
I've been working on what sounds like simple functionality for way too long now. The idea is that I have an application with a TreeView. This treeview represents contents of a database organized into files and folders, much like Windows Explorer. So it makes sense that the user should be able to drag those files/folders out of my app and into Windows Explorer.
I decided that I should use an IStream
if they drag a file or an IStorage
if they drag a folder. After a very long time and learning way more than I ever wanted to know about Win32/COM (and a nudge from a very helpful person here), I finally have that IStream
or IStorage
created.
Then I found that to pass this IStream
or IStorage
off to Windows Explorer, I had to get it in an STGMEDIUM
. Of course, the medium.tymed will be either IStream
or IStorage
, depending on which I'm using, but as I understand it the medium.unionmember
must be a pointer to the IStream/IStorage
.
I've tried using the fixed keyword (but it's not blittable, of course), I've tried just converting the blasted thing to a byte array, but can't come up with a way to make that happen, I've tried using GCHandle to pin it (but it still isn't blittable), and several other things that didn't work.
So my question is this: Given IStorage s
and STGMEDIUM m
, how do I populate m.unionmember
so that Windows Explorer will be happy and accept the drop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,C 联合在 C# 中使用
StructLayout.Explicit
表示。 然而,这可能不适用于您的情况 - 虽然您可以像这样覆盖IStream
和IStorage
引用,但编组器将尝试编组两者。 您最好的选择可能是在STGMEDIUM
声明中将该字段声明为IntPtr
,并使用Marshal.GetIUnknownForObject
对其进行初始化。Normally, C unions are represented using
StructLayout.Explicit
in C#. This probably won't work in your case, however - while you can overlayIStream
andIStorage
references like that, the marshaller will try to marshal both. Your best bet is probably to just declare the field asIntPtr
in your declaration ofSTGMEDIUM
, and useMarshal.GetIUnknownForObject
to initialize it.我认为这个主题已经过时很久了,但是任何想要文件拖放的人都可以查看此讨论,尤其是 dbirdz 的帖子: http://www.daniweb.com/forums/thread166429.html
在我看来,这甚至适用于这个应用程序,我会尝试将我的流写入一个临时文件,该文件可以然后被移动到它的位置。
I think the topic is outdated by long, however anyone looking for a file drag and drop check out this discussion and especially the post of dbirdz: http://www.daniweb.com/forums/thread166429.html
In my eyes this would even apply to this application, I'd try to write my stream into a temporary file which could then be moved to its location.