现在的位置: 首页 > 综合 > 正文

COM导入到C#开发 HWND句柄类型 对应于C#的 _RemotableHandle结构处理

2013年09月05日 ⁄ 综合 ⁄ 共 588字 ⁄ 字号 评论关闭

函数原型:            ShowMessage(HWND hwnd,uint msg,int wp,int lp)

COM导入进C#:   ShowMessage(_RemotableHandle hwnd,uint msg,int wp,int lp)

导入COM之后 在C++中调用直接用HWND句柄类型,而在C#中,类型为_RemotableHandle,此为一个结构:

    public struct _RemotableHandle
    {
        public int fContext;
        public __MIDL_IWinTypes_0009 u;
    }

 

    public struct __MIDL_IWinTypes_0009
    {
        public int hInproc;
        public int hRemote;
    }

 

那么C#中的窗口句柄IntPtr是不能as到_RemotableHandle结构,也不能强制类型转换. 尝试Marshal.PtrToStructure 仍然失败.最后采用以下方法成功:

unsafe

{
           RemotableHandle* rh = (_RemotableHandle*)this.Handle.ToPointer();
           ShowMessage(ref *rh, 0x1000, 0, 0); 

}

抱歉!评论已关闭.