Python: accessing DLLs from Python

Some good explanation on how to implement basic C-type DLLs
can be found here: http://docs.python.org/library/ctypes.html
Ctypes should work OK with standard DLLs.

The story here is that if you want to access .NET DLL library - standard
c_types built into Python will not help you. C-types can only handle
ctypes :) type of DLLs. To use .NET-build DLLs - you will need to use:

Using IronPython

This one looked to me initially pretty fast and easy but sending
data to DLL functions and retreiving it back might cause a small headache
from time to time. For instance - you need to make some conversion to
"out" parameters this way: message = clr.Reference[str]())
before passing it to DLL-function for read-backs. More description about
this approach can be found here: http://ironpython.net/documentation/dotnet/
I had one bad experience - when .DLL used unsigned 64-bit variable
that it defined somewhere. Conversion between int and this 64-bit uint
didn't work as it is different data type. Probably there must be an
easy solution but didn't really have time for it.

This is an example on conversions:

import clr
clr.AddReference("somedlllibrary.dll")
import somedlllibrary
 
clr_r = clr.Reference[float]()
somedlllibrary.function(clr_r)
clr_r.Value # reading back value passed from DLL function

Using CLR + PythonXY (for instance)

Second approach require a little different approach but I preferred it.
You have to install CLR library for Python, but it is generally pretty easy task.
I have noticed that sometimes when .NET DLL function requires object that is
modified and passed back as reference - you need to retain resulting object
from return value as shown below:

import clr
clr.AddReference("somedlllibrary.dll")
import somedlllibrary
 
obj = somedlllibrary.object_to_pass()
A = somedlllibrary.function(obj)
obj = A[1] # this gives you back properly object.

Conversion of types using CLR+ PythonXY

Most of the stuff is explained pretty well here:
http://pythonnet.sourceforge.net/readme.html

I found this post handy when dealing with arrays:
http://comments.gmane.org/gmane.comp.python.dotnet/588

Below is an example of handling Arrays of UInt16 (called also Short).
It is relatively straight-forward to handle that:

import clr
import System
from System.Collections import *
clr.AddReference("somedll")
import somedll
 
outarray = System.Array[System.UInt16](())
inarray = System.Array[System.UInt16](())
[retval, inarray]  = somedll.somefunction(0xF8000005, outarray, inarray)
for i in inarray:
  print i