如何访问本机IDataObject

时间:2012-11-01 13:52:10

标签: c++ .net visual-c++ com c++-cli

我正在尝试将System::Windows::Forms::IDataObject转换为ObjIdl.h中定义的本地IDataObject。不幸的是,以下源代码不返回本机IDataObject。

IntPtr pData = System::Runtime::InteropServices::Marshal::GetIUnknownForObject(e->Data);
::IDataObject* data = (::IDataObject*) pData.ToPointer();

如何在拖放期间在Windows窗体应用程序中获取本机IDataObject?

1 个答案:

答案 0 :(得分:2)

使用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..
 }