简体   繁体   English

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

[英]Launch a C# Application from C++ and performing a task on that application

I have read this and achieved the opening of my C# application. 我已阅读取得了我的C#应用程序的开放。 My C# application opens a folder and draws a graph. 我的C#应用​​程序打开一个文件夹并绘制一个图形。 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. 是否可以告诉我的C#应用​​程序要从C ++打开哪个文件夹,然后一旦看到图形并关闭了C#程序,它就会返回到C ++应用程序。

Edit: Thanks Matthew I got it working. 编辑:谢谢马修我让它工作。

Another query in relation to my CreateProcess lpCommandLine variable: (Below is the code) 与我的CreateProcess lpCommandLine变量有关的另一个查询:(下面是代码)

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

I get the variable szParam properly, but when the application opens up, the complete filename is not copied across. 我正确地获得了变量szParam,但是当应用程序打开时,不会复制完整的文件名。 For eg: In the above case, only " and Settings...." is copied across where as the "C:\\Documents" part is left behind. 例如:在上述情况下,仅将“ and Settings ....”复制到留下“ C:\\ Documents”部分的位置。 Could you point out on my mistake please? 你能指出我的错误吗?

C# implementation: C#实现:

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

It certainly is possible. 当然有可能。

The C++ CreateProcess() has a parameter called lpCommandLine . C ++ CreateProcess()具有一个名为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. 在C ++中,您需要做的是将包含您要打开的文件夹名称的字符串作为lpCommandLine传递。 You will need to enclose the string in double quotes if the folder path contains any spaces. 如果文件夹路径包含任何空格,则需要将字符串用双引号引起来。

Inside your C# program you will have a static void Main(string[] args) . 在您的C#程序内部,您将有一个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. args参数将包含您从C ++程序传递的文件夹名称,以便您可以适当地对其进行操作。

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() . 对于C ++程序等待的C#程序退出,它需要使用WaitForSingleObject()等待它退出,使用过程中从返回的句柄CreateProcess()

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

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

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