11

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???
Matt Davis
  • 45,297
  • 16
  • 93
  • 124

1 Answers1

16

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
  • 554,122
  • 78
  • 1,158
  • 1,373
  • @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 Aug 28 '09 at 22:30
  • 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 Sep 29 '10 at 20:04
  • Here's an Amazon link to the aforementioned book: http://www.amazon.com/CLI-Action-Manning-Nishant-Sivakumar/dp/1932394818 – Per Lundberg May 09 '13 at 08:27
  • @MattDavis I got the same problem, but, I've clear my basic fundas of C++/CLI using this Ebook [Microsoft Visual C++/CLI Step by Step](http://it-ebooks.info/book/2527/). Hope! it'll help you. and Thanks **Per Lundberg** for suggesting a good EBook – Kasim Rangwala Feb 03 '15 at 06:44