Solved

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

Posted on 2010-11-08
8
902 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
  • 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now