【发布时间】:2016-08-26 04:42:08
【问题描述】:
我有一个在 C++ 中调用 EGL 的程序。我想在 C# 中进行相同的调用,但 C# 中似乎没有等效的概念。
当执行上下文进入 C++ EGL 代码时,我收到一个读/写访问被拒绝错误。
这是我试图转换为 C# 的 C++ 程序中的代码:
PropertySet^ surfaceCreationProperties = ref new PropertySet();
surfaceCreationProperties->Insert(ref new String(EGLNativeWindowTypeProperty), somethingOtherThanAWindow);
mEglSurface = eglCreateWindowSurface(mEglDisplay, config, reinterpret_cast<IInspectable*>(surfaceCreationProperties), surfaceAttributes));
我有一个 C# 类,它将 C# EGL 调用转换为 C++ 调用。我相信 C++ 是不受管理的,虽然我不知道如何确切地告诉你。
C# 类如下所示:
public static IntPtr CreateWindowSurface(IntPtr dpy, IntPtr config, IntPtr win, int[] attrib_list)
{
IntPtr retValue;
unsafe {
fixed (int* p_attrib_list = attrib_list)
{
retValue = Delegates.peglCreateWindowSurface(dpy, config, win, p_attrib_list);
}
}
return (retValue);
}
更多代码可以在这里看到:https://github.com/luca-piccioni/OpenGL.Net/blob/master/OpenGL.Net/Egl.VERSION_1_0.cs#L751
你可能会注意到这个方法有一个IntPtr win——这是我传递PropertySet的地方。通常我认为这将是System.Windows.Forms.Control,但在 C++ EGL 代码中进行了一些检查以查看它是否是,或者它是否是 PropertySet。
被调用的 C++ 方法是这样的:
EGLSurface EGLAPIENTRY CreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
如您所见,C++ 方法需要一个 EGLNativeWindowType。我不确定 IInspectable 和 PropertSet 之间的关系是什么 - 可以强制转换这似乎很奇怪。
EGLNativeWindowType 的类型定义如下:
typedef HWND EGLNativeWindowType;
这意味着它必须是某种窗口句柄。我不明白 PropertySet 怎么可能是窗口句柄。
我怀疑主要问题在于选择正确的对象类型以传递给 C# EGL 实现。 PropertySet 似乎是正确的选择,但 reinterpret_cast 真的让我失望。
谁能帮我看看这个?
【问题讨论】:
-
我只能说你的代码看起来很可疑......我认为你不能在托管句柄上使用
reinterpret_cast将其转换为本机接口指针。 -
另外,鉴于 EGL 似乎是本机库,您可能应该在 C++/CLI(混合模式)中进行所有本机/托管转换,并且只在 C# 中使用托管代码。
-
@Phil1970 C++ - reinterpret_cast 的东西 - 工作正常。如果需要管理 EGL 才能使其正常工作,那么情况可能就是这样。 (我正在使用 ANGLE,这是一个 OpenGL 到 DX11 库,在我看来这意味着它很有可能被管理。)
-
@Phil1970 我的主要问题是内存访问异常。我认为这是因为一些托管 -> 本机进程,但我可能错了。
-
这很可疑,因为我找不到任何从经理引用到本机界面的
reinterpret_cast示例。您确定不会错过pin_ptr的电话吗?正如您应该知道的,托管引用与指针不同,并且可以在内存中移动,所以对我来说,进行这种强制转换看起来非常危险,就好像垃圾收集器决定在那一刻运行本机指针可能变得无效并导致您的应用程序崩溃。
标签: c# .net opengl c++-cli interop