Hi, i want to learn how to use a simple python script from c++ but the documentation is a little bit lacking imo. So far i looked here and here, but it's not very clear how it work. For example, do i use PyRun_SimpleString() or PyRun_String()? What's the difference between the two?
What im trying to do is, i have this program that can rename files using lua scripts, and it work perfectly, and i now want to add python as another option to the mix. The job of the script is simple, call a function called convert() that take a given filename as a string and return a modified filename. That way i can rename a batch of files based on a simple script. In lua i had to use globals since trying to push a string before the function call caused problem randomly so if i could do the same in python that would be great.
That's the code im trying to make work (work in progress from my lua code, i want to replace the remaining lua code for python code)
#include "PythonPlugin.h"
#include <string>
string LanguageName = "";
// Those are defined in the script, i need a way to access them
string ScriptName = "";
string InitialDirectory = "";
/**************************************************
main procedure.....................................
**************************************************/
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD fdwreason, LPVOID lpReserved)
{
switch(fdwreason)
{
case DLL_PROCESS_ATTACH: InitializePython(); break;
case DLL_PROCESS_DETACH: ShutdownPython(); break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
void InitializePython()
{
Py_Initialize();
}
void ShutdownPython()
{
Py_Finalize();
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
char* EXP_FUNC _GetLanguageName()
{
LanguageName = "Python";
return (char*)LanguageName.c_str();
}
///////////////////////////////////////////////////////////////////////////////////////////////
bool EXP_FUNC _LoadScript(char *txt)
{
//return PyRun_SimpleString(txt) == ;
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////
char* EXP_FUNC _GetScriptName()
{
//ScriptName = LuaScript.GetGlobalString("ScriptName");
//return (char*)ScriptName.c_str();
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////
char* EXP_FUNC _GetInitialDirectory()
{
//InitialDirectory = LuaScript.GetGlobalString("InitialDirectory");
//return (char*)InitialDirectory.c_str();
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////
bool EXP_FUNC _Convert(char *in, char *out, char *err)
{
/*bool res = false;
ZeroMemory(out, MAX_PATH);
ZeroMemory(err, 1024);
LuaScript.SetGlobalString("Input", in);
if(LuaScript.SafeCallFunction("Convert", err)){
string Output = LuaScript.GetGlobalString("Output");
res = Output.length() > 0;
if(res){
memcpy(out, Output.c_str(), Output.length());
} else {
sprintf(err, "%s", "Error: Convert() returned null output.");
}
}
return res;*/
return false;
}