Call Native DLL within Python

May 8, 2015

Report Abuse
Sample Experiment to call native DLL from Python
Create a Windows DLL (almost as usual): • Make sure your build target is x64 o It seems AzureML is a 64 bit OS with 64bit python 2.7.7 (this is important since the two flavors of Python are somewhat different from each other). • Make sure you statically link the libraries you need or include them with your project. o This might be a bit annoying but it doesn’t seem like certain VC dlls are available on the azure ML machines. To do this: In the C++ Project Property page, under Configuration Properties -> C/C++ -> Code Generation Change the Runtime Library option to wither Multi-threaded or Multi-threaded Debug instead of the DLL versions. • I did a regular C/C++ DLL. No .NET but .NET should work except that I’m guessing there’s also no .NET on the AzureML machines. Create a Python wrapper: o This is easy enough just use ctypes.LoadLibrary but remember that the DLL is unzipped into ".\Scripts Bundle"! o Read more: #Sample DLL code from here: from ctypes import cdll mydll = cdll.LoadLibrary('.\\Script Bundle\\MathsFuncsDll.dll') def get_sum(x, y): return(mydll.sum(x, y)) Package it up: • No joke: just zip the dll and python wrapper into a zip file and upload it as a “Dataset” in AzureML. Finally use this in the Execute Python Script Module: import callDll #or whatever you called the python wrapper module. callDll.print_sum(3, 10)