dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.75k stars 190 forks source link

System.TypeInitializationException: The type initializer for '<Module>' threw an exception. #464

Open kelilive opened 2 months ago

kelilive commented 2 months ago

Describe the bug and how to reproduce

Application does not support just-in-time (JIT) debugging. See the end of this message for details.

** Exception Text ** System.TypeInitializationException: The type initializer for '' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Vanara.PInvoke.User32, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c37e4080322237fa' or one of its dependencies. The system cannot find the file specified. at System.ModuleHandle.ResolveMethod(RuntimeModule module, Int32 methodToken, IntPtr typeInstArgs, Int32 typeInstCount, IntPtr methodInstArgs, Int32 methodInstCount) at System.ModuleHandle.ResolveMethodHandleInternalCore(RuntimeModule module, Int32 methodToken, IntPtr[] typeInstantiationContext, Int32 typeInstCount, IntPtr[] methodInstantiationContext, Int32 methodInstCount) at System.ModuleHandle.ResolveMethodHandleInternal(RuntimeModule module, Int32 methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) at System.Reflection.RuntimeModule.ResolveMethod(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) --- End of inner exception stack trace --- at 1BE442B8.6C8F42B5(Exception 75941B80) at 1BE442B8.44455E8D(Object[] 4F450FCC, Int32 15625F0E) at .cctor() --- End of inner exception stack trace --- at DrawingMaster.BlockManage.BlockManageCommand.Bm() at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

** JIT Debugging ** Application does not support Windows Forms just-in-time (JIT) debugging. Contact the application author for more information.

What code is involved

User32.SetFocus(doc.Window.Handle);

Expected behavior

When I tried to use the user32 API to activate focus on the current document window of AutoCAD, I reported an error. I will try not to use the encapsulated assembly later, because it prompts loading problems.

Screenshots

2024-05-08_151746

kelilive commented 2 months ago

I originally wanted to write one myself for testing, but I changed my mind and wanted to verify that Dapplo.Windows.User32 can run very well using other assembly tests. Therefore, there is little point in rewriting it yourself. The problem has basically been analyzed, and the assembly compatibility and reliability should be optimized.