函数原型: 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);
}
本文介绍了如何在C#中实现与COM组件的交互,特别是针对窗口句柄类型的处理方式。详细展示了通过unsafe代码块将IntPtr类型转换为_RemotableHandle结构的过程。

被折叠的 条评论
为什么被折叠?



