c++ and python

06/28/2013 15:08 3t3r4n#1
I have a dll made in c++ (gnu gcc compiler - codeblocks) injected in my metin2 pserver client and i need to call a function from game.py (or to execute a python code) but i don't know how to do it ...
I need an example ... (I have instaled python2.2 and I can use function from python.h but how?)
06/28/2013 15:47 Schlüsselbein#2
[Only registered and activated users can see links. Click Here To Register...]
06/28/2013 16:22 3t3r4n#3
I have tried this
Code:
PyObject* args = PyTuple_New(0);
PyObject_Call(PyObject_GetAttrString(PyImport_ImportModule("app"), "Exit"), args, NULL);
Py_XDECREF(args);
but nothing happened
06/28/2013 16:59 Zwawo#4
Maybe this helps you: [Only registered and activated users can see links. Click Here To Register...]
06/28/2013 17:26 3t3r4n#5
Code:
    PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
    Py_Initialize();
    pName = PyString_FromString("testfile");
    pModule = PyImport_Import(pName);
    pDict = PyModule_GetDict(pModule);
    pFunc = PyDict_GetItemString(pDict, "functocall");
    if (PyCallable_Check(pFunc))
    {
        PyObject_CallObject(pFunc, NULL);
    } else
    {
        ShowMessageFunc("ERROR");
    }
    Py_DECREF(pModule);
    Py_DECREF(pName);
    Py_Finalize();
testfile - i have testfile.py in root.epk
Code:
import uiCommon

def functocall():
	alertbox=uiCommon.PopupDialog()
	alertbox.SetText("message")
	alertbox.Open()
After Py_Finalize() i got a crash ...
06/28/2013 18:25 Zwawo#6
Delete "Py_Finalize()".
06/28/2013 18:31 3t3r4n#7
Quote:
Originally Posted by Zwawo View Post
Delete "Py_Finalize()".
If I delete Py_Finalize() nothing happens ...
06/28/2013 18:32 Zwawo#8
Just as example(this works):
Code:
string GetName(void)
{
	PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
	string name;
	Py_Initialize();
	pName =  PyString_FromString("player");

    pModule = PyImport_Import(pName);

    pDict = PyModule_GetDict(pModule);

	pFunc = PyDict_GetItemString(pDict, "GetName");
	if (PyCallable_Check(pFunc)) 
    {
       pValue = PyObject_CallObject(pFunc, NULL);
	   name = PyString_AsString(pValue);
	   if(name.empty())
	   {
		   name = "ERROR";
	   }

    } else 
    {
		name = "ERROR";
    }

	Py_DECREF(pModule);
    Py_DECREF(pName);
	Py_DECREF(pValue);


	return name;
}
06/28/2013 18:42 3t3r4n#9
"player" is a module but my file (i compiled it and i have put it in lib folder) i don't think it is a module ....

Do you have a source of a python module loader?
06/28/2013 18:51 Zwawo#10
Quote:
Originally Posted by 3t3r4n View Post
Do you have a source of a python module loader?
No, sry. I'm not very familiar with python.