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

我已经阅读了这篇文章并完成了我的C#应用​​程序的开放。 我的C#应用​​程序打开一个文件夹并绘制图形。 我是否有可能告诉我的C#应用​​程序从C ++打开哪个文件夹,然后一旦看到图形并且C#程序关闭,它就会返回到C ++应用程序。

编辑:谢谢马修,我得到了它的工作。

关于我的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 

我正确地得到变量szParam,但是当应用程序打开时,不会复制完整的文件名。 例如:在上述情况下,只有“和设置….”被复制到“C:\ Documents”部分留在哪里。 你能指出我的错误吗?

C#实现:

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

这当然是可能的。

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

您需要在C ++中做的是将lpCommandLine作为一个字符串传递,该字符串具有您要打开的文件夹的名称。 如果文件夹路径包含任何空格,则需要将字符串括在双引号中。

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

为了让C ++程序等待C#程序退出,它需要使用WaitForSingleObject()等待它退出,使用从CreateProcess()返回的进程句柄。

这在这里描述: http : //www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result