【问题标题】:Why is my COM method returning an incorrect value?为什么我的 COM 方法返回不正确的值?
【发布时间】:2010-11-07 03:02:22
【问题描述】:

我有一个返回 MFC CRect 的 COM 方法:

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 的值有误。这段代码有什么问题?

谢谢!

【问题讨论】:

    标签: .net com mfc


    【解决方案1】:

    AFAIK,Rectangle 是一个相当简单的结构,没有来自 CRect 的转换或转换;您所看到的值很可能是 m_currentRect 的 VTable 中的指针。
    作为一种可能的解决方案,我会传递一个更安全的 GDI RECT 结构,或者直接传递四个坐标。

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多