title: Using SWIG wrapped functions from Windows .dll
body: <p>I'm having a problem using functions from a <code>.dll</code> shared library that is exported with SWIG interface.</p>
  <blockquote>
  <p>Version Info</p>
  <p>Python: 2.6.4</p>
  <p>Swig: 2.0.4</p>
  </blockquote>
  <p>The big picture is: I have some code developed under Linux using C++ and wrapped using SWIG. I compiled the C++ source into a <code>.so</code> object under Linux and use the <code>.so</code> library in Python.</p>
  <p>Now, I need to migrate all these functions to windows and the equivalent for <code>.so</code> in Windows is a <code>.dll</code>. So, I planned to compile all the C++ source codes into a <code>.dll</code> and access them through Python.</p>
  <p>So the normal procedure would be: have the C++ source -&gt; wrap them using SWIG -&gt; compile into <code>.dll</code> -&gt; access through Python.</p>
  <p>There is a huge <code>.cxx</code> source file generated using SWIG which contains all the functions I developed. The task now is to compile this SWIG generated  file into a <code>.dll</code> so that I can use all the functions later. However, the .cxx file uses some weird way of wrapping all my functions and I have no clue how to use them.</p>
  <p>The functions are wrapped as follows. Ssay I have a C++ class called <code>sdrts_reverse_burst_ff</code>, after wrapping, the class becomes a function in the <code>.cxx</code> file and is defined like this:</p>
  <pre><code>SWIGINTERN PyObject *_wrap_sdrts_reverse_burst_ff_sptr___deref__(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  
  PyObject *resultobj = 0;
  
  boost::shared_ptr&lt; sdrts_reverse_burst_ff &gt; *arg1 = (boost::shared_ptr&lt; sdrts_reverse_burst_ff &gt; *) 0 ;
  
    void *argp1 = 0 ;
  
    int res1 = 0 ;
  
    PyObject * obj0 = 0 ;
  
    sdrts_reverse_burst_ff *result = 0 ;
  
  
  
    if(!PyArg_UnpackTuple(args,(char *)&quot;sdrts_reverse_burst_ff_sptr___deref__&quot;,1,1,&amp;obj0)) SWIG_fail;
  
    res1 = SWIG_ConvertPtr(obj0, &amp;argp1,SWIGTYPE_p_boost__shared_ptrT_sdrts_reverse_burst_ff_t, 0 |  0 );
  
    if (!SWIG_IsOK(res1)) {
  
      SWIG_exception_fail(SWIG_ArgError(res1), &quot;in method '&quot; &quot;sdrts_reverse_burst_ff_sptr___deref__&quot; &quot;', argument &quot; &quot;1&quot;&quot; of type '&quot; &quot;boost::shared_ptr&lt; sdrts_reverse_burst_ff &gt; *&quot;&quot;'&quot;); 
  
    }
  
    arg1 = reinterpret_cast&lt; boost::shared_ptr&lt; sdrts_reverse_burst_ff &gt; * &gt;(argp1);
  
    result = (sdrts_reverse_burst_ff *)(arg1)-&gt;operator -&gt;();
  
    resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_sdrts_reverse_burst_ff, 0 |  0 );
  
    return resultobj;
  
  fail:
  
    return NULL;
  
  }
  </code></pre>
  <p>That's not all: near the end of this <code>.cxx</code> file there is a huge array which contains all the class functions like this:</p>
  <pre><code>PyMethodDef SwigMethods[] = {
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr___deref__&quot;, _wrap_sdrts_reverse_burst_ff_sptr___deref__, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr___deref__(sdrts_reverse_burst_ff_sptr self)&quot;},
  
    { (char *)&quot;delete_sdrts_reverse_burst_ff_sptr&quot;, _wrap_delete_sdrts_reverse_burst_ff_sptr, METH_VARARGS, (char *)&quot;delete_sdrts_reverse_burst_ff_sptr(sdrts_reverse_burst_ff_sptr self)&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_set_reverse_burst&quot;, _wrap_sdrts_reverse_burst_ff_sptr_set_reverse_burst, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_set_reverse_burst(sdrts_reverse_burst_ff_sptr self, int samples) -&gt; int&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_enable_reverse_burst&quot;, _wrap_sdrts_reverse_burst_ff_sptr_enable_reverse_burst, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_enable_reverse_burst(sdrts_reverse_burst_ff_sptr self) -&gt; int&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_reset&quot;, _wrap_sdrts_reverse_burst_ff_sptr_reset, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_reset(sdrts_reverse_burst_ff_sptr self) -&gt; int&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_history&quot;, _wrap_sdrts_reverse_burst_ff_sptr_history, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_history(sdrts_reverse_burst_ff_sptr self) -&gt; unsigned int&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_output_multiple&quot;, _wrap_sdrts_reverse_burst_ff_sptr_output_multiple, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_output_multiple(sdrts_reverse_burst_ff_sptr self) -&gt; int&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_relative_rate&quot;, _wrap_sdrts_reverse_burst_ff_sptr_relative_rate, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_relative_rate(sdrts_reverse_burst_ff_sptr self) -&gt; double&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_start&quot;, _wrap_sdrts_reverse_burst_ff_sptr_start, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_start(sdrts_reverse_burst_ff_sptr self) -&gt; bool&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_stop&quot;, _wrap_sdrts_reverse_burst_ff_sptr_stop, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_stop(sdrts_reverse_burst_ff_sptr self) -&gt; bool&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_nitems_read&quot;, _wrap_sdrts_reverse_burst_ff_sptr_nitems_read, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_nitems_read(sdrts_reverse_burst_ff_sptr self, unsigned int which_input) -&gt; uint64_t&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_nitems_written&quot;, _wrap_sdrts_reverse_burst_ff_sptr_nitems_written, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_nitems_written(sdrts_reverse_burst_ff_sptr self, unsigned int which_output) -&gt; uint64_t&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_detail&quot;, _wrap_sdrts_reverse_burst_ff_sptr_detail, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_detail(sdrts_reverse_burst_ff_sptr self) -&gt; gr_block_detail_sptr&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_set_detail&quot;, _wrap_sdrts_reverse_burst_ff_sptr_set_detail, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_set_detail(sdrts_reverse_burst_ff_sptr self, gr_block_detail_sptr detail)&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_name&quot;, _wrap_sdrts_reverse_burst_ff_sptr_name, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_name(sdrts_reverse_burst_ff_sptr self) -&gt; string&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_input_signature&quot;, _wrap_sdrts_reverse_burst_ff_sptr_input_signature, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_input_signature(sdrts_reverse_burst_ff_sptr self) -&gt; gr_io_signature_sptr&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_output_signature&quot;, _wrap_sdrts_reverse_burst_ff_sptr_output_signature, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_output_signature(sdrts_reverse_burst_ff_sptr self) -&gt; gr_io_signature_sptr&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_unique_id&quot;, _wrap_sdrts_reverse_burst_ff_sptr_unique_id, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_unique_id(sdrts_reverse_burst_ff_sptr self) -&gt; long&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_to_basic_block&quot;, _wrap_sdrts_reverse_burst_ff_sptr_to_basic_block, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_to_basic_block(sdrts_reverse_burst_ff_sptr self) -&gt; gr_basic_block_sptr&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_check_topology&quot;, _wrap_sdrts_reverse_burst_ff_sptr_check_topology, METH_VARARGS, (char *)&quot;sdrts_reverse_burst_ff_sptr_check_topology(sdrts_reverse_burst_ff_sptr self, int ninputs, int noutputs) -&gt; bool&quot;},
  
    { (char *)&quot;sdrts_reverse_burst_ff_sptr_swigregister&quot;, sdrts_reverse_burst_ff_sptr_swigregister, METH_VARARGS, NULL},
  
    { (char *)&quot;reverse_burst_ff&quot;, _wrap_reverse_burst_ff, METH_VARARGS, (char *)&quot;reverse_burst_ff(int max_samples) -&gt; sdrts_reverse_burst_ff_sptr&quot;},
  
    { NULL, NULL, 0, NULL }
  
  };
  </code></pre>
  <p>I followed my old experience with <code>.dll</code>s and exported all the functions with a simple <code>__declspec(dllexport)</code> head. In Python, I can also call the <code>_wrap_sdrts_reverse_burst_ff_sptr___deref__</code> I exported:</p>
  <pre><code>import ctypes
  
  _dll_func_list = ctypes.WinDLL(&quot;func.dll&quot;)
  
  _reverse_burst_ref = _dll_func_list._wrap_sdrts_reverse_burst_ff_sptr___deref__
  </code></pre>
  <p>Yet, that's the furthest I can approach. When I try to access the sub functions in that class, for example,</p>
  <pre><code>_class_ref._wrap_sdrts_reverse_burst_ff_sptr_set_reverse_burst(0)
  </code></pre>
  <p>the damned machine told me:</p>
  <blockquote>
  <p>'_wrap_sdrts_reverse_burst_ff_sptr_set_reverse_burst' cannot be found.</p>
  </blockquote>
  <p>When I try to call the function directly and pass in a parameter like this:</p>
  <pre><code>_dll_func_list ._wrap_sdrts_reverse_burst_ff_sptr_set_reverse_burst(0)
  </code></pre>
  <p>The machine says</p>
  <blockquote>
  <p>WindowsError: exception: access violation reading 0x00000004</p>
  </blockquote>
  <p>Does anyone know how to access exported SWIG wrapped functions using Python?</p>