我试图找到答案,但没有运气.没有关于如何使用Marshal.QueryInterface(在我的情况下为"pISomething")返回的值的文档.它是一个实例IntPtr,但我需要它作为实例ISomething.简单的演员表不起作用.那么,有人可以展示如何使用由Marshal.QueryInterface?返回的接口?
例:
IBaseFilter pMyFilter = getMyFilter();
IntPtr pUnknown = Marshal.GetIUnknownForObject(pMyFilter);
IntPtr pISomething;
int success = Marshal.QueryInterface(pUnknown, ref IID_ISomething, out pISomething);
// success = 0 here!
Run Code Online (Sandbox Code Playgroud)
它是一种低级方法,就像所有Marshal方法一样,并不打算做你想要的.只有在需要将接口指针传递给本机代码时才使用它.
CLR将自动为您调用IUnknown :: QueryInterface(),而无需显式编程.您只需在C#代码中使用强制转换即可:
ISomething itf = (ISomething)pMyFilter;
Run Code Online (Sandbox Code Playgroud)
请注意,如果对象未实现接口,则会抛出异常.请注意,ISomething必须使用[ComImport]属性声明,就像IBaseFilter一样.如果导入了类型库,则为自动.正是该属性提供了CLR正确调用QueryInterface所需的IID.
| 归档时间: |
|
| 查看次数: |
1683 次 |
| 最近记录: |