Calling a C# function from a C++ program.

Thad Thorpe
Thad Thorpe used Ask the Experts™
on
I would like to solicit your help please.
I would like to call a specific C# feature from within a C++ programming environment, such as:
            Microsoft MSVC 2017 enterprise edition.  The specific C# specific is:          
                              System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() method.

This is not a school project/assignment. From what I have looked at, there is a lot more programming required that I am not able to understand.  Could you help me out please.....  Thank you.
RSVP.   Thad.

This is what I have come up with...
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Xml;

 using System.Net.NetworkInformation;  /// they say this "networkinformation stuff is C# material.
 public static BOOL IsAvailable(int * xx) {
       bool IsAvail;
       bool IsAvail = NetworkInterface.GetIsNetworkAvailable();
       *xx = IsAvail;
       return (IsAvail);
 }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:

Author

Commented:
Is creating a C#, managed code, DLL quite easy. And then how do I call it from my c++ program.
I want to use "System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() .
I have never used C# for anything.  What would you recommend for a newbie.
Top Expert 2016

Commented:
create a class library that contains a function, use one of the methods after copying the dll to the folder that you c++ program is in.
I have no practical experience in this either as I use managed code only.
you can't just invoke a .net framework function directly from C++
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Ok,  Where do I start and what must a do. Are there C# DLL creation instructions on how to create a DLL and how I put my interface method.
And how I call from a C++ progm.   Help me get started. Ok.
Top Expert 2016

Commented:
have you tried using the links provided ?
AndyAinscowFreelance programmer / Consultant

Commented:
Before you start doing that are you using classical C++ for your app or C++.net.  If the second then you already have those functions you want to call available (because it is the .net enabled version of C++)
If you're doing anything beyond a very simple interaction, consider using a tool that is designed to solve this stuff for you - e.g. SWIG (www.swig.org).

E.g. See this tutorial page and scroll down to the C# example:
http://www.swig.org/tutorial.html

SWIG is designed to handle the generation of the interop code between C/C++ and a host of other languages, including C#.

I used it years ago and found it enormously helpful - in part because it walks you through all of this wiring nonsense.

Doug
Top Expert 2014

Commented:
Instead of a CCW, you can create a C++/CLI wrapper class: http://blogs.microsoft.co.il/sasha/2008/02/16/net-to-c-bridge/

Author

Commented:
I made a C## library/DLL using NetworkInterface.GetIsNetworkAvailable(), and it compiles and builds ok. But how do you tie it into my C++ program.
I want to pull out the network name and current name.   For quite a while today, I've been trying to put together NetworkInterface.GetName() method, but each time I check to see if GetName() is a member, it always says, it is not a member of the group I'm in.  How do you tell??? It's getting frustrating.
AndyAinscowFreelance programmer / Consultant

Commented:
A repeat of part of my earlier quesstion:
are you using classical C++ for your app or C++.net

Using a dll is the way to go for classical C++.  Adding one (small number) line of code in a C++.net app is a simple alternative, no dll required.

Author

Commented:
I am using MSVC 2017 as my programming platform/environment. Enterprise Edition.  I know nothing about any classical versions.
I have made a normal DLL file that has a bunch or "C" routines.  and what I've been told is that I have to make another C# DLL file with all my wishlist routines.  I've made C# routine( with .GetIsNetworkAvailable) and compiles ok. BUt I still have to know/learn how do you attach it to my normal DLL File - which it will be called from. I've made a VB front end to call my routines in the normal DLL file.  So I can test the DLL(s).
Thad.
AndyAinscowFreelance programmer / Consultant

Commented:
Do you have the following option available for a new C++ project in visual studio?
SNAG-0088.bmp
Does the following code compile in your app
String^ s = "Hello";


The first is creating a .net C++ app - C++ code that combines with .net classes.  The line of code is how one declares a .net String variable for example.
If you can do those then you should just be able to use the classes used in your C# code directly in your C++ code.

Author

Commented:

what is "CLR"? I tried to install everything else except games tools. I don't have CLR installed.  Does the Enterprise Edition have the "CLR" option??  I'm tempted to re-install all. But not now.  

AndyAinscowFreelance programmer / Consultant

Commented:
CLR - Common Language Runtime  (simply put .net).  It is an optional component when you install VS 2017.
As you don't have it then you are coding in classical C++.  Which means you need some sort of dll to act as a bridge between the .net C# and the non .net C++ components.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial