为什么我的COM方法返回不正确的值?

时间:2021-08-24 02:07:38

I have a COM method which returns a MFC CRect:

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

GetMFCRect(LONG* pRect)
*((CRect*)pRect) = m_currentRect;

In my .NET application I try the following:

在我的.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));
}

But rc has the wrong values. What is wrong with this code?

但是rc有错误的价值观。这段代码有什么问题?

Thanks!

1 个解决方案

#1


AFAIK, Rectangle is a rather simple structure, and has no casting or conversion from CRect; what you see as values, there, are most probably pointers in the m_currentRect's VTable.
As a possible solution I would pass a GDI RECT structure, that is much safer, or directly the four coordinates.

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

#1


AFAIK, Rectangle is a rather simple structure, and has no casting or conversion from CRect; what you see as values, there, are most probably pointers in the m_currentRect's VTable.
As a possible solution I would pass a GDI RECT structure, that is much safer, or directly the four coordinates.

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