如何从控件中调用c#windows应用程序?

时间:2022-09-20 20:56:53

I need to invoke a c# application within my c# control, as I do not feel like rewriting the application as a control.

我需要在c#控件中调用一个c#应用程序,因为我不想将应用程序重写为控件。

I am able to launch the application using System.Diagnostics.Process.Start.

我可以使用System.Diagnostics.Process.Start启动应用程序。

Now how do I call the methods in my application from/via the c# control as this is where I invoked the application using System.Diagnostics.Process.Start

现在我如何从/通过c#控件调用我的应用程序中的方法,因为这是我使用System.Diagnostics.Process.Start调用应用程序的地方

5 个解决方案

#1


1  

The easiest way is to change the application from .exe to .dll and then just reference the application in your project like a normal library.

最简单的方法是将应用程序从.exe更改为.dll,然后像普通库一样引用项目中的应用程序。

#2


0  

You can't call "methods" of another process because its running in an entirely different OS process. You would have to essentially ask the process to execute a method for you, wait for the result, and then marshall the information back into your process. Of course, you would have to have to expose the methods through some kind of interprocess communication technology like WCF, COM+, etc. If you've got that kind of access to the executing program, you can also just use the assembly directly.

您无法调用另一个进程的“方法”,因为它在完全不同的操作系统进程中运行。您必须基本上要求进程为您执行一个方法,等待结果,然后将信息编组回您的进程。当然,您必须通过某种进程间通信技术(如WCF,COM +等)来公开这些方法。如果您对执行程序有这种访问权限,您也可以直接使用程序集。

You really don't want to go through that if you can help it. It's going to be a lot more hassle.

如果你能提供帮助,你真的不想通过它。这将是更麻烦的事情。

#3


0  

Since you have the source code, copy it into the new project or create a DLL as suggested by @Nick Berardi.

由于您拥有源代码,因此将其复制到新项目中或按照@Nick Berardi的建议创建DLL。

#4


0  

You could modify your .exe application and add a remoting interface to it, making it a server-process, and then let your "control" act as client-process and call methods on the server.

您可以修改.exe应用程序并为其添加远程处理接口,使其成为服务器进程,然后让您的“控件”充当客户端进程并在服务器上调用方法。

This is a hacky design and I wouldn't recommend it, but since you asked :)

这是一个hacky设计,我不推荐它,但因为你问:)

#5


0  

You can use System.Reflection to get into any assembly, regardless of EXE or DLL format.

无论EXE或DLL格式如何,您都可以使用System.Reflection进入任何程序集。

To run another application, for example,

例如,运行另一个应用程序,

string fileName = "MainApp.exe";
string className = "Program";
string methodName = "Main";
string[] args = {"arg1", "arg2"};

Assembly asm = Assembly.LoadFrom(fileName);
foreach (Type typ in asm.GetTypes())
{
    if (typ.Name == className) 
    {
        BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;
        MethodInfo method = typ.GetMethod(methodName, flags);
        method.Invoke(null, new object[] { args });
    }
}

To get to other methods, change the names accordingly.

要获取其他方法,请相应地更改名称。

This is based on actual working code that I originally derived from this example.

这基于我最初从此示例中派生的实际工作代码。

There might be better ways to write it, though -- this thread suggests using the Activator class for more concise code.

可能有更好的方法来编写它 - 这个线程建议使用Activator类来获得更简洁的代码。

#1


1  

The easiest way is to change the application from .exe to .dll and then just reference the application in your project like a normal library.

最简单的方法是将应用程序从.exe更改为.dll,然后像普通库一样引用项目中的应用程序。

#2


0  

You can't call "methods" of another process because its running in an entirely different OS process. You would have to essentially ask the process to execute a method for you, wait for the result, and then marshall the information back into your process. Of course, you would have to have to expose the methods through some kind of interprocess communication technology like WCF, COM+, etc. If you've got that kind of access to the executing program, you can also just use the assembly directly.

您无法调用另一个进程的“方法”,因为它在完全不同的操作系统进程中运行。您必须基本上要求进程为您执行一个方法,等待结果,然后将信息编组回您的进程。当然,您必须通过某种进程间通信技术(如WCF,COM +等)来公开这些方法。如果您对执行程序有这种访问权限,您也可以直接使用程序集。

You really don't want to go through that if you can help it. It's going to be a lot more hassle.

如果你能提供帮助,你真的不想通过它。这将是更麻烦的事情。

#3


0  

Since you have the source code, copy it into the new project or create a DLL as suggested by @Nick Berardi.

由于您拥有源代码,因此将其复制到新项目中或按照@Nick Berardi的建议创建DLL。

#4


0  

You could modify your .exe application and add a remoting interface to it, making it a server-process, and then let your "control" act as client-process and call methods on the server.

您可以修改.exe应用程序并为其添加远程处理接口,使其成为服务器进程,然后让您的“控件”充当客户端进程并在服务器上调用方法。

This is a hacky design and I wouldn't recommend it, but since you asked :)

这是一个hacky设计,我不推荐它,但因为你问:)

#5


0  

You can use System.Reflection to get into any assembly, regardless of EXE or DLL format.

无论EXE或DLL格式如何,您都可以使用System.Reflection进入任何程序集。

To run another application, for example,

例如,运行另一个应用程序,

string fileName = "MainApp.exe";
string className = "Program";
string methodName = "Main";
string[] args = {"arg1", "arg2"};

Assembly asm = Assembly.LoadFrom(fileName);
foreach (Type typ in asm.GetTypes())
{
    if (typ.Name == className) 
    {
        BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;
        MethodInfo method = typ.GetMethod(methodName, flags);
        method.Invoke(null, new object[] { args });
    }
}

To get to other methods, change the names accordingly.

要获取其他方法,请相应地更改名称。

This is based on actual working code that I originally derived from this example.

这基于我最初从此示例中派生的实际工作代码。

There might be better ways to write it, though -- this thread suggests using the Activator class for more concise code.

可能有更好的方法来编写它 - 这个线程建议使用Activator类来获得更简洁的代码。