Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 67257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:13:54+00:00 2026-05-10T19:13:54+00:00

I’m trying to use opengl in C#. I have following code which fails with

  • 0

I’m trying to use opengl in C#. I have following code which fails with error 2000 ERROR_INVALID_PIXEL_FORMAT
First definitions:

[DllImport('user32.dll', CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern IntPtr GetDC(IntPtr hWnd);  [StructLayout(LayoutKind.Sequential)]     public struct PIXELFORMATDESCRIPTOR     {         public void Init()         {             nSize = (ushort) Marshal.SizeOf(typeof (PIXELFORMATDESCRIPTOR));             nVersion = 1;             dwFlags = PFD_FLAGS.PFD_DRAW_TO_WINDOW | PFD_FLAGS.PFD_SUPPORT_OPENGL | PFD_FLAGS.PFD_DOUBLEBUFFER | PFD_FLAGS.PFD_SUPPORT_COMPOSITION;             iPixelType = PFD_PIXEL_TYPE.PFD_TYPE_RGBA;             cColorBits = 24;             cRedBits = cRedShift = cGreenBits = cGreenShift = cBlueBits = cBlueShift = 0;             cAlphaBits = cAlphaShift = 0;             cAccumBits = cAccumRedBits = cAccumGreenBits = cAccumBlueBits = cAccumAlphaBits = 0;             cDepthBits = 32;             cStencilBits = cAuxBuffers = 0;             iLayerType = PFD_LAYER_TYPES.PFD_MAIN_PLANE;             bReserved = 0;             dwLayerMask = dwVisibleMask = dwDamageMask = 0;         }         ushort nSize;         ushort nVersion;         PFD_FLAGS dwFlags;         PFD_PIXEL_TYPE iPixelType;         byte cColorBits;         byte cRedBits;         byte cRedShift;         byte cGreenBits;         byte cGreenShift;         byte cBlueBits;         byte cBlueShift;         byte cAlphaBits;         byte cAlphaShift;         byte cAccumBits;         byte cAccumRedBits;         byte cAccumGreenBits;         byte cAccumBlueBits;         byte cAccumAlphaBits;         byte cDepthBits;         byte cStencilBits;         byte cAuxBuffers;         PFD_LAYER_TYPES iLayerType;         byte bReserved;         uint dwLayerMask;         uint dwVisibleMask;         uint dwDamageMask;     }      [Flags]     public enum PFD_FLAGS : uint      {         PFD_DOUBLEBUFFER = 0x00000001,         PFD_STEREO = 0x00000002,         PFD_DRAW_TO_WINDOW = 0x00000004,         PFD_DRAW_TO_BITMAP = 0x00000008,         PFD_SUPPORT_GDI = 0x00000010,         PFD_SUPPORT_OPENGL = 0x00000020,         PFD_GENERIC_FORMAT = 0x00000040,         PFD_NEED_PALETTE = 0x00000080,         PFD_NEED_SYSTEM_PALETTE = 0x00000100,         PFD_SWAP_EXCHANGE = 0x00000200,         PFD_SWAP_COPY = 0x00000400,         PFD_SWAP_LAYER_BUFFERS = 0x00000800,         PFD_GENERIC_ACCELERATED = 0x00001000,         PFD_SUPPORT_DIRECTDRAW = 0x00002000,         PFD_DIRECT3D_ACCELERATED = 0x00004000,         PFD_SUPPORT_COMPOSITION = 0x00008000,         PFD_DEPTH_DONTCARE = 0x20000000,         PFD_DOUBLEBUFFER_DONTCARE = 0x40000000,         PFD_STEREO_DONTCARE = 0x80000000     }      public enum PFD_LAYER_TYPES : byte     {         PFD_MAIN_PLANE = 0,         PFD_OVERLAY_PLANE = 1,         PFD_UNDERLAY_PLANE = 255     }      public enum PFD_PIXEL_TYPE : byte     {         PFD_TYPE_RGBA = 0,         PFD_TYPE_COLORINDEX = 1     }      [DllImport('gdi32.dll', CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]     public static extern int ChoosePixelFormat(IntPtr hdc, [In] ref PIXELFORMATDESCRIPTOR ppfd);      [DllImport('gdi32.dll', CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]     public static extern bool SetPixelFormat(IntPtr hdc, int iPixelFormat, ref PIXELFORMATDESCRIPTOR ppfd); [DllImport('opengl32.dll', CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]     public static extern IntPtr wglCreateContext(IntPtr hDC); 

And now the code that fails:

IntPtr dc = Win.GetDC(hwnd);  var pixelformatdescriptor = new GL.PIXELFORMATDESCRIPTOR(); pixelformatdescriptor.Init();  var pixelFormat = GL.ChoosePixelFormat(dc, ref pixelformatdescriptor); if(!GL.SetPixelFormat(dc, pixelFormat, ref pixelformatdescriptor))     throw new Win32Exception(Marshal.GetLastWin32Error()); IntPtr hglrc; if((hglrc = GL.wglCreateContext(dc)) == IntPtr.Zero)     throw new Win32Exception(Marshal.GetLastWin32Error()); //<----- here I have exception 

the same code in managed C++ is working

HDC dc = GetDC(hWnd);  PIXELFORMATDESCRIPTOR pf; pf.nSize = sizeof(PIXELFORMATDESCRIPTOR); pf.nVersion = 1; pf.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_SUPPORT_COMPOSITION; pf.cColorBits = 24; pf.cRedBits = pf.cRedShift = pf.cGreenBits = pf.cGreenShift = pf.cBlueBits = pf.cBlueShift = 0; pf.cAlphaBits = pf.cAlphaShift = 0; pf.cAccumBits = pf.cAccumRedBits = pf.cAccumGreenBits = pf.cAccumBlueBits = pf.cAccumAlphaBits = 0; pf.cDepthBits = 32; pf.cStencilBits = pf.cAuxBuffers = 0; pf.iLayerType = PFD_MAIN_PLANE; pf.bReserved = 0; pf.dwLayerMask = pf.dwVisibleMask = pf.dwDamageMask = 0;  int ipf = ChoosePixelFormat(dc, &pf); SetPixelFormat(dc, ipf, &pf);  HGLRC hglrc = wglCreateContext(dc); 

I’ve tried it on VIsta 64-bit with ATI graphic card and on Windows XP 32-bit with Nvidia with the same result in both cases.
Also I want to mention that I don’t want to use any already written framework for it.

Can anyone show me where is the bug in C# code that is causing the exception?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:13:54+00:00Added an answer on May 10, 2026 at 7:13 pm

    Found solution.
    Problem is very strange ugly and really hard to find. Somwhere on the internet I found that when you are linking opengl32.lib while compiling c++ application it must be placed before gdi32.lib. The reason for this is that (supposedly) opengl32.dll is overwriting ChoosePixelFormat and SetPixelFormat functions (and probably more :-). As I found in my c++ version, accidentally it was the case.
    Heh, but how to do it in C#
    After few days of searching I found that in tao framework they solved it using kernel32.dll LoadLibrary() function and loading opengl32.dll before calling SetPixelFormat

    public static bool SetPixelFormat(IntPtr deviceContext, int pixelFormat, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor) {         Kernel.LoadLibrary('opengl32.dll');         return _SetPixelFormat(deviceContext, pixelFormat, ref pixelFormatDescriptor);     } 

    So we know that opengl32.dll must be loaded before gdi32.dll, is there any other way of doing this. After while I thought that we can call some NOP function from opengl32.dll to load it. For example:

    [DllImport('opengl32.dll', EntryPoint = 'glGetString', CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] static extern IntPtr _glGetString(StringName name); public static string glGetString(StringName name) {     return Marshal.PtrToStringAnsi(_glGetString(name)); } public enum StringName : uint {     GL_VENDOR = 0x1F00,     GL_RENDERER = 0x1F01,     GL_VERSION = 0x1F02,     GL_EXTENSIONS = 0x1F03 } 

    and on the start of application, before any call to gdi32.dll I use this:

    GL.glGetString(0); 

    Both ways solves the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Generally, it's best to let the code itself explain what… May 11, 2026 at 1:45 pm
  • added an answer Yes, they are. Until there's a complete documentation on the… May 11, 2026 at 1:45 pm
  • added an answer Everything in CSS can be accessed through JS, but bear… May 11, 2026 at 1:45 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.