C++ to communicate to Python function
<p>I am new to c++, </p>

<p>I created DLL which contains Class, and functions and all the return type of each function is PyObject (Python Object), So now i want to write the C++ application which loads DLL dynamically using LoadLibrary function.</p>

<p>Able to execute with adding the project to same solution and adding the reference to DLL.</p>

<p>I am able to load the DLL, but when i am calling the function it returns PyObject data type, how to store the return type of PyObject in C++?</p>

<p>You should take a look at Python's documentation on <a href="https://docs.python.org/3.5/c-api/concrete.html" rel="nofollow">Concrete Objects Layer</a>. Basically you have to convert <code>PyObject</code> into a C++ type using a function of the form <code>Py*T*_As*T*(PyObject* obj)</code> where <code>T</code> is the concrete type you want to retrieve.</p>

<p>The API assumes you know which function you should call. But, as stated in the <a href="https://docs.python.org/3.5/c-api/concrete.html" rel="nofollow">doc</a>, you can check the type before use:</p>

<blockquote>
  <p>...if you receive an object from a Python program and you are not sure that it has the right type, you must perform a type check first; for example, to check that an object is a dictionary, use <a href="https://docs.python.org/3.5/c-api/dict.html#c.PyDict_Check" rel="nofollow"><code>PyDict_Check()</code></a>.</p>
</blockquote>

<p>Here is an example to convert a <code>PyObject</code> into <code>long</code>:</p>

<pre><code>PyObject* some_py_object = /* ... */;

long as_long(
    PyLong_AsLong(some_py_object)
);

Py_DECREF(some_py_object);
</code></pre>

<p>Here is another, more complicated example converting a Python <a href="https://docs.python.org/3.5/c-api/list.html" rel="nofollow"><code>list</code></a> into a <code>std::vector</code>:</p>

<pre><code>PyObject* some_py_list = /* ... */;

// assuming the list contains long

std::vector&lt;long&gt; as_vector(PyList_Size(some_py_list));

for(size_t i = 0; i &lt; as_vector.size(); ++i)
{
    PyObject* item = PyList_GetItem(some_py_list, i);

    as_vector[i] = PyLong_AsLong(item);

    Py_DECREF(item);
}

Py_DECREF(some_py_list);
</code></pre>

<p>A last, more complicated example, to parse a Python <a href="https://docs.python.org/3.5/c-api/dict.html" rel="nofollow"><code>dict</code></a> into a <code>std::map</code>:</p>

<pre><code>PyObject* some_py_dict = /* ... */;

// assuming the dict uses long as keys, and contains string as values

std::map&lt;long, std::string&gt; as_map;

// first get the keys
PyObject* keys = PyDict_Keys(some_py_dict);

size_t key_count = PyList_Size(keys);

// loop on the keys and get the values
for(size_t i = 0; i &lt; key_count; ++i)
{
    PyObject* key = PyList_GetItem(keys, i);
    PyObject* item = PyDict_GetItem(some_py_dict, key);

    // add to the map
    as_map.emplace(PyLong_AsLong(key), PyString_AsString(item));

    Py_DECREF(key);
    Py_DECREF(item);
}

Py_DECREF(keys);
Py_DECREF(some_py_dict);
</code></pre>

python c++ dll