{"record":{"id":"4e8e3b102e4f5d56","repo":"unoplatform/uno","slug":"pinvoke-createwindowex-failed-win32helper-geterr","errorCode":null,"errorMessage":"PInvoke.CreateWindowEx failed: {Win32Helper.GetErrorMessage()}","messagePattern":"PInvoke\\.CreateWindowEx failed: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/AddIns/Uno.UI.MediaPlayer.Skia.Win32/Win32MediaPlayerPresenterExtension.cs","lineNumber":87,"sourceCode":"\t\t_extForNextCreateWindow = new WeakReference<Win32MediaPlayerPresenterExtension>(this);\n\t\t_hwnd = PInvoke.CreateWindowEx(\n\t\t\t0,\n\t\t\tlpClassName,\n\t\t\tnew PCWSTR(),\n\t\t\t0,\n\t\t\tPInvoke.CW_USEDEFAULT,\n\t\t\tPInvoke.CW_USEDEFAULT,\n\t\t\tPInvoke.CW_USEDEFAULT,\n\t\t\tPInvoke.CW_USEDEFAULT,\n\t\t\tHWND.Null,\n\t\t\tHMENU.Null,\n\t\t\tWin32Helper.GetHInstance(),\n\t\t\tnull);\n\t\t_extForNextCreateWindow = null;\n\n\t\tif (_hwnd == HWND.Null)\n\t\t{\n\t\t\tthrow new InvalidOperationException($\"{nameof(PInvoke.CreateWindowEx)} failed: {Win32Helper.GetErrorMessage()}\");\n\t\t}\n\n\t\tif (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace))\n\t\t{\n\t\t\tthis.Log().Trace(\"Created media player window.\");\n\t\t}\n\n\t\tvar success = PInvoke.RegisterTouchWindow(_hwnd, 0);\n\t\tif (!success && this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error))\n\t\t{\n\t\t\tthis.Log().Error($\"{nameof(PInvoke.RegisterTouchWindow)} failed: {Win32Helper.GetErrorMessage()}\");\n\t\t}\n\n\t\tvar cp = new ContentPresenter { Content = new Win32NativeWindow(_hwnd) };\n\t\t_presenter.Child = cp;\n\t\tcp.SizeChanged += (_, _) => StretchChanged();\n\t}\n","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.UI.MediaPlayer.Skia.Win32/Win32MediaPlayerPresenterExtension.cs#L69-L105","documentation":"InvalidOperationException thrown in the instance constructor of Win32MediaPlayerPresenterExtension when CreateWindowEx returns a null HWND, meaning the child window hosting the LibVLC surface could not be created. The window class must already be registered (error 30) for CreateWindowEx to succeed.","triggerScenarios":"Constructing Win32MediaPlayerPresenterExtension (on first media playback on Skia.Win32) when CreateWindowEx fails: the registered class is invalid, hInstance mismatch, the process lacks a window station/desktop, or extended styles are rejected. GetErrorMessage() gives the OS error code.","commonSituations":"Running the Skia Win32 media player in a headless/service context without an interactive desktop; a race where the window class registration partially failed; insufficient GDI/user objects; antivirus hooking window creation. The companion RegisterClassEx (error 30) typically succeeds first.","solutions":["Inspect Win32Helper.GetErrorMessage() in the exception — common codes: ERROR_INVALID_WINDOW_HANDLE (1400), ERROR_NOT_ENOUGH_MEMORY (8), access-denied on the desktop.","Ensure the process runs on an interactive window station/desktop (not session 0 / service).","Confirm the window class atom from RegisterClassEx is valid (if 30 failed first, 31 follows).","Reduce simultaneous window creation if hitting GDI/user object limits.","Update the Uno.UI.MediaPlayer.Skia.Win32 add-in to the latest version."],"exampleFix":"// No API tweak changes CreateWindowEx outcome; guard the environment.\nif (Environment.UserInteractive)\n{\n    _presenter = new Win32MediaPlayerPresenterExtension(mediaPresenter);\n}","handlingStrategy":"validation","validationCode":"// Verify an interactive desktop before constructing the media window\nif (OperatingSystem.IsWindows() && Environment.UserInteractive)\n    _presenter = new Win32MediaPlayerPresenterExtension(mediaPresenter);","typeGuard":"bool CanCreateWin32MediaWindow()\n    => OperatingSystem.IsWindows() && Environment.UserInteractive;","tryCatchPattern":"try { _presenter = new Win32MediaPlayerPresenterExtension(mediaPresenter); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"CreateWindowEx\"))\n{ Log.Error($\"Win32 media window creation failed: {ex.Message}\"); }","preventionTips":["Ensure the process has a window station/desktop with create-window rights.","Check the class atom from RegisterClassEx succeeded first (error 30).","Avoid headless/service contexts for media playback."],"tags":["win32","media-player","libvlc","skia","windows","uno-platform"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}