How To Create Dll File In Python
1.19.1.3 Access Python via External DLL
In Origin C, there is no way to call Python functions directly. However, if you want to reuse your Python code in Origin C, it is recommended to wrap the Python functions in a DLL first, and then expose the functions in the DLL to Origin C code. In the following sections of this chapter, an example will be shown on how to do this step by step.
Notes: From Origin 2015, we can run python in Origin (support both command line and .py file), and use a PyOrigin module to access Origin from Python. view Python.chm for more details.
Running Environment
The example shown below is based on the following environment.
- Windows 7
- Python 3.3.5 (Assume the Python settings are already OK.)
- Visual Studio 2012
Python File
The Python functions defined in the following are going to be reused in Origin C.
def SayHello( ): print ( "Welcome to use Python in your Origin C programs!" ) #actually this string will not show in Origin C return 12345 def Add(a, b): return a + b def Sub(a, b): return a - b def Mult(a, b): return a * b def Div(a, b): return a / b def Mod(a, b): return a % b
- First of all, go to the Python installed directory, then create a Python file under this directory, say "myLib.py". Then open this newly created file using a text editor, Notepad for example, and put the Python code above to this file, and save.
- Start Windows Console (cmd.exe), and then switch the current path to the directory where Python installed.
- Run the following command to compile the Python file created just now.
-
python -m py_compile myLib.py
- If successfully, there will be a pyc file (myLib.cpython-33.pyc, or something similar with "myLib") under this folder <Python Installation Directory>/__pycache__/ by default.
-
Build DLL
- Start Visual Studio 2012, and create a new Win32 Project, named OPython.
- Click OK, and select the Application type to be DLL, and click Finish to create the project.
- Set the Solution Configurations to be Release. Right click on the project, and choose Property from the context menu, to open the Property dialog.
- In the Property dialog, from the left panel, activate Configuration Properties: VC++ Directories, and then in the right panel, add the Python header file path and library path to Include Directories and Library Directories respectively.
- From the left panel, activate Configuration Properties: Linker: Input, and then in the right panel, add the Python library to Additional Dependencies, here is python33.lib. Apply all the settings and click OK.
- Add a header file, named OPython.h, to the project, and put the following code to it.
Note: Here the 32-bit DLL will be created, and that will be used in 32-bit version of Origin.
#ifndef _OPYTHON_H_ #define _OPYTHON_H_ #ifdef _OPYTHON_CPP_ #define OP_API __declspec(dllexport) //use in VC #else #define OP_API //use in OC #pragma dll(OPython) //this line is important, when use in OC, Origin will find function body by searching in this DLL. Note:OPythonDLL, is the generated dll, without extension name.Subscribe to: Post Comments (Atom)
0 Response to "How To Create Dll File In Python"
Post a Comment