none
How to call a console application from a project clr? "Spawn"? RRS feed

  • Question

  • I work in C++ with Visual C++ 2010:

    I have a CLR project (windows form), it is the GUI of my application. When, I have a console project, it is the engine of my application. I have to display out engine in GUI.

    I thought this: CLR<>DLL<>console engine. But the CLR  project communicates with the DLL, but I can not call the console application from CLR.  I tried with spawn and system, but failed. The console project  seems inactive.

    Instead I have no problem if I start a console application from the console project  with spawn, using a DLL to communicate:

    solution CLR project <>DLL<> console project = KO

    solution console project <>DLL<>  console project  = OK

    solution CLR project <>DLL = OK

    How to call a console application from a project clr?


    beginner of planning


    • Edited by Albitex Sunday, February 3, 2013 9:11 PM
    Sunday, February 3, 2013 9:07 PM

Answers

  • I suggest you use "Named Pipes" for communicating between your processes.

         Named Pipes are too much hard for me. I have resolved with  a .txt file  use as input/output  interface. Thanks equally.

    beginner of planning

    Sunday, February 10, 2013 2:51 AM

All replies

  • I could think of the following possible solutions for your issue

    1. If you have the control over the source code, then you should just convert the part of the console project that is needed to be reused as a dll, and just refer to this from your project.

    2. You could use an Inter Process Communication (IPC) between this console Process and your process where you want to run your CLR application.

    Hope that helps, or you should explain your problem a little more in detail.

    • Proposed as answer by Konrad Neitzel Monday, February 4, 2013 9:27 AM
    Monday, February 4, 2013 9:11 AM
  • I could think of the following possible solutions for your issue

    1. If you have the control over the source code, then you should just convert the part of the console project that is needed to be reused as a dll, and just refer to this from your project.

    2. You could use an Inter Process Communication (IPC) between this console Process and your process where you want to run your CLR application.

    Hope that helps, or you should explain your problem a little more in detail.

    I think your n.2 solution is the right one for me.
    The n.1 solution  I do not think is feasible in my case.
    This is because my engine is a multithreaded application , and I can not incorporate its functions in a DLL.
    I have attached below the test code I used. Code is a simple test.
    The simple DLL is used to communicate data "string Depth" and "int state" between the clr application and the  console application.
     I acquire a string of text in a textbox.
    Then send the string to the DLL. It is deposited in the variable "string Depth" of the DLL. Booting from the CLR the Console Application, with spawn.
    The console application should read the "string Depth" from DLL, when edit "string Depth" and send the modified string to DLL. So the CLR application reads the "string Depth" and displays it in a text box. But error: the displayed string is not changed.
    In addition, the spawn function, returns -1 and not 0.
     This means that spawn failed the start operation of the console application.

    The DLL code:

    // DLL.h
    #include<string>
    namespace inpoutFun
    {
    	
        class inpfGUI
        {
        public:
         // Load string "in" in DLL	
          static __declspec(dllexport) void inpD(std::string in);
         // Upload string "out" da DLL
         static __declspec(dllexport) std::string outD();
        // Load state in DLL
        static __declspec(dllexport) void inpCs(int stateC);
       // Upload state da DLL
       static __declspec(dllexport) int outCs();
    		
    	            
        };
    }
    // DLL.cpp
    // compile with: /EHsc /LD
    #include "inpoutdll.h"
    #include <stdexcept>
    #include<string>
    using namespace std;
    namespace inpoutFun
    {
    	string Depth;int state;
    	
    	// fun input data string from GUI
        void inpfGUI::inpD(std::string in)
        {
    		Depth=in;
        }
    	
    	// fun  export data string 
        string inpfGUI::outD()
    	{
    		return Depth;
    	}
    	
    	// fun input the "int state" from Children
    	void inpfGUI::inpCs(int stateC)
    	{
    		state=stateC;
    	}
    	// fun export "int state" from  Children to DLL
    	int inpfGUI::outCs()
    	{
    		return state;
    	}
    THE CONSOLE CHILDREN CODE:
    // Code console application,  simple test
    #include "stdafx.h"
    #include "inpoutdll.h"
    #include <string>
    #include<iostream>
    using namespace std;
    int main()
    {
        int statec=0; string DEP,input;
        	   
        inpoutFun::inpfGUI::inpCs(statec);
        DEP=inpoutFun::inpfGUI::outD();
        inpoutFun::inpfGUI::inpD("Depth= "+DEP);
        statec=1;
        inpoutFun::inpfGUI::inpCs(statec);
    		
        return 0;
    	
    }
     THE CLR FUNCTION
    // FUN CLR
    // Run Children and display "string Depth" in textbox in a button event
    void outtras()
    {
        int ok;std::string str;
        // call children program 	
        ok=_spawnl(_P_DETACH,"Children.exe","Children",NULL);
        //wait Children end work (outCs==1)
        // (here the program stop,I must write:while(outCs==1) for continue)
       do{
               str=" ";
        }
       while(inpoutFun::inpfGUI::outCs()==0);
      str=inpoutFun::inpfGUI::outD();
      // display modified string 
      textBox2->Text = gcnew String(str.c_str());
    				
     }


    beginner of planning





    • Edited by Albitex Monday, February 4, 2013 2:59 PM
    Monday, February 4, 2013 2:50 PM
  • I want to use "popen" but in the Visual Studio Help Guide, in popen thread I read:


    beginner of planning

    Monday, February 4, 2013 8:20 PM
  • I suggest you use "Named Pipes" for communicating between your processes.
    Tuesday, February 5, 2013 11:26 AM
  • I suggest you use "Named Pipes" for communicating between your processes.

         Named Pipes are too much hard for me. I have resolved with  a .txt file  use as input/output  interface. Thanks equally.

    beginner of planning

    Sunday, February 10, 2013 2:51 AM