?? maincpp.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 + -