Using ctypes with National Instruments nilibddc.dll

aewindhorn at aewindhorn at
Thu Mar 26 22:25:33 CET 2009

I would like to use Python ctypes with this "DiaDEM Connectivity
Library", but it only has examples for C and Matlab.  I have got a
couple of things to work by trial and error, but it is slow.  Has
anyone here used this library with Python?

Here's what I have so far:

# Module to handle *.tdm and *.tdms National Instruments TDM
# format files
from ctypes import *

#Define short names for required dll functions
DDCOpen = windll.nilibddc.DDC_OpenFile
DDCNumGroups = windll.nilibddc.DDC_GetNumChannelGroups
DDCReadGroups = windll.nilibddc.DDC_GetChannelGroups
DDCGroupStrPropLen =
DDCGroupProperty = windll.nilibddc.DDC_GetChannelGroupProperty

# File handles are pointers to 32 bit integer
czero = c_int(0)
FileHandle = pointer(czero)

# Retrieve a handle to the file
DDCErrCode = DDCOpen('Test_0919.tdms', 'TDMS', FileHandle)
print(DDCErrCode)  # Should be zero
myfile = FileHandle.contents  #The actual file handle

# Find number of groups
grpnum = c_int(0)
numptr = pointer(grpnum)
DDCErrCode = DDCNumGroups(myfile, numptr)
# if DDCErrCode == 0: print(numptr.contents, grpnum)

# Read the groups  [UNTESTED]
groups = c_int(0)*grpnum
grpptr = pointer(groups)  # Needs to be a pointer to memory to hold
                          #   of DDCChannelGrouphandle fields
DDCErrCode = DDCReadGroups(myfile, groups, grpnum)
if DDCErrCode == 0: print(groups)

# Length of group name  [UNTESTED]
namelen = c_int(0)
nlenptr = pointer(name)
DDCErrCode = DDCGroupStrPropLen(groups[0], 'name', namelen)
if DDCErrCode == 0: print(namelen)

#Make a buffer to hold the name, then retrieve it  [UNTESTED]
grpname = create_string_buffer(namelen+1)
nameptr = pointer(grpname)
DDCErrCode = DDCGroupProperty(groups[0], 'name', nameptr, namelen+1)
if DDCErrCode == 0: print(grpname.value)

# Read the channels in the group and get the names  [UNTESTED]

# Read the channel # of data values  [UNTESTED]

# Read the channel data values  [UNTESTED]

# Process the data values  [UNTESTED]

# Write the processed data to a file in a different format  [UNTESTED]

The "UNTESTED" sections are pseudocode of a sort.  I have figured out
how to get back (integer) file and object pointers, but I don't know
yet how to get a pointer to an array of strings, which is the next

I can supply a copy of the library if you want to try it, or it is
available from  Thanks in advance for any help.

Allen Windhorn, P.E. (MN)  (507) 345-2782
Kato Engineering
P.O. Box 8447, N. Mankato, MN  56002

More information about the Python-list mailing list