Unable to load C++ dll in python
Question: I have a **C++** **dll** which I'm trying to use it in **Python** ,

    
    
    >>> from ctypes import *
    >>> mydll = cdll.LoadLibrary("C:\\TestDll.dll")
    

until now there are no errors, system seem to be doing what I wanted, but when
I try to access `mydll`, the Intellisence in the Python IDLE shows the
following,

![enter image description here](http://i.stack.imgur.com/RwUpm.png)

from the above pic, it's clear that the intellisence doesn't show up any
available functions of the dll, but when I checked the same TestDll.dll with
`dumpbin /EXPORTS TestDll.dll` it has 45 functions, but none of those
functions are available with python.

![enter image description here](http://i.stack.imgur.com/dmYbz.png)

**Please Note** : There are several questions on this topic, I tried the
following suggestions but no use,

  * [incompatible version of Python installed or the DLL](http://stackoverflow.com/a/13268880/1463551) [TestDll.dll & Python both are 32 bit versions]
  * [I think ctypes is the way to go](http://stackoverflow.com/a/252473/1463551) [Same Issue, cant see any functions, but loads the dll]

Now my question is how do I load all the available functions(as shown by
dumpbin)?

**Edit 1**

Based on [eryksun](http://stackoverflow.com/users/205580/eryksun) suggestion,
I was able to make some progress. The `TestDll.dll` comes along with a header
file `TestDll.h`(my bad I missed this file earlier), from which I could see
the available Exported Functions.

**`TestDll.h:`**

    
    
    _stdcall Function1 (const char* prtFileString, cont char* prtDescrip, struct FileParams* ptrParsms);
    
    struct FileParams
    {
      float val1;
      void* pMarker;
    };
    

now I've tried the following,

    
    
    >>> mydll = CDLL("c:\\TestDll.dll")
    >>> Function1 = mydll.Function1
    >>> Function1.restype = c_int
    

until now it's fine, but when I try to define the **argTypes** , not sure how
to do it for structs?

    
    
    >>> Function1.argtypes = (c_char_c, c_char_c, ???)
    

any suggestions are much appreciated.


Answer: Based on your header, here's a dummy C file (for Windows) that can be used to
test a `ctypes` wrapper. You mentioned C++ but the example was a C interface,
which is a good thing because `ctypes` works with C functions not C++.

### x.c

    
    
    #include <stdio.h>
    
    struct FileParams
    {
      float val1;
      void* pMarker;
    };
    
    __declspec(dllexport) _stdcall Function1 (const char* prtFileString, const char* prtDescrip, struct FileParams* ptrParsms)
    {
        printf("%s\n%s\n%f %p\n",prtFileString,prtDescrip,ptrParsms->val1,ptrParsms->pMarker);
    }
    

Here's the `ctypes` wrapper. Note that `ctypes` does not inspect the DLL and
automatically load all the functions. You have to declare the functions,
parameters and return values yourself. See [ctypes:structures and
unions](https://docs.python.org/3/library/ctypes.html#structures-and-unions)
for the `Structure` syntax.

Also note that for a DLL with `_stdcall` functions use `WinDLL` instead of
`CDLL`.

### x.py

    
    
    #!python3
    import ctypes
    
    class FileParams(ctypes.Structure):
        _fields_ = [('val1',ctypes.c_float),
                    ('pMarker',ctypes.c_void_p)]
    
        x = ctypes.WinDLL('x')
        x.Function1.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(FileParams)]
        x.Function1.restype = ctypes.c_int
    
        fp = FileParams(1.234,None)
        x.Function1(b'abc',b'def',ctypes.byref(fp))
    

And the output:

    
    
    abc
    def
    1.234000 0000000000000000
    

