Chapter 8: Calling C/C++ Functions in Add-ins


In the initial chapter, we introduced the Add-ins development approach for MicroStation. Add-ins provide a modern programming framework based on C#, while NativeCode development uses C/C++. Although the two approaches share many concepts, they are built on different languages and technologies. Because of this, not every NativeCode feature is available directly through the Add-ins API.

 

As you develop Add-ins, you may occasionally need access to functionality that exists only in the native C/C++ APIs. In these situations, your Add-in must call C/C++ code directly to fill the gap.

 

There are two common ways to do this:

 

This chapter introduces the first approach, PInvoke, and shows how C# Add-ins can call native C/C++ functions. The next chapter explores C++/CLI and its role in mixed-language development.

 

 

Levels are used in MicroStation to organize and manage elements. By placing elements on different levels, you can group related objects and control their display more easily.

 

Both Add-ins and NativeCode provide APIs for retrieving level names from a DGN file. However, you may notice that the Add-ins API does not always return every level. In contrast, the NativeCode API can retrieve the complete list.

 

This happens because levels can come from two different sources:

 

The Add-ins API can only access levels stored in the current DGN file, while the NativeCode API can access levels from both sources. As a result, some level names may be missing when you use the Add-ins API alone.

 

 

In this chapter, we will use PInvoke to call a function from a native C/C++ DLL and retrieve all available level names, including those loaded from DGNLib files.

 

Since this article focuses on Add-ins development, we will not cover the NativeCode implementation in detail. Instead, the required native source code and precompiled DLL are provided for you to use. 

 

On the NativeCode side, we export two functions:

GetDGNlibLevelNames gets all the level names.

ReleaseDGNlibLevelNames releases the memory for dynamic requests.

 

We will encapsulate these two functions in Addins. Please download the SampleNative.dll from the following link and copy it to the folder

..\Bentley\MicroStation 2026\MicroStation\Mdlapps\

 

SampleNative_DLL

 

 

Let's build this feature step by step.

 

1. Update commands.xml

 

Open the commands.xml file, add the command csAddins8 DemoForm LevelChanged, and specify its processing function as csAddins8.DemoForm.LevelChanged.

 

 

2. Allow Unsafe Code

 

Right-click csAddins8 in the Solution Explorer and select Properties.

Under Build turn on Allow Unsafe Code

 

 

3. Update DeomoForm.cs

Open the DemoForm.cs file and add the following using statement to the beginning of the code.

using System.Runtime.InteropServices; 
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;

 

Go to the end of the file, add the command handler function ShowLevelNames, and declare the DllImport property required to call external functions.

    unsafe public static void ShowLevelNames(string unparsed)
        {
            List<string> namesList = new List<string>();
            LevelHandleCollection lvlHanCol = Session.Instance.GetActiveDgnFile().GetLevelCache().GetHandles();
            foreach (LevelHandle lvlHan in lvlHanCol)
            {
                namesList.Add(lvlHan.Name);
            }
            int namesCnt = 0;
            void** namesvpp = GetDgnlibLevelNames(ref namesCnt);
            IntPtr ptr = new IntPtr(namesvpp);
            for (int i = 0; i < namesCnt; i++)
            {
                IntPtr ptr1 = new IntPtr(ptr.ToInt64() + 8 * i);
                string lvlName = Marshal.PtrToStringUni(new IntPtr(*(void**)ptr1.ToPointer()));
                namesList.Add(lvlName);

            }
            ReleaseDgnlibLevelNames(namesvpp, namesCnt);
            foreach(string lvlName in namesList)
            {
                MessageCenter.Instance.ShowInfoMessage(lvlName, lvlName, false);
            }
        }

        [DllImport("SampleNative.dll")]
        public static unsafe extern void** GetDgnlibLevelNames(ref int namesCnt);


        [DllImport("SampleNative.dll")]
        public static unsafe extern void ReleaseDgnlibLevelNames(void** namesPP, int namesCnt);

 

 

4. Build Solution

 

Under menus, select Build > Build Solution to compile the solution.

 

 

 

The examples in this chapter use functions with very simple parameters and return values, making it easy to focus on the basics of PInvoke. In real-world applications, however, native functions often work with more complex data types, such as C++ classes and structures.

 

When calling these functions from a C# Add-in, you must create corresponding C# types that match the memory layout of the C++ classes or structures. This allows data to be passed correctly between managed and native code. Once the matching types are defined, you can use the DllImport attribute to call the native function just as you did in the examples shown in this chapter.

 

Covering C++ to C# type mapping is outside the scope of this article. Fortunately, there are many excellent resources available online that explain structure marshal, memory layout, and interoperability between C# and C++. Readers who want to explore more advanced PInvoke scenarios are encouraged to refer to those resources.

 

You can download the source code for this wiki here.

 

Prev: Responding to MicroStation Events Next: Writing Add-ins in C++/CLI