System.AccessViolationException when adding a new item in Visual Studio using IVsProject.AddItem

Julius 20 Reputation points
2024-10-16T21:32:58.7566667+00:00

I'm encountering a System.AccessViolationException while working in Visual Studio 2022. The error occurs specifically when I try to add a new UI or CLR item to a project using the IVsProject.AddItem() method. Below is the full exception log:

Aplicación: devenv.exe
Versión de Framework: v4.0.30319
Descripción: el proceso terminó debido a una excepción no controlada.
Información de la excepción: System.AccessViolationException
   en Microsoft.VisualStudio.Shell.Interop.IVsProject.AddItem(UInt32, Microsoft.VisualStudio.Shell.Interop.VSADDITEMOPERATION, System.String, UInt32, System.String[], IntPtr, Microsoft.VisualStudio.Shell.Interop.VSADDRESULT[])
   en Microsoft.VisualStudio.Dialogs.ServiceHelper.AddNewItem(Microsoft.VisualStudio.TemplateProviders.Templates.IVsTemplate, System.String, Microsoft.Internal.VisualStudio.Shell.Interop.VSNEWPROJECTDLGCTX, Microsoft.VisualStudio.Shell.Interop.IVsProject, UInt32, Boolean, UInt32, Boolean, System.String, Microsoft.VisualStudio.Dialogs.OpenWithEditorSelection, Boolean)
   en Microsoft.VisualStudio.NewProjectDialog.LegacyItemTemplateProvider+<InstantiateItemTemplateAsync>d__14.MoveNext()
   en System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   en System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   en System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
   en Microsoft.VisualStudio.Threading.JoinableTaskFactory+SingleExecuteProtector.TryExecute()
   en Microsoft.VisualStudio.Threading.JoinableTask.CompleteOnCurrentThread()
   en Microsoft.VisualStudio.Threading.JoinableTask`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].CompleteOnCurrentThread()
   en Microsoft.VisualStudio.Dialogs.ServiceHelper.AddNewItem(Microsoft.VisualStudio.TemplateProviders.Templates.IVsTemplate, System.String, Microsoft.Internal.VisualStudio.Shell.Interop.VSNEWPROJECTDLGCTX, Microsoft.VisualStudio.Shell.Interop.IVsProject, UInt32, Boolean, UInt32, Boolean, System.String, Microsoft.VisualStudio.Dialogs.OpenWithEditorSelection, Boolean)
   en Microsoft.VisualStudio.Dialogs.SVsDialogService.InvokeDialog(Microsoft.Internal.VisualStudio.Shell.Interop.VSNEWPROJECTDLGINFO, System.String ByRef)
   en Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchy.ExecCommand(UInt32, System.Guid ByRef, UInt32, UInt32, IntPtr, IntPtr)
   en Microsoft.Internal.VisualStudio.PlatformUI.HierarchyUtilities+<>c__DisplayClass35_0.<ExecHierParentChain>b__1()
   en Microsoft.VisualStudio.ErrorHandler.CallWithCOMConvention(System.Func`1<Int32>, Boolean, Boolean)
   en Microsoft.Internal.VisualStudio.PlatformUI.HierarchyUtilities.ExecHierParentChain(Microsoft.VisualStudio.Shell.IVsHierarchyItemManager, Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchy, Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchy, UInt32, System.Guid ByRef, UInt32, UInt32, IntPtr, IntPtr)
   en Microsoft.VisualStudio.PlatformUI.PivotNavigatorCommandTarget.Exec(System.Guid ByRef, UInt32, UInt32, IntPtr, IntPtr)
   en Microsoft.VisualStudio.PlatformUI.SolutionNavigatorCommandTarget.Exec(System.Guid ByRef, UInt32, UInt32, IntPtr, IntPtr)
   en Microsoft.VisualStudio.Platform.WindowManagement.DocumentObjectSite.<Exec>g__TryHandleExec|82_0(System.Object, Int32 ByRef, <>c__DisplayClass82_0 ByRef)
   en Microsoft.VisualStudio.Platform.WindowManagement.DocumentObjectSite.Exec(System.Guid ByRef, UInt32, UInt32, IntPtr, IntPtr)
   en Microsoft.VisualStudio.Platform.WindowManagement.WindowFrame.Exec(System.Guid ByRef, UInt32, UInt32, IntPtr, IntPtr)

This crash consistently happens when adding UI elements or CLR templates to a project. I have tried repairing Visual Studio, updating it to the latest version, and even reinstalling, but the issue persists. Here are the things I've tried:

  • Running Visual Studio as an administrator.
  • Checking for corrupted templates or missing dependencies.
  • Resetting Visual Studio settings.

Any suggestions on how to resolve or further investigate this issue? What could be causing this AccessViolationException when adding UI or CLR items?

Environment:

  • Visual Studio version: 2022
  • OS: Windows 11 24H2
  • .NET Framework: v4.0.30319
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,313 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
340 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,807 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,182 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,284 questions
{count} votes

Accepted answer
  1. Anna Xiu-MSFT 30,391 Reputation points Microsoft Vendor
    2024-10-28T09:01:40.7466667+00:00

    Hi @Julius,

    Do you have any update with the issue? 

    It may be a Windows issue. Please manually create the following key and value:

    Path: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Internet Explorer\Main

    Name: JScriptReplacement

    Type: REG_DWORD

    Value: 0

    Then, reboot the computer and add the Windows Forms UI again. 

    Sincerely,

    Anna


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anna Xiu-MSFT 30,391 Reputation points Microsoft Vendor
    2024-10-18T09:30:59.86+00:00

    Hi @Julius, 

    Welcome to Microsoft Q&A! 

    I created a CLR Empty Project (.NET Framework) > right-click on the project node > Add > New item… > select UI or CLR > Windows Forms > Add.

    Then, I will get the following error, but my VS will not close.

    User's image

    I suspect there is a known issue with CLR project, I suggest you report it to product team in Developer Community

    After you report the issue, you can share the feedback ticket link here. Thanks for your feedback. 

    Sincerely,

    Anna


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.