banner



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.

  1. Windows 7
  2. Python 3.3.5 (Assume the Python settings are already OK.)
  3. 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
  1. 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.
  2. Start Windows Console (cmd.exe), and then switch the current path to the directory where Python installed.
  3. 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

  1. Start Visual Studio 2012, and create a new Win32 Project, named OPython.
  2. PythonDLL 1.png

  3. Click OK, and select the Application type to be DLL, and click Finish to create the project.
  4. PythonDLL 2.png

  5. Set the Solution Configurations to be Release. Right click on the project, and choose Property from the context menu, to open the Property dialog.
  6. Note: Here the 32-bit DLL will be created, and that will be used in 32-bit version of Origin.

  7. 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.
  8. PythonDLL 3.png

  9. 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.
  10. PythonDLL 4.png

  11. Add a header file, named OPython.h, to the project, and put the following code to it.
  12. #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.              
    

0 Response to "How To Create Dll File In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel