Use visual Studio to implement Python C + + extensions in a few steps, as well as DLL calls

Source: Internet
Author: User

Search the web for the Python extension tutorial, a lot of references to third-party open source libraries, and the official recommendation is to use setup.py. In fact, using Visual Studio is simple! Take a look at how you can write a python extension step-by-step, and how to call a DLL by extension.

Reference Original: Wrapping/C + + Methods of Dynamsoft Barcode SDK for Python

Visual Studio Environment Configuration

Create a WIN32 engineering dynamsoftbarcodereaderin Visual Studio. To open the project properties, add the header file and the library file path:

To add a dependent library python27.lib:

Change the extension to . PYD:

This will allow you to build the project. However, if you use Debug to build, an error will occur:

The reason is because the official Python installation package does not contain the debug library, if necessary, you can download the source code compiled. So switch the configuration to release and build the dynamsoftbarcodereader.pydsuccessfully:

DLL Invocation Example: encapsulating Dynamsoft Barcode SDK C + + interface

Importing Dynamsoftbarcodereaderinto a python script, Python searches for dynamsoftbarcodereader.pydand calls Initdynamsoftbarcodereader () is initialized.

In the project configuration, first add the Dynamsoft Barcode SDK header file and library path, and then use the following code to call Barcode decoding, and convert the results into Python data:

Static pyobject *decodefile (Pyobject *self, pyobject *args) {     char *pfilename;    int option_imaxbarcodesnumperpage = -1;     int option_llBarcodeFormat = -1;     if  (! Pyarg_parsetuple (args,  "s",  &pfilename))  {         Return null;    }     pbarcoderesultarray presults  = null;    readeroptions option;    setoptions (& Option, option_imaxbarcodesnumperpage, option_llbarcodeformat);      int  ret = dbr_decodefile (        pFileName,         &option,        &presults         );     if  (RET&NBSP;==&NBSP;DBR_OK) {         int count = pResults->iBarcodeCount;         pBarcodeResult* ppBarcodes = pResults->ppBarcodes;         pBarcodeResult tmp = NULL;          pyobject* list = pylist_new (count);         pyobject* result = null;         for  (int  i = 0; i < count; i++)         {             tmp = ppBarcodes[i];             result = pystring_fromstring (tmp- >pbarcodedata);   &Nbsp;          pylist_setitem (List, i, Py_ Buildvalue ("in",  (int) tmp->llformat, result));         }          // release memory         dbr_freebarcoderesults (&presults);         Return list;    }     return py_none;}  static PyMethodDef methods[] = {    {  "Initlicense",  initlicense, meth_varargs, null },    {  "DecodeFile",  decodeFile,  METH_VARARGS, NULL },    { NULL, NULL }};

In the project configuration to add DLL automatic copy, after compiling the DLL can be copied to the release directory:

Compile the project to generate Dynamsoftbarcodereader.pyd.

Create a new Python script in the release directory:

A simple Python Barcode reader code is as follows:

Import os.pathimport dynamsoftbarcodereader formats = {    0x1ffl  :  "OneD",    0x1l   :  "code_39",     0x2l :  "code_128",    0x4l   :  "CODE_93",     0x8L :  "Codabar",    0x10l   :  "ITF",     0x20L :  "ean_13",    0x40l   :  "Ean_8",     0x80L :  "Upc_a",    0x100l   :  "Upc_e",}  def initlicense (license):     dynamsoftbarcodereader.initlicense (license)   Def decodefile (fileName):     results = dynamsoftbarcodereader.decodefile ( FileName)     for result in results:         print  "Barcode format:  " + formats[result[0]]        print  "barcode value: "  + result[1] if __name__ ==  "__main__":     barcode_image = input ("enter the barcode file: ");     if not os.path.isfile (barcode_image):         print   "It is not a valid file."     else:        decodefile (Barcode_image);

Test with Barcode Pictures:

Source

Https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/samples/Python



Use visual Studio to implement Python C + + extensions in a few steps, as well as DLL calls

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.