Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-08
8
Medium Priority
?
929 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:panJames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:masheik
ID: 34085191
You can call the window application using system call
System(path\app.exe)
Inside main
0
 

Author Comment

by:panJames
ID: 34086114
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 34091530
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:masheik
ID: 34091556
    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
 

Author Comment

by:panJames
ID: 34092477
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
 
LVL 9

Accepted Solution

by:
masheik earned 2000 total points
ID: 34092678
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
 
LVL 9

Expert Comment

by:masheik
ID: 34092787
0
 

Author Comment

by:panJames
ID: 34094020
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question