Graviton
Graviton

Reputation: 83296

Interop with another function in a different class

I would have to call C++ code from .Net code via interop.

I just wonder whether is there anyway to interop with another function in a different class? For example, in C++, I have the following utility class:

 class  ConvertUtility
{
    public:
        static void Convert(PointList &ptList, const list<pts> &pts);


};

I wish to call it directly from .Net via interop, any idea how to do this?

Note: here's a related question asking about how to use namespace to distinguish between different method. But this time, I want nothing to do with namespace, only a class with static function.

Edit: Given that there are already too many functions in the C wrapper ( e.g, static extern "C" function that are callable from .Net, without class or namespace), I won't want to introduce an extra layer of wrapping, if I can help it.

Upvotes: 1

Views: 119

Answers (1)

Anders Abel
Anders Abel

Reputation: 69280

In the related question you linked to, Ben Voigt says in a comment to the suggestion to write a C++/CLI wrapper:

This IS the correct answer. P/Invoke should only be used to call functions with a "C" interface, which means extern "C" to prevent name mangling, and also restrictions on parameter and return types.

Since the method is static, I see two options:

  1. Write a simple C wrapper function that can be called with P/Invoke.
  2. Write a C++/CLI wrapper that can be called directly from C#.

Upvotes: 2

Related Questions