Skip to content

MSLNZ/msl-loadlib

Repository files navigation

MSL-LoadLib

CI Status Docs Status PyPI - Version

This package loads a library in Python. It is basically just a thin wrapper around ctypes (for libraries that use the __cdecl or __stdcall calling convention), Python.NET (for libraries that use Microsoft .NET, CLR), Py4J (for Java .jar or .class files) and comtypes (for libraries that use the Component Object Model or ActiveX).

However, the primary advantage is that it is possible to communicate with a 32-bit library from 64-bit Python.

msl-loadlib is a pure-python package, but Python.NET depends on the .NET Common Language Runtime (CLR) on Windows and Mono Runtime on Linux/macOS, and Py4J depends on having a Java Virtual Machine installed.

Install

msl-loadlib is available for installation via the Python Package Index

pip install msl-loadlib

Optional dependencies:

To set up your environment on Linux, please follow the instructions on the prerequisites section of the documentation.

Examples

If you are loading a 64-bit library in 64-bit Python (or a 32-bit library in 32-bit Python), then you can directly load the library using LoadLibrary.

The following examples load a 64-bit library in a 64-bit Python interpreter. If you are using a 32-bit Python interpreter replace 64 with 32 in the filename.

Import the LoadLibrary class and the directory where the example libraries are located

>>> from msl.loadlib import LoadLibrary
>>> from msl.examples.loadlib import EXAMPLES_DIR

If the file extension is not included then a default extension, .dll (Windows), .so (Linux) or .dylib (macOS), is used.

Load the example C++ library and call the add function

>>> cpp = LoadLibrary(EXAMPLES_DIR / "cpp_lib64")
>>> cpp.lib.add(1, 2)
3

Load the example FORTRAN library and call the factorial function

>>> fortran = LoadLibrary(EXAMPLES_DIR / "fortran_lib64")

With a FORTRAN library you must pass values by reference using ctypes, and, since the returned value is not of type c_int we must configure ctypes for a value of type c_double to be returned

>>> from ctypes import byref, c_int, c_double
>>> fortran.lib.factorial.restype = c_double
>>> fortran.lib.factorial(byref(c_int(37)))
1.3763753091226343e+43

Load the example Java byte code and call the cos function

>>> java = LoadLibrary(EXAMPLES_DIR / "Trig.class")
>>> java.lib.Trig.cos(1.234)
0.33046510807172985

Python interacts with the Java Virtual Machine via a local network socket and therefore the connection must be closed when you are done using the Java library

>>> java.gateway.shutdown()

Load the example .NET library and call the reverse_string function, we must specify that the library type is a .NET library by including the "net" argument

>>> net = LoadLibrary(EXAMPLES_DIR / "dotnet_lib64.dll", "net")
>>> net.lib.StringManipulation().reverse_string("abcdefghijklmnopqrstuvwxyz")
'zyxwvutsrqponmlkjihgfedcba'

To load a Component Object Model (COM) library pass in the library's Program ID. NOTE: This example will only work on Windows.

Here we load the FileSystemObject library and include the "com" argument to indicate that it is a COM library.

>>> com = LoadLibrary("Scripting.FileSystemObject", "com")

We then use the library to create, edit and close a text file

>>> f = com.lib.CreateTextFile("a_new_file.txt")
>>> f.WriteLine("This is a test")
0
>>> f.Close()
0

Inter-process communication is used to access a 32-bit library from a module that is running within a 64-bit Python interpreter. The procedure uses a client-server protocol where the client is a subclass of msl.loadlib.Client64 and the server is a subclass of msl.loadlib.Server32. See the examples for examples on how to implement Inter-process communication.

Documentation

The documentation for msl-loadlib can be found here.

About

Load a library (and access a 32-bit library from 64-bit Python)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •