从 C++ 启动 C# 应用程序并在该应用程序上执行任务 [英] Launch a C# Application from C++ and performing a task on that application

查看:26
本文介绍了从 C++ 启动 C# 应用程序并在该应用程序上执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读this 并实现了我的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.

谢谢 Matthew,我搞定了.

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 的参数.

您需要在 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().

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

这篇关于从 C++ 启动 C# 应用程序并在该应用程序上执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆