I want to simply pass the value of a variable to the c++ application.
I am trying this code:
/* Import the Python script */
pModule = PyImport_ImportModule("test1");
if (pModule != nullptr) {
res = PyModule_AddStringConstant(pModule, "__file__", "");
if (res == 0) {
pLocal = PyModule_GetDict(pModule);
if (pLocal != nullptr) {
/* Define my function in the newly created module */
pValue = PyRun_String(
"def executed(dun):\n\treturn dun\n",
Py_file_input,
pGlobal,
pLocal
);
/* pValue would be null if the Python syntax is wrong, for example */
if (pValue == NULL) {
if (PyErr_Occurred()) {
PyErr_Print();
}
}
else {
/* Get a pointer to the function just defined
Retrieve an attribute named attr_name from object o.
Returns the attribute value on success, or NULL on failure */
pFunc = PyObject_GetAttrString(pModule, "executed");
/* Double check we have actually found it and it is callable
Return 1 if the object is callable and 0 otherwise */
if (!pFunc || !PyCallable_Check(pFunc)) {
if (PyErr_Occurred()) {
PyErr_Print();
}
return 2;
}
/* Build a tuple to hold arguments
Return a new tuple object of size len, or NULL */
pArgs = PyTuple_New(1);
if (pArgs != nullptr) {
int val = 0;
/* Parse the input argument as an integer */
if (!PyArg_ParseTuple(pArgs, "i", &val)) {
return NULL;
}
/* Return Py_True or Py_False, depending on the value */
pValue = PyBool_FromLong(val);
if (pValue != nullptr) {
tup = PyTuple_SetItem(pArgs, 0, pValue);
if (tup == 0) {
// success
}
else {
// failed
}
/* Call function */
pValue = PyObject_CallObject(pFunc, pArgs);
}
}
}
}
}
}
However, PyArg_ParseTuple does not succeed.
Am I going about this correctly?
My python code looks like this:
dun = False
pix = PIL.ImageGrab.grab()
x, y = pyautogui.position()
r, g, b = pix.getpixel((x, y))
dun = True
I want to get the boolean value.