Python for .NET

I have a problem loading an external dll using Python via Python for .NET. I tried different methodologies after stackoverflow and similar. I will try to generalize the situation and describe all the steps that I have taken.

I have a dll so named for example. Test.NET.dll. I checked with dotPeek and I can see by clicking on it, x64 and the .NET Framework v4.5. On my computer, I installed .Net Framework 4.

I also installed Python for .NET in different ways. I think the best one is to download .whl from this LINK site. I downloaded and installed: pythonnet-2.0.0.dev1-cp27-none-win_amd64.whl. I can imagine that it will work for .NET 4.0, as it requires Microsoft.NET Framework 4.0.

Once I installed everything, I can execute the following commands:

>>> import clr >>> import System >>> print System.Environmnet.Version >>> print System.Environment.Version 4.0.30319.34209 

It seems to work. Then I tried to load my dll by typing the following commands:

 >>> import clr >>> dllpath= r'C:\Program Files\API\Test.NET' >>> clr.AddReference(dllpath) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> clr.AddReference(dllpath) FileNotFoundException: Unable to find assembly 'C:\Program Files\API\Test.NET'. at Python.Runtime.CLRModule.AddReference(String name) 

I also tried adding '.dll' to the end of the path, but nothing changed. Then I also tried various solutions, as described in LINK , LINK , LINK and much more .... Unfortunately, this does not work, and I get different errors. I know that IronPython exists, but I tried to avoid using it.

Thank you for your help!

+6
source share
2 answers

Was Test.NET.dll from another computer? According to this thread of some .NET security features, it can prevent DLL files from loading beautifully.

For a more informative error message try

 > from clr import System > from System import Reflection > full_filename = r'C:\Program Files\API\Test.NET' > Reflection.Assembly.LoadFile(dllpath) 

If you get an error in the lines

 NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. 

then the following solution to the problem for me:

  • right click on Test.NET.dll
  • select "Properties"
  • on the General tab, click "Unblock"
  • click "apply"
+1
source

This is not a complete answer, but help future readers: you should not trust FileNotFoundException clr.AddReference(dllpath) Unfortunately, while trial assembly exceptions are swallowed

 public static Assembly AddReference(string name) { AssemblyManager.UpdatePath(); Assembly assembly = null; assembly = AssemblyManager.LoadAssemblyPath(name); if (assembly == null) { assembly = AssemblyManager.LoadAssembly(name); } if (assembly == null) { string msg = String.Format("Unable to find assembly '{0}'.", name); throw new System.IO.FileNotFoundException(msg); } return assembly ; } 

AssemblyManager.LoadAssemblyPath swallow exceptions

 try { assembly = Assembly.LoadFrom(path); } catch {} 

AssemblyManager.LoadAssembly also caresses exceptions

 try { assembly = Assembly.Load(name);} catch (System.Exception e) {} 

You can check the list of possible swallowed exceptions in Assembly.LoadFrom and Assembly.Load to find out the possible real reasons.

+1
source

Source: https://habr.com/ru/post/988583/


All Articles