Search code examples
c#.netc++-cli

C++/CLI global function accessibility issue


How can I make a C++/CLI function visible, when the compiled DLL is imported in C#?

I can do it with classes simply by preceding their name with public, but its not the case with functions and I get syntax error when I do so.


Solution

  • You can't, the CLR does not support global functions. You can write them in C++/CLI but the compiler generates a special class to give them a home. The class name is <Module>, it is not accessible from C# code.

    You'll get the exact equivalent by declaring a public ref class with public static methods. No trouble accessing those. Same idea as a static class in C#, minus the checks. You can add the checks by declaring it abstract and sealed:

    public ref class Utils abstract sealed
    {
    public:
        void static foo() {}
    };