我正在尝试将System::Windows::Forms::IDataObject转换为ObjIdl.h中定义的本机IDataObject。不幸的是,以下源代码没有返回本地IDataObject。
IntPtr pData = System::Runtime::InteropServices::Marshal::GetIUnknownForObject(e->Data);
::IDataObject* data = (::IDataObject*) pData.ToPointer();
在拖放过程中如何在Windows Forms应用程序中获取本机IDataObject?
最佳答案
用C样式转换强制转换COM接口指针是不合法的。您必须使用QueryInterface()方法很好地询问。像这样:
System::Windows::Forms::IDataObject^ obj = Clipboard::GetDataObject();
::IUnknown* punk = (::IUnknown*)System::Runtime::InteropServices::Marshal::GetIUnknownForObject(obj).ToPointer();
::IDataObject* pdata = nullptr;
HRESULT hr = punk->QueryInterface(__uuidof(::IDataObject), (void**)&pdata);
if (SUCCEEDED(hr)) {
// etc..
}