Load a 32-bit .NET library in 64-bit Python
This example shows how to access a 32-bit .NET library from 64-bit Python.
DotNet32
is the 32-bit server and
DotNet64
is the 64-bit client.
The source code of the C# program is available here.
Note
If you have issues running the example please make sure that you have the prerequisites installed for your operating system.
Tip
The JetBrains dotPeek program can be used to decompile a .NET assembly into
the equivalent source code. For example, peeking inside the
dotnet_lib32.dll library, that the
DotNet32
class is a wrapper around, gives
Attention
To configure pythonnet to use the .NET Core runtime, you must either run
from pythonnet import load
load("coreclr")
or define a PYTHONNET_RUNTIME=coreclr
environment variable
import os
os.environ["PYTHONNET_RUNTIME"] = "coreclr"
before super()
is called in the
Server32
subclass. To use the Mono runtime,
replace "coreclr"
with "mono"
.
Create a DotNet64
client to communicate
with the 32-bit dotnet_lib32.dll library
>>> from msl.examples.loadlib import DotNet64
>>> dn = DotNet64()
Get the names of the classes in the .NET library module, see
get_class_names()
>>> dn.get_class_names()
['StringManipulation', 'StaticClass', 'DotNetMSL.BasicMath', 'DotNetMSL.ArrayManipulation']
Add two integers, see add_integers()
>>> dn.add_integers(8, 2)
10
Divide two C# floating-point numbers, see
divide_floats()
>>> dn.divide_floats(3., 2.)
1.5
Multiple two C# double-precision numbers, see
multiply_doubles()
>>> dn.multiply_doubles(872.24, 525.525)
458383.926
Add or subtract two C# double-precision numbers, see
add_or_subtract()
>>> dn.add_or_subtract(99., 9., True)
108.0
>>> dn.add_or_subtract(99., 9., False)
90.0
Multiply a 1D array by a number, see
scalar_multiply()
>>> a = [float(val) for val in range(10)]
>>> a
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
>>> dn.scalar_multiply(2.0, a)
[0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0]
Multiply two matrices, see
multiply_matrices()
>>> m1 = [[1., 2., 3.], [4., 5., 6.]]
>>> m2 = [[1., 2.], [3., 4.], [5., 6.]]
>>> dn.multiply_matrices(m1, m2)
[[22.0, 28.0], [49.0, 64.0]]
Reverse a string, see
reverse_string()
>>> dn.reverse_string('New Zealand')
'dnalaeZ weN'
Call the static methods in the StaticClass
class
>>> dn.add_multiple(1, 2, 3, 4, 5)
15
>>> dn.concatenate('the ', 'experiment ', 'worked ', False, 'temporarily')
'the experiment worked '
>>> dn.concatenate('the ', 'experiment ', 'worked ', True, 'temporarily')
'the experiment worked temporarily'
Shutdown the 32-bit server when you are done communicating with the 32-bit library
>>> stdout, stderr = dn.shutdown_server32()