cTYPES manipulating Python and C + + dll-pass struct pointers

Source: Internet
Author: User

CMakeLists.txt

# project (project name) project (blog-3123958139-1) # add_library (link library name SHARED link library code) add_library (Dll_ shared Dll_.cpp)

Dll_.cpp

#include <iostream>using namespacestd;//C + + struct-Body definitionstructCpp_struck_ {//stock Code, String    Char*stock_name_; //date, array of strings    Char*stock_date_[]; //Open and low four set price, floating-point array    Doublestock_open_[]; Doublestock_high_[]; Doublestock_low_[]; Doublestock_close_[]; //volume, floating-point array    Doublestock_volume_[]; //length, integral type    intStock_len_; intMa_len_;};//declaring a function exported as a standard C formatextern "C" {//passing in struct pointers, outgoing struct-body pointersCpp_struck_ *dll_function_1 (Cpp_struck_ *py_struck_pointer_) {    /** Data Processing Section ... * * **/Cpp_struck_*Cpp_struck_pointer_; Cpp_struck_pointer_=Py_struck_pointer_; returncpp_struck_pointer_;}}

test_dll_.py

ImportTushare fromcTYPESImport*#Download test DataSh_data_frame_ = Tushare.get_hist_data ('SH') Stock_name_='SH'Stock_date_=Sh_data_frame_.index.valuesstock_open_= sh_data_frame_['Open'].valuesstock_high_= sh_data_frame_[' High'].valuesstock_low_= sh_data_frame_[' Low'].valuesstock_close_= sh_data_frame_['Close'].valuesstock_volume_= sh_data_frame_['Volume'].valuesstock_len_=Len (sh_data_frame_) Ma_len_= 2#print old data as a comparisonPrint("Old_date_ =", List (stock_date_))Print("Old_open_ =", List (stock_open_))#python struct-body definitionclasspy_struct_ (Structure): _fields_= [("stock_name_", c_wchar_p),#Note that the string format needs to be c_wchar_p instead of c_char_p("Stock_date_", C_wchar_p *stock_len_), ("Stock_open_", c_double *stock_len_), ("Stock_high_", c_double *stock_len_), ("Stock_low_", c_double *stock_len_), ("Stock_close_", c_double *stock_len_), ("stock_volume_", c_double *stock_len_), ("Stock_len_", C_int), ("Ma_len_", C_int)]#python struct instantiation, initializingPy_struct_1 =py_struct_ () py_struct_1.stock_name_=Stock_name_py_struct_1.stock_date_= (C_wchar_p * stock_len_) (*stock_date_) Py_struct_1.stock_open_= (C_double * stock_len_) (*Stock_open_)#Incoming Pointer instancePy_struct_1_pointer_ =ByRef (py_struct_1)#Get DLL HandleH_dll_ =Cdll ('c:\\users\\perelman\\. Clion2016.1\\system\\cmake\\generated\\blog-3123958139-1-6c04ac5e\\6c04ac5e\\debug\\libdll_.dll')#defines the DLL return value type as a python struct-body pointerH_dll_.dll_function_1.restype =POINTER (py_struct_)#returns the DLL struct-body pointerCpp_struct_pointer_ =h_dll_.dll_function_1 (py_struct_1_pointer_)#structure pointer fetching contentCpp_struct_contents_ =cpp_struct_pointer_.contents#save result as Python list formatNew_date_ = [] forValueinchCpp_struct_contents_.stock_date_: New_date_.append (value)Print("New_date_ =", New_date_)#save result as Python list formatNew_open_ = [] forValueinchCpp_struct_contents_.stock_open_: New_open_.append (value)Print("New_open_ =", New_open_)

cTYPES manipulating Python and C + + dll-pass struct pointers

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.