Python embedded in C ++/C, etc.

Source: Internet
Author: User
Take python2.6 as an example;
 
1.PythonSource code, Open and compile python26.dll, python26.lib, python26_d.lib, and python26_d.dll. add them to the lib directory and put them in the System32 folder.
 
2. You can proceed with the followingCodeNow.
 
 
 
Additional point: Get the currentProgramRunning directory:

Tchar buffer [bufsiz];

DWORD dwret = getcurrentdirectory (bufsiz, buffer );

Widechartomultibyte (cp_acp, wc_compositecheck, (buffer),-1, CH, sizeof (CH), null, null );

 
// This is the same as the following three statements.
// Ah ''' Shen Na, tchar, cstring char * is still in the fog of the cloud. You have to check it again...
 
Cstring strpath;
 
Getcurrentdirectory (max_path, strpath. getbuffer (max_path ));
 
Widechartomultibyte (cp_acp, wc_compositecheck, (strpath. getbuffer (strpath. getlength () + 1 )),
 
-1, CH,Sizeof(CH), null, null); // convert wchar_t * To char *... you can refer to the previous log about Unicode and ASCI
 
Strpath. releasebuffer ();
 
IntLen = strlen (CH );
 
StringFilename ="\ Test. py";
 
For(IntI = 0; I <FILENAME. Length (); I ++)
 
Ch [Len ++] = filename [I];
 
Ch [Len] = '\ 0 ';
Locate the. py file path you are looking.
 
The initial path of the pyobject below is different from the default debug folder of ..
 
It is also self-seeking troubles ..
 
 
 
 
 
 
 
3. Two embedding methods of Python: one is to directly call an existing py file.
Pyobject * pyfile = pyfile_fromstring (CH ,"R");
If(Pyfile = NULL)
{Printf ("Exit 1"); System ("Pause");Return1 ;}
File * f = pyfile_asfile (pyfile );
If(F = NULL)
{Printf ("Exit 2"); System ("Pause");Return1 ;}
Pyrun_anyfileex(F ,"Test. py", 0 );
 
The other is to directly Insert the statement for execution.
 
String pythoncode ="Print ('Hello world, I am python! ')";
 
Pyrun_simplestring (pythoncode );
 
 
 
Code:
 
// Pythonplugintest. cpp:
//
 
# Pragma once # include"Targetver. h"# Include <stdio. h> # include <tchar. h> # DEFINE _ afxdll
# Include" Stdafx. h "# Include <stdio. h> # include < String > # Include <iostream> # include <fstream> # include <python. h> # include <cstring> # include <afx. h> Using   Namespace STD; Int _ Tmain ( Int Argc, _ tchar * argv []) {py_initialize (); // Start the python Interpreter     //////////////////////// Cstring strpath; getcurrentdirectory (max_path, strpath. getbuffer (max_path )); Char Ch [bufsiz]; widechartomultibyte (cp_acp, wc_compositecheck, (strpath. getbuffer (strpath. getlength () + 1),-1, CH, Sizeof (CH), null, null); strpath. releasebuffer (); String Tmpstr; cout <ch <Endl; Int Len = strlen (CH ); String Filename =" \ Test. py "; For ( Int I = 0; I <FILENAME. length (); I ++) CH [Len ++] = filename [I]; ch [Len] = '\ 0'; cout <ch <Endl; // directly call the existing Python py Program
 
Pyobject * pyfile = pyfile_fromstring (CH ,"R");If(Pyfile = NULL) {printf ("Exit 1"); System ("Pause");Return1;} file * f = pyfile_asfile (pyfile );If(F = NULL) {printf ("Exit 2"); System ("Pause");Return1;} pyrun_anyfileex (F ,"Test. py", 0 );// Run the command directly. The command is a string. Note that parentheses must be added to Python 3.1.1.
Pyrun_simplestring ("Print ('Hello world, I am python! ')");///////////////////////Py_finalize ();// Close the python Interpreter
 
System ("Pause");Return0 ;}

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.