1

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);
    }
}
Community
  • 1
  • 1
Neophile
  • 5,660
  • 14
  • 61
  • 107

1 Answers1

2

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

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Awesome.. I have done the C# implementation already, just did not know the C++ implementation. Also, I'm not sure about the negative vote from someone :( – Neophile Jun 05 '13 at 15:29
  • 1
    @TheNewbie I'm not sure about the negative vote; the question seemed clear enough. – Matthew Watson Jun 05 '13 at 15:31
  • Hi Matthew.. sorry to bother again.. but could you please have a look at my updated question and let me know why the file name is not getting copied across properly? Since it was related to the same question, I thought I'd just post below it. – Neophile Jun 06 '13 at 10:44
  • @TheNewbie Check out the help for CreateProcess(). You'll need to pass a dummy first string (usually the program name) followed by the program arguments, enclosed in quotes if they contain spaces, something like this: `DummyFirstArg "Path Name With Spaces"` – Matthew Watson Jun 06 '13 at 11:00