Can I create c++ extensions via DLLs?

Operating system: window 10
Slicer version: 5.2.1

Hi, slicer users

I’m trying to create a C++ dll(dynamic-link library) to use a function implemented in C++ in Python.

Can this DLL be applied to 3d slicer?

So, wouldn’t it be possible to extend C++ modules the same way Python extends?

If this is not possible you will have to learn some more advanced ways of handling Cmake.

C++ components of 3D Slicer Qt Loadable Modules (Module Overview — 3D Slicer documentation) are wrapped in python and exposed automatically (mostly). These wrapped python components will be available in Slicer and can be used by other modules.

I would recommend that you try to integrate this C++ code as part of a Loadable Module. It is also possible to have a sort of hybrid module, where Scripted module (Module Overview — 3D Slicer documentation) can include some C++ components (an example of this approach is one of the modules of Slicer-Liver: Slicer-Liver/LiverSegments at develop · ALive-research/Slicer-Liver · GitHub)

1 Like

You can also integrate the C++ code into Slicer via CLI module. It is simpler to create and maintain CLI modules, as they consists of only a single file and don’t contain any Slicer specific code. But perhaps more importantly, the code is executed in a separate process, so any errors in the code cannot make the application crash. Execution can also run in the background, keeping the application responsive during lengthy computations. These are all very important advantages. The only drawback is that you need to pass input and output data between Slicer and this process, which may be too slow for interactive use (when you need updates at dozens of times per second).

1 Like

If you need faster communication between Slicer and custom C++ code than you can get with a CLI module you can also consider using the WebServer or OpenIGTLink networking options. CLI modules are started as a new process each time they run, while the networking options all back-and-forth communication for a running process.

1 Like

You could also consider using C++ in Python techniques with cython. My group has a Slicer Scripted CLI (python) that contains a custom cythonized python module to speed up a specific calculation in a processing algorithm.

Yes, there’s cython and also cppyy that I find interesting, but if they goal is to provide a maintainable Slicer extension or even custom app then putting the C++ code in either a VTK or ITK class will allow you to use all the existing cross-platform deployment infrastructure without a second thought.

Thanks everyone for your replies. I’ll try it as soon as the preceding work is finished. Thank you!