?
Solved

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

Posted on 2010-11-08
8
Medium Priority
?
919 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

752 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