Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/CLI global function accessibility issue

Tags:

c#

.net

c++-cli

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.

like image 928
Haix64 Avatar asked Apr 17 '12 06:04

Haix64


1 Answers

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() {}
};
like image 146
Hans Passant Avatar answered Oct 18 '22 06:10

Hans Passant