从C ++启动C#应用程序并在该应用程序上执行任务

编程入门 行业动态 更新时间:2024-10-28 07:24:26
本文介绍了从C ++启动C#应用程序并在该应用程序上执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已阅读此,并实现了C#的打开应用.我的C#应用​​程序打开一个文件夹并绘制一个图形.是否可以告诉我的C#应用​​程序要从C ++打开哪个文件夹,然后一旦看到图形并关闭了C#程序,它就会返回到C ++应用程序.

I have read this and achieved the opening of my C# application. My C# application opens a folder and draws a graph. Is it possible for me to tell my C# application which folder to open from C++ and then once the graph is seen and the C# program is closed, it returns back to the C++ app.

谢谢马修,我可以使用它.

Thanks Matthew I got it working.

与我的CreateProcess lpCommandLine变量有关的另一个查询:(下面是代码)

Another query in relation to my CreateProcess lpCommandLine variable: (Below is the code)

CString sFolderPath = "C:\Documents and Settings\..."; int nStrBuffer = sFolderPath.GetLength() + 50; LPTSTR szParam = _tcsdup(sFolderPath.GetBuffer(nStrBuffer)); nRet = ::CreateProcess(szCmdline,// pointer to name of executable module szParam,// pointer to command line string NULL,// pointer to process security attributes NULL,// pointer to thread security attributes FALSE,// handle inheritance flag DETACHED_PROCESS,// creation flags NULL,// pointer to new environment block NULL,// pointer to current directory name &sui,// pointer to STARTUPINFO &pi );// pointer to PROCESS_INFORMATION

我正确地获取了变量szParam,但是当应用程序打开时,不会复制完整的文件名.例如:在上述情况下,仅将"and Settings ...."复制到留下"C:\ Documents"部分的位置.你能指出我的错误吗?

I get the variable szParam properly, but when the application opens up, the complete filename is not copied across. For eg: In the above case, only " and Settings...." is copied across where as the "C:\Documents" part is left behind. Could you point out on my mistake please?

C#实现:

[STAThread] static void Main(string[] args) { foreach (string result in args) { MessageBox.Show(result); } }

推荐答案

当然可以.

C ++ CreateProcess() 具有一个名为lpCommandLine的参数.

The C++ CreateProcess() has a parameter called lpCommandLine.

在C ++中您需要做的是以lpCommandLine的形式传递一个字符串,该字符串具有您要打开的文件夹的名称.如果文件夹路径中包含空格,则需要将字符串用双引号引起来.

What you need to do in the C++ is to pass as lpCommandLine a string that has the name of the folder that you want to open. You will need to enclose the string in double quotes if the folder path contains any spaces.

在C#程序中,您将有一个static void Main(string[] args). args参数将包含您从C ++程序传递的文件夹名称,以便您可以适当地对其进行操作.

Inside your C# program you will have a static void Main(string[] args). The args parameter will contain the folder name that you passed from the C++ program so that you can act on it appropriately.

要让C ++程序等待C#程序退出,就需要使用WaitForSingleObject()来等待它退出,并使用从CreateProcess()返回的进程句柄.

For the C++ program to wait for the C# program to exit, it will need to use WaitForSingleObject() to wait for it to exit, using the process handle returned from CreateProcess().

在此处进行了描述: www.codeproject /Tips/333559/CreateProcess-and-wait-for-result

更多推荐

从C ++启动C#应用程序并在该应用程序上执行任务

本文发布于:2023-11-02 13:09:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   并在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!