Launch a C# Application from C++ and performing a

2019-03-05 14:07发布

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.

Edit: Thanks Matthew I got it working.

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

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# implementation:

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

标签: c# c++ mfc launch
1条回答
啃猪蹄的小仙女
2楼-- · 2019-03-05 14:38

It certainly is possible.

The C++ CreateProcess() has a parameter called 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.

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.

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().

This is described here: http://www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result

查看更多
登录 后发表回答