Calling main from (console application) from (windows forms application)

Please have a look at the code!

I get "All tags must have a closing tag." error if I try to do the other way.

Hello experts!

I have a C++ program example written as a console application.

It looks something like this:

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

#define CVX_RED		CV_RGB(0xff,0x00,0x00)
#define CVX_GREEN	CV_RGB(0x00,0xff,0x00)
#define CVX_BLUE	                CV_RGB(0x00,0x00,0xff)


int main(int argc, char* argv[]) {
  //some code here

}

Now I would like to create windows form application and call the main function from my example- passing file names to it.

How can I do it?



This is my attempt:

(Added my example file to Source Files)

I have a form, a button and listBox on it + OpenFileDialog

User clicks the button:

private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
				if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
				{
					for (int i; i < openFileDialog1->FileNames->Length; i ++)
					{
						listBox1->Items->Add(openFileDialog1->FileNames[i]);

					}
				}

			 }


Then user clicks the file name in a listBox:

private: System::Void listBox1_Click(System::Object^  sender, System::EventArgs^  e) {
//what should I put here?
				 
			 }


Thank you

panJames

Open in new window

panJamesAsked:
Who is Participating?
 
masheikConnect With a Mentor Commented:
Hi,

 >>Do I need to create "ExampleFile.h", cannot I just use my Example.cpp file?

   Yes , basically the Example.h contains the declarations where as Example.cpp
contains definitons of what you declared in example.h

   If you added the .cpp and .h files to the project , if you want to call a function
 inside the class of Example.cpp file in another file say myWindowPane.cpp then you can call
 the call class,functions by including #include "Example.h" to the myWindowPane.cpp...
 
 simple ha..?
 
 And include all your .h files at very beginnning say
 
 #include "Includes.h" //Here say i am not using < > if you want to use <> then
                       //you need to inlcude the library to the stardard libray path
 #include <iostream> //some std includes say for example
 
 namespace OpenCVtester {

      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;
     
      public class testClass{
      };
      }
     
      Hope this helps...
0
 
masheikCommented:
You can call the window application using system call
System(path\app.exe)
Inside main
0
 
panJamesAuthor Commented:
Ok... you want me to run compiled example but I would prefer to add this example's code to the code of my windows form application.

panJames
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
mrwad99Commented:
You have what looks like a C++ console application that you want to be called from the managed C++ Winforms app, right?  If you don't want to call the compiled binary from your winforms app, then since both projects are C++ you can simply add the source code for your console app to your winforms project.  If you do it this way, you will not need a main() method: you could simply rewrite main() to accept slightly differently formatted arguments, perhaps making use of the managed classes, like String.

If you clarify exactly what you want and why you want to do it, I am sure you will receive more help.
0
 
masheikCommented:
    You Can NOT call main() from your windows application instead make it as a anthor class function
 and write that class,function definitons in .h file then include the .h file inside your window code where you wanted
 to call the functions,

 For example
 your ExampleFile.cpp

         public class testClass
             {
                   testClass()
                   {
                         //init
                   }
                   int function2Call(int x,int y)//instead of main
                   {
                         //some code...
                   }
                   ~testClass()
                   {
                   }
             };

             create a ExampleFile.h

                   inside it,

                   just include guard headers,write,

             #ifndef EXAMPLE_FILE_H
             #define EXAMPLE_FILE_H
             #define CVX_RED    CV_RGB(0xff,0x00,0x00)
             #define CVX_GREEN      CV_RGB(0x00,0xff,0x00)
             #define CVX_BLUE      CV_RGB(0x00,0x00,0xff)
                   class testClass
                   {
                         /*varibables if any */
                         int vars;
                         /* methods */
                         int function2Call(int x,int y);
                   };
             #endif

         And also you have to consider the #include <cv.h>,#include <highgui.h> you needed to include the source of the
 both & correspoding source files,

         Another solution is make that example as a dll library and call from your windows applictiaon
0
 
panJamesAuthor Commented:
Do I need to create "ExampleFile.h", cannot I just use my Example.cpp file?

Anyway, I added one line here:

namespace OpenCVtester {

      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;

      #include <ExampleFile.h> //<- new line


Now compiler found lots of errors like:

c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(234) : error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(234) : error C2039: 'YesNoMaybe' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(234) : error C2146: syntax error : missing ';' before identifier 'SA_YesNoMaybe'
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(234) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(234) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(235) : error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(235) : error C2039: 'YesNoMaybe' : is not a member of '`global namespace''
c:\program files\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(235) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

What should I do?

panJames
0
 
masheikCommented:
0
 
panJamesAuthor Commented:
Still struggling to make it work. Anyway, this topic is probably too long now for 500 points so I open another one so please find it :-)

panJames
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.