简体   繁体   English

如何从C#系统托盘应用程序中将C程序作为线程运行?

[英]How do I run a C program as a thread from a C# System Tray Application?

I have a C console application which used to be run in the foreground in a CMD terminal in Windows and take user keystroke inputs. 我有一个C控制台应用程序,它曾经在WindowsCMD终端中运行,并带有用户按键输入。 However it now needs to be moved to the background and requires no user input. 但是现在需要将其移动到后台并且不需要用户输入。

I have created a system tray, which is implemented correctly with a right click exit and right click about. 我创建了一个系统托盘,通过右键单击退出并右键单击即可正确实现。 And have a terminal program which does the functionality. 并有一个功能的终端程序。

Rather than rewritting the program again I would like to be able to create a thread which calls the functions from my existing program which are do not require the terminal. 我希望能够创建一个调用现有程序中不需要终端的函数的线程,而不是重新编写程序。

Just to stress the point the console interactive aspects have been removed from the code as have the applications response to keystrokes etc. Hopefully this clarifies things slightly. 只是强调控制台交互方面已从代码中删除,因为应用程序响应击键等。希望这会略微澄清一些事情。

Question: Is this possible? 问题:这可能吗? And how would I be able to implement this? 我怎么能够实现这个?

(I am generally not a PC programmer using more embedded C so .NET is quite foreign to me. Any help is greatly appreciated) (我通常不是使用更多嵌入式C的PC程序员,所以.NET对我来说很陌生。非常感谢任何帮助)

I'm not sure if this is what you are asking, but if you are asking if you can call functions from your C program from a C# application, then you can. 我不确定这是不是你问的问题,但如果你问你是否可以从C#应用程序调用C程序中的函数,那么你可以。 So long as you have the source code to your C program. 只要你有C程序的源代码。 In order for functions in C to be available to a C# program, they must be exported. 为了使C语言中的函数可用于C#程序,必须导出它们。 I will show you an example: 我会告诉你一个例子:

MyAddDll.h MyAddDll.h

#include <iostream>

extern "C"
{
    __declspec(dllexport) int Add(int a, int b);
}

Whats important here is that your function is wrapped in an extern "C" block. 这里重要的是你的函数包含在一个extern "C"块中。 Basically, this tells the compiler not to mangle the name of your function. 基本上,这告诉编译器不要破坏你的函数名称。 The other important piece is __declspec(dllexport) . 另一个重要的部分是__declspec(dllexport) This tells the compiler that you want to export this function so it can be called from another program (like your C# program). 这告诉编译器您要导出此函数,以便可以从其他程序(如C#程序)调用它。

MyAddDll.cpp MyAddDll.cpp

#include <iostream>
#include "MyAddDll.h"

extern "C"
{
    __declspec(dllexport) int Add(int a, int b)
    {
        return (a + b);
    }
}

Again, your code gets wrapped in an extern "C" block and you need to add __declspec(dllexport) to the function. 同样,您的代码包含在extern "C"块中,您需要将__declspec(dllexport)添加到该函数中。 Now you can compile this either into a DLL file or an EXE file. 现在您可以将其编译为DLL文件或EXE文件。 Now to call it from C#, its pretty straight forward: 现在从C#调用它,它很直接:

MyProgram.cs MyProgram.cs

class Program
{
    [DllImport("c:\\PathToMyDllOrExeFile\\MyAddDll.dll")]
    public static extern int Add(int val1, int val2);

    static void Main(string[] args)
    {
        int val1 = 12;
        int val2 = 4;
        Console.WriteLine("{0} + {1} = {2}", val1, val2, Add(val1, val2));
    }
}

That's all there is! 这就是全部!

One "gotcha" though is make sure your C program and your C# program are both compiled as either 32-bit or 64-bit. 一个“问题”是确保您的C程序和C#程序都编译为32位或64位。 You can't compile your C program as 64-bit and your C# program as 32-bit. 您不能将C程序编译为64位,将C#程序编译为32位。 They must both be using the same bit length. 它们必须使用相同的位长度。

As already posted, you could use the Process.Start from your C# application tray. 如上所述,您可以使用C#应用程序托盘中的Process.Start

    // Uses the ProcessStartInfo class to start new processes, 
    // both in a minimized mode. 
    void OpenWithStartInfo()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
        startInfo.WindowStyle = ProcessWindowStyle.Minimized;

        Process.Start(startInfo);

        startInfo.Arguments = "www.northwindtraders.com";

        Process.Start(startInfo);
    }

http://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx

You could try launching it on a hidden state: http://msdn.microsoft.com/en-us/library/system.diagnostics.processwindowstyle(v=vs.110).aspx 您可以尝试在隐藏状态下启动它: http//msdn.microsoft.com/en-us/library/system.diagnostics.processwindowstyle(v=vs.110).aspx

Some programmers are telling to be careful. 一些程序员告诉要小心。 If any input is expected, then probably your process might halt. 如果需要任何输入,那么您的进程可能会暂停。 make sure nothing really stops your program. 确保没有什么能真正阻止你的程序。

You could try to start a cmd command with Process.Start also, but I would say it is just too much. 您也可以尝试使用Process.Start启动cmd命令,但我会说它太多了。 This would start another process without your C# tray application control: 如果没有C#托盘应用程序控件,这将启动另一个进程:

 Process.Start("start /min cmd /c mycommand");

You could start your C program from .NET code via System.Diagnostics.Process. 您可以通过System.Diagnostics.Process从.NET代码启动您的C程序。 It will be run in a separate process. 它将在一个单独的过程中运行。

You can kill the process from .NET code as well 您也可以从.NET代码中删除该进程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM