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

查看:159
本文介绍了从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().

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

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

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