Moti
Moti

Reputation: 65

class in managed c++ called from c#

I have a class decalred inside managed c++ dll as

public class IPHelper
{
public:
    static void CheckIP(LPWSTR pSocketName);
    static void DebugMessage(const wchar_t *str, ...);
private:        
    static DWORD GetIPInformation(PSOCKET_RECORD &pInfo);
};

I compiled it successfully and add it as reference to my c# project. I am able to use the namespace, however the class seems empty and I'm unable to call the functions inside it.

Any ideas?

Upvotes: 3

Views: 2019

Answers (4)

C.J.
C.J.

Reputation: 16111

That class seems to be a hybrid. You specified public class IPHelper, which is halfway what you want. You should specify it as public ref class IPHelper. However even so, it will still not interface well with managed classes, because of the parameter types it receives. For instance a wchar_t is not the same as a System::String^ (managed C++ way to declare strings). Similarly a LPWSTR is also not the same as a System::String^. As a side note, it would be best for you to write some utility methods to convert between .NET System::Strings and wchar_t and other native strings that you will most likely need. This is an excellent article on MSDN on how to convert between all the various string types.

Now I don't know if you want to expose this class directly to C#, or have this class in turn wrapped by a better managed wrapper than what you have here. But anyway you do it, the Managed C++ class's methods have to take .NET types in order to be directly used in C# code.

Upvotes: 1

Brandon Moretz
Brandon Moretz

Reputation: 7641

As others have noted, that's a native C++ class. Assuming your project is CLR enabled, then you can easily write a simple C++ / CLI wrapper around it, which will allow you to use it in a managed context.

This is a quick overview of C++ / CLI which should get you started in the right direction.

Upvotes: 1

Paul Sonier
Paul Sonier

Reputation: 39520

You're going to need to invoke it using the P/Invoke method. See this reference for more information.

Upvotes: 1

Puppy
Puppy

Reputation: 147036

That class is not managed, it is native. You need to call it a public ref class if you want to use it from managed code.

Upvotes: 3

Related Questions