Loading dynamic libraries using ctypes library in python














































Loading dynamic libraries using ctypes library in python



                         Loading Dynamic libraries

The dynamically linked library is a compiled binary that is linked at the runtime to the main process executable. On windows platform these libraries is called as dynamic link libraries(DLL), and on Linux they are called shared objects(SO).

There are three different ways to load dynamic libraries in ctypes: cdll(), windll() and oledll() The difference between all these three is in the way the functions inside those libraries are called and their resulting return values. The cdll() method is used for loading libraries that export functions using the standard cdecl calling convention. The windll() method loads libraries that export functions using stdcall calling convention. The oledll() method is similar to the windll() method, however the exported functions return a windows HRESULT error code which is used specifically for error messages returned from Microsoft component object model(COM) functions.



Accessing functions from Loaded dynamic libraries:-

As usual we will use linux for accessing all the functions of shared objects.

Open the terminal in your linux and use this synopsis:

nm [-D| --dynamic] [-fformat|--format=format]

for example:- nm -D /lib/x86_64-linux-gnu/libc.so.6

This command displays information about symbols in the specified File. It will give you a general Idea of the name of the functions in shared object which we can access.

For example:-



In above example the printf function of shared object file is used to print the provided input.


Note that we used b in the beginning of the printf() function. The "b" indicates a byte-string. printf() expects a string of (single, ASCII) bytes, not unicode points (which make up the default strings, i.e., non-"b" strings, in Python).


In C library function atoi() is used to convert the string argument to int type. This can be used using ctypes as shown below.


Output:-



Hope you understand.


Comments