Configure the project

The examples were made and tested by using Visual Studio Tools for python_small ‘’Python 3.6’’. The usage for Python 2.x versions requires maybe modifications. An already defined project can be found by following:

C:\<sofistik_installation>\2022\SOFiSTiK 2022\interfaces\examples\python\connect_to_cdb

Set up DLL functions

Bellow you can find the description how to define the DLL functions.
All functions are described in the database_help_small ‘’CDBase Help’’ file.

Important

In case you are using Python 3.8 or higher then see following changes: https://docs.python.org/3/whatsnew/3.8.html#ctypes

‘’On Windows, CDLL and subclasses now accept a winmode parameter to specify flags for the underlying LoadLibraryEx call. The default flags are set to only load DLL dependencies from trusted locations, including the path where the DLL is stored (if a full or partial path is used to load the initial DLL) and paths added by ``add_dll_directory()``. (Contributed by Steve Dower in bpo-36085.)’’

See an example how to use add_dll_directory() in Connect to CDB. It is not required to set the DLL path to the %PATH% environment variable since add_dll_directory() is provided.

To use the functions from the DLL file, ctypes function library is used. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

Some code samples reference the ctypes c_int type. This type is an alias for the c_long type on 32-bit systems. So, you should not be confused if c_long is printed if you would expect c_int, they are actually the same type.

ctypes library for DLL functions
from ctypes import
For more details please visit the official Python Website:

Here is a small example how to use the sof_cdb_get() function:

Use DLL functions
# Get the DLL functions
myDLL = cdll.LoadLibrary("sof_cdb_w-2022.dll")
py_sof_cdb_get = cdll.LoadLibrary("sof_cdb_w-2022.dll").sof_cdb_get
py_sof_cdb_get.restype = c_int

Hint

In this example the 64-bit DLL is used.

Set up the Environment Variable [PATH]

Important

In case you are using a python version 3.8 or later, then it is not required to use the environment variable PATH.

Use os.add_dll_directory() instead.

For more details see https://docs.python.org/3/whatsnew/3.8.html#ctypes.

Python 3.7 or a previous version

It is also necessary to define the environment path:

# Set environment variable for the dll files
print ("Hint: 64bit DLLs are used")
path = os.environ["Path"]

# 64bit DLLs
dllPath = r"C:\<sofistik_installation>\2022\SOFiSTiK 2022\interfaces\64bit"
dllPath += ";"

# other necessary DLLs
dllPath += r"C:\<sofistik_installation>\2022\SOFiSTiK 2022"
os.environ["Path"] = dllPath + ";" + path

To check the Python platform architecture, use this code example:

# Check the python platform (32bit or 64bit)
print ("Python architecture=", platform.architecture())
sofPlatform = str(platform.architecture())

The environment path must be defined before DLL functions are called. In this example environment path is set to:

C:\<sofistik_installation>\2022\SOFiSTiK 2022\interfaces\64bit
C:\<sofistik_installation>\2022\SOFiSTiK 2022

Python 3.8 or a later version

Since version 3.8 ctypes provides a new function for loading the DLLs, for more details see https://docs.python.org/3/whatsnew/3.8.html#ctypes.

See example that shows how to call the function (for 64-bit):

# Set environment variable for the dll files
print ("Hint: 64bit DLLs are used")

os.add_dll_directory(r"C:\sofistik_installation\2022\SOFiSTiK 2022\interfaces\64bit")
os.add_dll_directory(r"C:\sofistik_installation\2022\SOFiSTiK 2022")