Jump to content

Calling a C++ program in C#

toobladink
Go to solution Solved by FlappyBoobs,

The basic way to call any program from C# is:

 

Process.Start(pathToProgram, arguments);

 

The C++ program would need to accept arguments in order for this to work, but that's how to do it.

 

You can improve the code with some call backs and waits if you want, look into the Process.Start method to see what it can do.

The title is pretty generalized, so let me elaborate.

 

There's a program that is written in C++ that I am working with. It takes a .csv file, performs specific types of calculations on that file based on user input, and then outputs several .csv files. Basically, imagine it like this.

 

User opens program (console)

Enters file name (code for this is in main())

Enters an integer based on calculations to be performed (code for this is in main())

Enters a prefix for the new file names (code for this is in main())

Calculations performed (several functions are called in main())

New files generated (a function called in main())

 

Now, why do I need to call this program in C#? Glad you asked.

 

I am trying to make an Excel Add-in. That way, they don't have to open the program, they can just enter in a cell =Calc(x,x,...) where x are parameters needed such as calculation type, prefix file name, and whatever else may be required, and the program gets run for them. I am restricted to using Excel DNA for a couple reasons - XLW is outdated (only up to Excel 2014), XLL+ is inaccessible for some reason, and the same goes for xll (by Keith). If there is another package I can use that can use C++, please let me know. Otherwise, I am stuck with this.

 

How can I call my C++ program in C#? The biggest problem I'm having is that any user input is in main, and with the way main is written, there are no arguments. Now, I can modify the C++ code, but that's the last thing I want to do if at all possible. 

 

Here's how I want it to look:

namespace AddIn
{
  public static class ExcelOutput
  {
    [ExcelFunction]
    public static int CalcName(string calcType, string prefix, string logDirectory)
    {
      //calls C++ program based on above parameters
      
      return 0;
    }
  }
}

This is setup so the user would enter =CalcName(1, test, \Users\User\Documents\DEV\logs)

 

However, I'm afraid I may have to ask the user to enter the file name too (which isn't a big deal, but I'd like to deal with one less parameter).

 

Even if this command launches the program rather than just calling it to do the calculations, that's fine too! I can imagine this will be easier and I could even just do it with VBA, rather than C# and try and deal with this serious headache.

 

The biggest thing is, I don't know where to start. I've been reading about managed and unmanaged code, but I am really stuck on how to implement something like this. I could really use a guiding hand to show me what I need to look at, and then see if I can find something myself.

 

Relevant links: 

xll (by Keith) - https://archive.codeplex.com/?p=xll (download is inaccessible)

Excel DNA - https://archive.codeplex.com/?p=exceldna

XLW - http://xlw.sourceforge.net/

hi

pipes
Undervolting 5700 (not mine but important)

 

Link to comment
Share on other sites

Link to post
Share on other sites

Any particular reason you can't just convert the C++ into C# code?

 

There's ready made libraries for C# that can read/parse/write CSV files, here's just the top google results:

 

https://www.filehelpers.net/

https://bartsimons.me/reading-csv-files-in-csharp-with-csvparser/

https://dotnetcoretutorials.com/2018/08/04/csv-parsing-in-net-core/

https://joshclose.github.io/CsvHelper/

https://coding.abel.nu/2012/06/built-in-net-csv-parser/

 

to answer your question you could convert your c++ code into a DLL and then call the DLL from your C# project

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, mariushm said:

Any particular reason you can't just convert the C++ into C# code?

It would be like almost two thousand lines - I'm not sure how to do that efficiently. And I believe C++ was chosen for a particular reason - because this is the only project I've seen where I am at that has something in C++ rather than C#.

hi

pipes
Undervolting 5700 (not mine but important)

 

Link to comment
Share on other sites

Link to post
Share on other sites

The basic way to call any program from C# is:

 

Process.Start(pathToProgram, arguments);

 

The C++ program would need to accept arguments in order for this to work, but that's how to do it.

 

You can improve the code with some call backs and waits if you want, look into the Process.Start method to see what it can do.

Link to comment
Share on other sites

Link to post
Share on other sites

With Microsoft's C# (not Mono C#) you can do what's called data marshaling.  I can promise you though at 2-5 thousand lines of code.  it will be easier to port your project to C#.  Unless the C++ code is being is an intrinsic way that is unavoidable it will be very painful for you to do data marshaling. Totally possible though.

Link to comment
Share on other sites

Link to post
Share on other sites

If you are stuck with C++, then I recommend writing (and exporting) C++ functions that does exactly what you want to do from C#, building the C++ application as a DLL instead, and then using Pinvoke.

 

Additionally, Data marshalling isn't as bad as phoenixflower makes it out to be, you just have to know what you're doing. You dont have to marshal, pinvoke or export anything you dont have to use, which is why I recommend simply writing and exporting the wrapper functions as mentioned above. Then you'd only have to marshal the parameters and return types from those functions, if they aren't builtin types.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×