2017-02-15 202 views
0

我想使用一些pinvokes爲某些單元測試設置wgl上下文,但是(到目前爲止)我的方法之一不平衡堆棧。P /調用「wglChoosePixelFormatARB」不平衡堆棧

我所做的是先創建一個窗口並獲得其DC。這一切正如我在與kernel32,user32,gdi32庫一樣。我想用OpenGL繪製到pbuffer,爲了創建PB,我需要使用擴展。這就要求我有一個背景......這一切都很難以正常,並且迄今爲止工作。

問題出現在我嘗試創建pbuffer時。當我嘗試使用wglChoosePixelFormatARB獲得配置時,這看起來會使堆疊不平衡。我剛剛執行了另一個ARB方法(wglGetExtensionsStringARB)以檢查擴展 - 並且使用相同的父DC可以正常工作。

因此,在代碼...我的委託是這樣的:

[UnmanagedFunctionPointer(CallingConvention.Winapi)] 
[return: MarshalAs(UnmanagedType.Bool)] 
public delegate bool wglChoosePixelFormatARBDelegate(
    IntPtr dc, 
    [In] int[] attribIList, 
    [In] float[] attribFList, 
    uint maxFormats, 
    [Out] int[] pixelFormats, 
    out uint numFormats); 

我覺得是這樣的:

[DllImport(opengl32)] 
public static extern IntPtr wglGetProcAddress(string lpszProc); 
// ... 
var ptr = wglGetProcAddress("wglCreatePbufferARB"); 
wglCreatePbufferARB = Marshal.GetDelegateForFunctionPointer(ptr, typeof(wglChoosePixelFormatARBDelegate)); 

而且我調用它像這樣:

var iAttrs = new int[] 
{ 
    Wgl.WGL_ACCELERATION_ARB, Wgl.WGL_FULL_ACCELERATION_ARB, 
    Wgl.WGL_DRAW_TO_WINDOW_ARB, Wgl.TRUE, 
    Wgl.WGL_SUPPORT_OPENGL_ARB, Wgl.TRUE, 
    Wgl.NONE, Wgl.NONE 
}; 
var fAttrs = new float[2]; 
var piFormats = new int[1]; 
uint nFormats; 
wglChoosePixelFormatARB(
    parentDC, 
    iAttrs, 
    fAttrs, 
    (uint)piFormats.Length, 
    piFormats, 
    out nFormats); 
if (nFormats == 0) 
{ 
    return IntPtr.Zero; 
} 

var pbuf = extensions.wglCreatePbufferARB(parentDC, piFormats[0], 1, 1, null); 

這是本機端(不輸出):

BOOL WINAPI wglChoosePixelFormatARB (
    HDC hdc, 
    const int *piAttribIList, 
    const FLOAT *pfAttribFList, 
    UINT nMaxFormats, 
    int *piFormats, 
    UINT *nNumFormats); 

和函數DEF是這樣的:

typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (
    HDC hdc, 
    const int *piAttribIList, 
    const FLOAT *pfAttribFList, 
    UINT nMaxFormats, 
    int *piFormats, 
    UINT *nNumFormats); 

代碼看起來OK我,但一定是有什麼錯:)我希望有人能指出我的錯誤。

如果需要更多的代碼,我在這裏有它在一個單一的文件,該文件只是一個簡單的控制檯應用程序: https://gist.github.com/mattleibow/755eba3c8ff5eafb9549842a0abb0426

(代碼有意見大塊,這僅僅是因爲我從C++到C#的繁忙移植,以及三分之一的代碼只是dllimports/structs/enums)

+0

你明白警告的含義嗎?如果是這樣,那麼你會意識到我們也需要看到非託管聲明。 –

+0

@DavidHeffernan陷入困境,忘了這一點。 – Matthew

回答

2

函數聲明看起來很合理,但看起來你只是簡單地導入了錯誤的函數。

想要wglChoosePixelFormatARB但實際導入wglCreatePbufferARB。這聞起來像一個班複製/粘貼SNAFU。

通過更正您傳遞給GetProcAddress的名稱來解決此問題。

+0

認真嗎?我檢查了一切,但織物字符串?已經在這裏幾個小時了......謝謝!看起來我確實需要睡眠。 http://m.memegen.com/qis49q.jpg – Matthew