Writing PHP Extension in C + +

Source: Internet
Author: User
Like high-level languages such as Python,javascript, PHP can also write extension functions in C + +. Here's how to build a simple PHP extension and how to call a third-party DLL library.

Reference Original: Making PHP Barcode Extension with Dynamsoft Barcode SDK

Building PHP Extensions with Visual Studio 2012

The Windows PHP installation package itself does not contain the header files, so to build the PHP extension, you must download the PHP source code. On Windows, to compile PHP, and to build PHP extensions, you must use the corresponding visual Studio, otherwise there will be a lot of errors. Here we use Visual Studio 2012 to build a PHP 5.6 extension. The steps are as follows:

Download the source code for PHP 5.6 and the VC11 build version.

Create an empty Win32 project, and apply the type selection DLL.

To add a header file path:

F:\php_pack\php-5.6.10-srcf:\php_pack\php-5.6.10-src\zendf:\php_pack\php-5.6.10-src\win32f:\php_pack\ Php-5.6.10-src\tsrmf:\php_pack\php-5.6.10-src\main

To add a library path:

F:\php_pack\php-5.6.10-Win32-VC11-x86\dev

Add Dependencies:

Php5ts.lib

Create php_dbr.h:

#pragma once #include "zend_config.w32.h"    #include "php.h"

Create php_dbr.cpp:

#include "Php_dbr.h" zend_function (decodebarcodefile); Zend_function_entry customextmodule_functions[] = {    Zend_fe (decodebarcodefile, null)    {null, NULL, NULL}}; Zend_module_entry customextmodule_module_entry = {    Standard_module_header,    "Dynamsoft Barcode Reader",    Customextmodule_functions, NULL, NULL, NULL, NULL, NULL,    No_version_yet, standard_module_properties}; Zend_get_module (Customextmodule) zend_function (decodebarcodefile) {     return_string ("No Barcode detected", true);}

To add a macro definition:

Zend_debug=0zts=1zend_win32php_win32

If you do not add it, many errors will occur.

The build project can now generate php_dbr.dll .

Use the Dynamsoft Barcode SDK to create PHP Barcode Extension

See how to call a third-party DLL library with PHP extensions:

Add Dynamsoft Barcode SDK header file and library file path to project properties

Decode the barcode from the SDK's C/C + + interface and convert the results to PHP readable data:

#include "php_dbr.h" #include "if_dbr.h" #include "BarcodeFormat.h" #include "BarcodeStructs.h" #include "ErrorCode.h" # ifdef _win64#pragma Comment (lib, "DBRx64.lib") #else #pragma comment (lib, "DBRx86.lib") #endif void SetOptions ( Preaderoptions poption, int option_imaxbarcodesnumperpage, int option_llbarcodeformat) {if (Option_llbarcodeformat ;    0) Poption->llbarcodeformat = Option_llbarcodeformat;     else Poption->llbarcodeformat = OneD;    if (Option_imaxbarcodesnumperpage > 0) poption->imaxbarcodesnumperpage = option_imaxbarcodesnumperpage; else poption->imaxbarcodesnumperpage = Int_max; } zend_function (Decodebarcodefile); Zend_function_entry customextmodule_functions[] = {Zend_fe (decodebarcodefile, null) {NULL, NULL, NULL}}; Zend_module_entry customextmodule_module_entry = {standard_module_header, "Dynamsoft Barcode Reader", CustomExtM Odule_functions, NULL, NULL, NULL, NULL, NULL, No_version_yet, Standard_module_properties};     Zend_get_module (Customextmodule) zend_function (decodebarcodefile) {array_init (return_value);    Get Barcode Image Path char* pfilename = NULL;     int ilen = 0; if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "s", &pfilename, &ilen) = = FAILURE) {return_string ("I    Nvalid parameters ", true);    }//Dynamsoft Barcode reader:init int option_imaxbarcodesnumperpage =-1;    int option_llbarcodeformat =-1;    Pbarcoderesultarray presults = NULL;     Readeroptions option;     SetOptions (&option, Option_imaxbarcodesnumperpage, Option_llbarcodeformat);        Decode barcode image File int ret = Dbr_decodefile (Pfilename, &option, &presults     );        if (ret = = DBR_OK) {int count = presults->ibarcodecount;        pbarcoderesult* ppbarcodes = presults->ppbarcodes;         Pbarcoderesult tmp = NULL; Loop all results for (int i = 0; i < count; i++) {TMP = Ppbarcodes[i];             Convert format type to string char format[64];              sprintf (format, "%d", Tmp->llformat);        (barcode type, result) add_assoc_string (return_value, format, tmp->pbarcodedata, 1);    }//Dynamsoft Barcode reader:release memory dbr_freebarcoderesults (&presults);    } else {return_string ("No Barcode detected", true); } }

Now we need to write a test script for PHP and deploy the DLL to PHP.

A simple PHP Barcode Reader:

 
  $value) {      print "format: $key, result: $value \ n";      print "*******************\n";    }  }  else {    print "$resultArray",  }} else {    echo "The file $filename does not exist";}?>

Open the PHP.ini initialization file and add:

[Dynamsoft Barcode Reader]extension=php_dbr.dll

Now copy the generated DLL to {PHP root directory}\ext. If you also copy the DynamsoftBarcodeReaderx86.dll to this directory, PHP will not find this DLL, the following error is reported:

How do I fix this problem? You just have to copy the third-party DLL to the PHP root directory. Now try again:

Source

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

  • 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.