How to pass string parameters to C++ function using ctypes, python
<p>I have a C++ DLL library. One function is shown as below.</p>

<p>The DLL import in Python is correct.</p>

<pre><code>int _ParseLogin2Resp(const std::string&amp; username,
                     const std::string&amp; password,
                     char szErr[1024]);
</code></pre>

<p>In fact, I have tried many ways to pass parameters. I know the difference between C++ <code>std::string</code> and <code>char *</code>.</p>

<p>1.Use the c_char_p() and byref()</p>

<pre><code>username = ctypes.c_char_p("username")
_ParseLogin2Resp(ctypes.byref(username),...)
</code></pre>

<p>2.Use create_string_buffer()</p>

<pre><code>username = ctypes.create_string_buffer("username")
_ParseLogin2Resp(username,...)
</code></pre>

<p>But function ERROR returns </p>

<blockquote>
  <p>The username or password is empty.</p>
</blockquote>

<p>So I couldn't find any solution to pass string parameters to my function.</p>

<p>My Question is.</p>

<p>Is it possibility that I could pass correct parameters to call my function as expected or I have to rewrite my dll library to make function work correctly?</p>

<p>Python doc says ctypes provides C compatible data types, it is better to use c style API interface in your DLL.</p>

python c++ dll ctypes