{"record":{"id":"8c044ecb0ecae018","repo":"dotnet/wpf","slug":"win32exception-errorcode","errorCode":null,"errorMessage":"Win32Exception(errorCode)","messagePattern":"Win32Exception\\(errorCode\\)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Cursor.cs","lineNumber":190,"sourceCode":"            if (_cursorHandle == null || _cursorHandle.IsInvalid)\n            {\n                // LoadImage returns a null handle but does not set\n                // the error condition when icon file is of an incorrect type (e.g., .bmp)\n                //\n                // LoadImage has a bug where it doesn't set the correct error code\n                // when a file is given that is not an ico file.  Icon load fails\n                // but win32 error code is still zero (success).  Thus, we need to\n                // special case this scenario.\n                //\n                if (errorCode != 0)\n                {\n                    if ((errorCode == NativeMethods.ERROR_FILE_NOT_FOUND) || (errorCode == NativeMethods.ERROR_PATH_NOT_FOUND))\n                    {\n                        throw new Win32Exception(errorCode, SR.Format(SR.Cursor_LoadImageFailure, fileName));\n                    }\n                    else\n                    {\n                        throw new Win32Exception(errorCode);\n                    }\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.Cursor_LoadImageFailure, fileName));\n                }\n            }\n        }\n\n        //**** DEAD CODE - retained only for compat, if user sets quirk flag  ****\n        private const int BUFFERSIZE = 4096; // the maximum size of the buffer used for loading from stream\n\n        private void LegacyLoadFromStream(Stream cursorStream)\n        {\n            //Generate a temporal file based on the memory stream.\n\n            // GetTempFileName requires unrestricted Environment permission\n            // FileIOPermission.Write permission.  However, since we don't","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Cursor.cs#L172-L208","documentation":"Cursor.LoadFromFile throws a bare Win32Exception(errorCode) when the Win32 LoadImage call fails with a Win32 error code other than ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND (e.g. ERROR_ACCESS_DENIED, ERROR_INVALID_PARAMETER). Unlike the not-found case, this exception carries only the native error code, so the message text is whatever Win32Exception maps the code to.","triggerScenarios":"new Cursor(fileName) where LoadImage fails with an unexpected native error — file exists but is not a valid cursor file, is corrupt, is locked, or access is denied (shares/permissions).","commonSituations":"Pointing a Cursor at a .png/.ico/.bmp instead of a real .cur/.ani file; corrupted downloads; file locked by another process; missing read permission; antivirus blocking reads.","solutions":["Confirm the file is a genuine .cur or .ani cursor file, not a renamed image.","Check the Win32Exception.NativeErrorCode and look up its meaning (e.g. 5 = access denied → fix file ACLs).","Verify the file is not locked by another process and readable by the app identity.","Re-export/re-download the cursor file to rule out corruption.","Wrap construction in try-catch and fall back to a built-in System.Windows.Input.Cursors value."],"exampleFix":"// before\ncursor = new Cursor(logoPath); // logoPath points to a .png\n// after\nif (Path.GetExtension(logoPath).Equals(\".cur\", StringComparison.OrdinalIgnoreCase))\n    cursor = new Cursor(logoPath);\nelse\n    cursor = Cursors.Arrow;","handlingStrategy":"try-catch","validationCode":"if (!File.Exists(path)) throw new FileNotFoundException(path);\nvar ext = Path.GetExtension(path).ToLowerInvariant();\nif (ext != \".cur\" && ext != \".ani\") throw new InvalidOperationException($\"Not a cursor file: {ext}\");","typeGuard":"bool LooksLikeCursorFile(string path) => File.Exists(path) && \".cur|.ani\".Contains(Path.GetExtension(path).ToLowerInvariant());","tryCatchPattern":"try { cursor = new Cursor(path); }\ncatch (Win32Exception ex)\n{ logger.LogError(\"LoadImage failed, native error {Code}\", ex.NativeErrorCode); cursor = Cursors.Arrow; }","preventionTips":["Only pass genuine .cur/.ani files — never renamed .png/.ico/.bmp files.","Inspect NativeErrorCode to distinguish access-denied from corrupt-file issues.","Verify file permissions for the running app identity in restricted environments.","Fall back to built-in Cursors values for non-critical customization."],"tags":["wpf","cursor","win32","loadimage"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}