开发者

Is it possible to make a call to a C# application from a C++ application?

开发者 https://www.devze.com 2022-12-18 12:11 出处:网络
I\'m a programming student, and I\'ve now had two classes in C#, this semester I\'m taking my first C++ class.

I'm a programming student, and I've now had two classes in C#, this semester I'm taking my first C++ class.

Out of curiosity, is it possible to call a C# application from a C++ application?

If so, is it also possible to check if the computer running the program has the .NET framework?

I'm just curious, and I think if its possible, it would be a great little program to write and have as a tool fo开发者_如何学JAVAr the future.

Just for your information, here's what I'd like to do:

  • Check to see if .NET framework is installed on the computer
  • If it's not, ask the user if they would like to install it, and if they would, proceed to download and install the framework
  • Call my program written in C# and then kill the C++ program


There are many ways to call .Net code from C#.

You can expose a class using COM Interop, or you can make a "bridge" using C++/CLI.

You can do it in "raw" C++ (without COM) by exporting methods from the C# DLL.

However, in your case, you might as well simply launch the C# EXE file using CreateProcess.


Out of curiosity, is it possible to call a C# application from a C++ application?

Yes. There are a few options here. If you use C++/CLI, you can use types defined in C# directly from within C++. Otherwise, a typical approach is to use COM, esposing your C# types as COM objects.

If so, is it also possible to check if the computer running the program has the .NET framework?

Yes. Here, typically, you'd just install the framework along with your application. Here's a page that shows a few options for checking which version of .NET (if any) is installed.


A C# (actually .NET) application is really a unmanaged stub exe that loads the .NET environment and then jumps into the C# code.

In other words, a C# app is always contained within an unmanaged process. So if you are willing to restrict yourself to using C# DLLs rather than C# applications you can host them in your own C++ process.

  • Check to see if .NET framework is installed on the computer
  • If it's not, ask the user if they would like to install it, and if they would, proceed to download and install the framework
  • Initialize the .NET runtime, load my C# library into it and jump into the C# code.
  • profit

Note the difference here is that you never shut down the C++ process, it becomes the host for your C# code - this is basically what Windows does anyway when you run a C# application.

0

精彩评论

暂无评论...
验证码 换一张
取 消