title: Python ctypes cdll.LoadLibrary, instantiate an object, execute its method, private variable address truncated
body: <p>I wrote a dll library in c, compile with vs2017 64-bit, and try to load it with python3.6 64-bit. However the object's member variable's address got truncated to 32-bit. </p>
  
  <p>Here's my sim.c file, which is compiled to sim.dll:</p>
  
  <pre><code>class Detector {
  public:
      Detector();
      void process(int* pin, int* pout, int n);
  
  private:
      int member_var;
  };
  
  Detector::Detector()
  {
      memset(&amp;member_var, 0, sizeof(member_var));
      myfile.open("addr_debug.txt");
      myfile &lt;&lt; "member_var init address: " &lt;&lt; &amp;member_var &lt;&lt; endl;
  }
  void Detector::process(int* pin, int* pout, int n);
  {
      myfile &lt;&lt; "member_var process address: " &lt;&lt; &amp;member_var &lt;&lt; endl;
      myfile.close();
  }
  
  #define DllExport   __declspec( dllexport )  
  
  extern "C" {
      DllExport Detector* Detector_new() { return new Detector(); }
      DllExport void Detector_process(Detector* det, int* pin, int* pout, int n)
      {
          det-&gt;process(pin, pout, n);
      }
  }
  </code></pre>
  
  <p>Here's my python script:</p>
  
  <pre><code>from ctypes import cdll
  lib = cdll.LoadLibrary(r'sim.dll')
  
  class Detector(object):
      def __init__(self):
          self.obj = lib.Detector_new()
  
      def process(self,pin, pout, n):
          lib.Detector_process(self.obj,pin, pout, n)
  
  detector = Detector()
  
  n = 1024
  a = np.arange(n, dtype=np.uint32)
  b = np.zeros(n, dtype=np.int32)
  
  aptr = a.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
  bptr = b.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
  
  detector.process(aptr, bptr, n)
  </code></pre>
  
  <p>Here's the address of the member_var in addr_debug.txt:</p>
  
  <pre><code>member_var init address:    0000025259E123C4
  member_var process address: 0000000059E123C4
  </code></pre>
  
  <p>So accessing it trigger memory access error:</p>
  
  <pre><code>OSError: exception: access violation reading 0000000059E123C4
  </code></pre>
  
  <hr>
  
  <p>Some attempts I tried to understand the issue:</p>
  
  <ul>
  <li>Define member_var as public instead of private, not help, address still truncated.</li>
  <li>Define member_var as global variable, then the address is ok. So I guess the member_var address truncation happens either when returning the object to python, or passing the object back to dll. </li>
  </ul>