[英]Autoplay handler: wrong clipboard format?
I'm trying to implement a Windows Autoplay handler; 我正在尝试实现Windows自动播放处理程序; according to the documentation and the examples I found, I'm supposed to query the
IDataObject
for the "Autoplay Enumerated IDList Array"
clipboard format. 根据我发现的文档和示例,我应该向
IDataObject
查询"Autoplay Enumerated IDList Array"
剪贴板格式。
So I tried to do something like that: 所以我试图做这样的事情:
[DllImport("user32.dll", SetLastError = true, EntryPoint = "RegisterClipboardFormatW")]
public static extern uint RegisterClipboardFormat([MarshalAs(UnmanagedType.LPWStr)] String format);
private const string CFSTR_AUTOPLAY_SHELLIDLISTS = "Autoplay Enumerated IDList Array";
private static readonly uint AUTOPLAY_SHELLIDLISTS = RegisterClipboardFormat(CFSTR_AUTOPLAY_SHELLIDLISTS);
...
public int Drop(IDataObject pDataObj, int grfKeyState, Point pt, ref DropEffect pdwEffect)
{
var fmt = new FORMATETC
{
cfFormat = (short)AUTOPLAY_SHELLIDLISTS,
ptd = IntPtr.Zero,
dwAspect = DVASPECT.DVASPECT_CONTENT,
lindex = -1,
tymed = TYMED.TYMED_HGLOBAL
};
int hr = pDataObj.QueryGetData(ref fmt);
if (hr == S_OK)
{
...
}
return 0;
}
But QueryGetData
always returns S_FALSE
(and GetData
throws, obviously). 但是
QueryGetData
总是返回S_FALSE
(很明显, GetData
抛出)。 So I tried to enumerate the available formats with EnumFormatEtc
:
it returns only one format
it returns 4 formats, none of which is the same as the one I passed to QueryGetData
( cfFormat
values are -16238, 15, -16378 and -16377). 因此,我尝试使用
EnumFormatEtc
枚举可用格式:
它仅返回一种格式,
它返回4种格式,这些格式均与我传递给QueryGetData
格式相同( cfFormat
值为-16238、15,-16378和-16377)。 If I use the first format from EnumFormatEtc
, instead of AUTOPLAY_SHELLIDLISTS
, everything works fine, but I don't think it's the right way to do it... 如果我使用
EnumFormatEtc
的第一种格式,而不是AUTOPLAY_SHELLIDLISTS
,那么一切都很好,但是我认为这不是正确的方法...
Could someone explain what is going on? 有人可以解释发生了什么吗? Am I using the wrong format?
我使用的格式错误吗?
EDIT: apparently the first format returned from EnumFormatEtc
is "Shell IDList Array"
; 编辑:显然,从
EnumFormatEtc
返回的第一种格式是"Shell IDList Array"
; clearly I can work with that, but what happened to "Autoplay Enumerated IDList Array"
? 显然我可以使用它,但是
"Autoplay Enumerated IDList Array"
发生了什么?
OK, I finally found a much easier way to do it, using the System.Windows.Forms.DataObject
class: 好的,我终于找到了一种更简单的方法,使用
System.Windows.Forms.DataObject
类:
public int Drop(IDataObject pDataObj, int grfKeyState, Point pt, ref DropEffect pdwEffect)
{
try
{
var dataObj = new DataObject(pDataObj);
if (dataObj.ContainsFileDropList())
{
StringCollection files = dataObj.GetFileDropList();
// Do something with files...
}
return 0;
}
catch(Exception ex)
{
Trace.WriteLine(string.Format("Error: {0}", ex));
return 1;
}
}
No need for any low-level COM interop (except the IDropTarget
interface declaration)... 不需要任何低级COM互操作(
IDropTarget
接口声明除外)...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.