views:

552

answers:

1

Forgive me if my terminology is a little off. My knowledge of managed C++/CLI is very limited.

I have an MFC application that uses a dll with the /clr option enabled. This dll uses a couple of C# dlls to communicate with a server using WCF. For the most part this works fine.

In one of the C# dlls, I've added an extension method to the System.Net.IPAddress class that will retrieve the subnet mask for the IPAddress object (using the UnicastIPAddressInformation class and its IPv4Mask). The extension method works great on the C# side, but I cannot figure out how to use it in the managed C++/CLI code.

First, is this even possible? If so, what does the syntax look like on the managed C++/CLI side? Do I have to be using the /clr:pure option for this to work?

Here's an example of the extension method:

using System.Net;
using System.Net.NetworkInformation;
public static class IPAddressExtensions
{
    public static IPAddress GetSubnetMask(this IPAddress address)
    {
        UnicastIPAddressInformation addressInfo = address.GetAddressInformation(); // elided
        return ((addressInfo != null) ? addressInfo.IPv4Mask : null);
    }
}

In my managed C++ code, how would I use this extension method, if it's even possible?

unsigned long bytes= 0x010000FF; // example address - 127.0.0.1
IPAddress^ address = gcnew IPAddress(BitConverter::GetBytes(bytes));
IPAddress^ subnet = address->GetSubnetMask(); // how do I do this???
+4  A: 

You have to just call it like a static method:

IPAddressExtensions::GetSubnetMask(address);

The "extension" method is more of a compiler trick than a difference in the CLR.

Reed Copsey
@Reed Copsey: First, thanks! Second, is there a good online tutorial for writing managed C++ code? The syntax escapes me right now, but I need to get good at it in a hurry. Thanks again.
Matt Davis
I don't know of a good online tutorial but for those still interested, I recommend "C++/CLI In Action" by Nishant Sivakumar published by Manning.
jschroedl