我有一个返回mfc的com方法 CRect :
CRect
GetMFCRect(LONG* pRect) *((CRect*)pRect) = m_currentRect;
在.NET应用程序中,我尝试以下操作:
int pointer = new int(); Rectangle rc; IntPtr pIntResult; unsafe { int* p = &pointer; _COMobj.GetMFCRect(ref *p); pIntResult = new IntPtr(p); rc = (Rectangle)Marshal.PtrToStructure(pintResult, typeof(Rectangle)); }
但是 rc 有错误的值。这个密码怎么了?
rc
谢谢!
嗯, Rectangle 是一个相当简单的结构,并且没有从crect转换或转换;您所看到的值,很可能是m戋currentrect的vtable中的指针。 作为一个可能的解决方案,我将传递一个gdi rect结构,这样更安全,或者直接传递四个坐标。