Avatar of panJames
panJames asked on

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

C++

Avatar of undefined
Last Comment
panJames

8/22/2022 - Mon
masheik

You can call the window application using system call
System(path\app.exe)
Inside main
ASKER
panJames

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
mrwad99

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
masheik

    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
ASKER
panJames

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
ASKER CERTIFIED SOLUTION
masheik

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
masheik

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
panJames

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