⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maincpp.cpp

📁 C++结合Python脚本编程
💻 CPP
字号:
#ifdef   _DEBUG   
#define   D_E_B_U_G   
#undef   _DEBUG   
#include   <Python.h>                                                                      
#else   
#include   <Python.h>     
#endif   
#ifdef     D_E_B_U_G   
#define   _DEBUG   
#undef   D_E_B_U_G   
#endif   

#include <windows.h>
#include <Python.h>
#include <iostream.h>

//const strings declaration
LPCTSTR wScriptFileName = TEXT("test_1");
LPCTSTR wScriptFuncName = TEXT("GetMax");

//convert Unicode characters to ANSI characters
BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize);

int main(void)
{
	Py_Initialize();      //python init   

	//convert Unicode characters to ANSI characters
	char sText[20]= {0};
	WCharToMByte(wScriptFileName,sText,sizeof(sText)/sizeof(sText[0]));
	//import python script file
	cout << "Loading Script test_1.py ..." << endl;
    PyObject* pName = PyString_FromString(sText);
	PyObject* pModule = PyImport_Import( pName );

	//not use assert
	//assert( !pModule && "pModule Error!" );
	if (!pModule)
	{
		//printf("Could not open script.\n");
		cout << "Could not open script." << endl;
		return 0;
	}

	//get the dictionary of the script
	PyObject* pDict = PyModule_GetDict ( pModule );
	//get the function name in the script
	WCharToMByte(wScriptFuncName,sText,sizeof(sText)/sizeof(sText[0]));
	PyObject* pFunc = PyDict_GetItemString(pDict, sText);

	//sent parameter from c++ to python
	//create struct tuple to send param between functions
	PyObject* pParams = PyTuple_New(2);
	//add to params 16,32
	PyObject* pCurrParam;
	pCurrParam = PyInt_FromLong(16);
	PyTuple_SetItem(pParams, 0, pCurrParam); 
	pCurrParam = PyInt_FromLong(32);
	PyTuple_SetItem(pParams, 1, pCurrParam); 

	//
	PyObject* pMax = PyObject_CallObject(pFunc, pParams);
	int iMax = PyInt_AsLong(pMax);

	cout << "\tResult from call to GetMax (16,32):" << iMax << endl;

	Py_Finalize();     

	return 0;
}

  //-------------------------------------------------------------------------------------
  //Description:
  // This function maps a wide-character string to a new character string
  //
  //Parameters:
  // lpcwszStr: [in] Pointer to the character string to be converted 
  // lpszStr: [out] Pointer to a buffer that receives the translated string. 
  // dwSize: [in] Size of the buffer
  //
  //Return Values:
  // TRUE: Succeed
  // FALSE: Failed
  // 
  //Example:
  // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
  //---------------------------------------------------------------------------------------
  BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
  {
   DWORD dwMinSize;
   dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
   if(dwSize < dwMinSize)
   {
    return FALSE;
   }
   WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
   return TRUE;
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -