Solved

Console app with parms -> dll app with parms

Posted on 2009-05-06
22
476 Views
Last Modified: 2013-11-07
Hi
I have a C# console application, that I will call from a website (ASP.NET).
Is it possible to convert the console app to a Class/dll and then call it.
And how?

The console app have parameters on.
0
Comment
Question by:conceptdata
  • 12
  • 9
22 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24314372
Converting to a dll is possible. When you add the reference to the dll, just call the appropriate function with the required parameters.

My question to you would be: Is there a reason you need it to be a dll? You can start an executable from your site by using System.Diagnostics.Process.Start("C:\path\filename.exe"). You would most likely need to add the executable to your website folder in order for the ASPNET user to execute it; otherwise you will have to grant permissions to the ASPNET user on the folder and executable containing your program.
0
 

Author Comment

by:conceptdata
ID: 24314456
Thanks.
I started to use the Process.Start, and a .exe file.
BUT, then I read that it could be an unstable solution.

I will try it
What about parameters ?
my local batch file looks like this :
nfop.exe -c ..\conf\userconfig.xml -fo ..\off.xsl.fo -pdf ..\off.pdf
0
 
LVL 9

Expert Comment

by:tculler
ID: 24315018
Just throw your "parameters" in next to the file in the Start method. For example, see the code below.

Do you need to actually start this process, or can you just use its methods instead? You can add a reference to an EXE just as well as a DLL, if the EXE was programmed in a .NET-Compliant language using the CLR.
System.Diagnostics.Process.Start("nfop.exe",

"-c ..\conf\userconfig.xml -fo ..\off.xsl.fo -pdf ..\off.pdf");

Open in new window

0
 

Author Comment

by:conceptdata
ID: 24317510
I'm a little bit newbie into this.
What do you mean by :
-------------
You can add a reference to an EXE just as well as a DLL, if the EXE was programmed in a .NET-Compliant language using the CLR.
-------------
Can you explain or better give an example - THANKS

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24317761
If your program was written in C#, VB.NET, C++ .NET, etc..  then you can add a reference to it--just like you add references to dll's. Just go to Project->Add Reference->Browse Tab and then locate your executable file and click Ok.

I didn't think about doing it that way. Good call tculler.
0
 

Author Comment

by:conceptdata
ID: 24318069
Okay thanks - But how do I run the "reference" (exe file) then ?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24318510
You won't be running the file at that point--at least not in the sense of running an executable through explorer. You can call all the public methods that you declared in your executable--just like when you reference a dll, you can call the public functions available in that dll.

Look at the following two applications. ConsoleApplication2 has a public class "A_Class" that has a public method "Add_1_and_2()". You can build/run this application as a standalone executable.

ConsoleApplication3 has a reference to ConsoleApplication2. As such, I can declare an "A_Class" object and call its method(s).
//////////////////////////////////////

//  App 1

//////////////////////////////////////
 

using System;
 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            A_Class ac = new A_Class();
 

            Console.WriteLine(ac.Add_1_and_2());

            Console.ReadKey();

        }

    }
 

    public class A_Class

    {

        private const int ONE = 1;

        private const int TWO = 2;
 

        public int Add_1_and_2()

        {

            return ONE + TWO;

        }

    }

}
 

//////////////////////////////////////

//  App 2

//////////////////////////////////////
 

using System;
 

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            ConsoleApplication2.A_Class ac = new ConsoleApplication2.A_Class();
 

            Console.WriteLine(ac.Add_1_and_2());

            Console.ReadKey();

        }

    }

}

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24318593
Additionally, if you really wanted to be confusing to coders that follow behind you (and I am, of course, not suggesting you do this; rather I am demonstrating a concept), you could call the static method Main of ConsoleApplication2 from ConsoleApplication3. To do this, however, you would have to, within ConsoleApplication2, mark the class Program as public and the static method Main as public. Then the following could be done in ConsoleApplication3:
using System;
 

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            ConsoleApplication2.Program.Main(args);  // We marked "Program" and "Main" as public in ConsoleApplication2
 

            Console.ReadKey();

        }

    }

}

Open in new window

0
 

Author Comment

by:conceptdata
ID: 24319308
I'm a little bit lost here..

my nFopApp look like Code Snippet
using System;
 

namespace NFopApp

{

   
 

    

    public class NFopApp

    {

        public static void PrintUsage()

        {

            Console.WriteLine(GetUsage());

        }
 

        public static string GetUsage()

        {

            System.Text.StringBuilder usageBuffer = new System.Text.StringBuilder();

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("nFop [options] [-fo|-xml] <infile> [-xsl file] [-foout | -pdf] <outfile>");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("[OPTIONS]");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("-c cfg.xml        use additional configuration file cfg.xml");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("(you can set NFOP_USER_CONFIG_PATH environment variable and override it with -c parameter)");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("[INPUT]");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("infile            xsl:fo input file (the same as the next)");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("-fo  infile       xsl:fo input file");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("-xml infile       xml input file, must be used together with -xsl");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("-xsl stylesheet   xslt stylesheet");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("[OUTPUT]");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("-pdf outfile      input will be rendered as pdf file (outfile req'd)");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("-foout outfile    input will be rendered as fo file (outfile req'd)");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("[Examples]");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("nFop foo.fo foo.pdf");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("nFop -fo foo.fo -pdf foo.pdf (does the same as the previous line)");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("nFop -xsl foo.xsl -xml foo.xml -pdf foo.pdf");

            usageBuffer.AppendFormat(Environment.NewLine);

            usageBuffer.AppendFormat("nFop -xsl foo.xsl -xml foo.xml -foout foo.fo -pdf foo.pdf");

            usageBuffer.AppendFormat(Environment.NewLine);
 

            return usageBuffer.ToString();

        }
 

        public static bool CheckParameters(CommandLineArguments cmdLineArgs)

        {

            if (cmdLineArgs.HasArguments)

            {

                switch (cmdLineArgs.Arguments.Count)

                {

                    case 2: if (cmdLineArgs.HasFO  && cmdLineArgs.HasPDF) return true; break;

                    case 3: if (cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasPDF) return true; break;

                    case 4: if (cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasFOOUT && cmdLineArgs.HasPDF) return true; break;

                }

            }
 

            return false;

        }
 
 

        static void Main(string[] args)

        {

            int ExitCode = 0;
 

            try

            {

                CommandLineArguments cmdLineArgs = new CommandLineArguments(args);
 

                if (CheckParameters(cmdLineArgs))

                {

                    switch (cmdLineArgs.Arguments.Count)

                    {

                        case 2:
 

                            if (cmdLineArgs.HasUSERCONFIG && cmdLineArgs.HasFO && cmdLineArgs.HasPDF)

                            {

                                Fop.Net.NFop.Create_PDF_from_FO_with_CONFIG(cmdLineArgs.FO, cmdLineArgs.PDF, cmdLineArgs.USERCONFIG);

                            }

                            else if (cmdLineArgs.HasUSERCONFIG == false && cmdLineArgs.HasFO && cmdLineArgs.HasPDF)

                            {

                                Fop.Net.NFop.Create_PDF_from_FO(cmdLineArgs.FO, cmdLineArgs.PDF);

                            }
 

                            break;

                        case 3:
 

                            if (cmdLineArgs.HasUSERCONFIG && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasPDF)

                            {

                                Fop.Net.NFop.Create_PDF_from_XML_XSL_with_CONFIG(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.PDF, cmdLineArgs.USERCONFIG);

                            }

                            else if (cmdLineArgs.HasUSERCONFIG == false && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasPDF)

                            {

                                Fop.Net.NFop.Create_PDF_from_XML_XSL(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.PDF);

                            }
 

                            break;

                        case 4:
 

                            if (cmdLineArgs.HasUSERCONFIG && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasFOOUT && cmdLineArgs.HasPDF)

                            {

                                Fop.Net.NFop.Create_PDF_and_FO_from_XML_XSL_with_CONFIG(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.FOOUT, cmdLineArgs.PDF, cmdLineArgs.USERCONFIG);

                            }

                            else if (cmdLineArgs.HasUSERCONFIG == false && cmdLineArgs.HasXML && cmdLineArgs.HasXSL && cmdLineArgs.HasFOOUT && cmdLineArgs.HasPDF)

                            {

                                Fop.Net.NFop.Create_PDF_and_FO_from_XML_XSL(cmdLineArgs.XML, cmdLineArgs.XSL, cmdLineArgs.FOOUT, cmdLineArgs.PDF);

                            }
 

                            break;

                    }

                }

                else

                {

                    PrintUsage();

                }

            }

            catch (System.IO.FileNotFoundException fileNotFoundException)

            {

                ExitCode = 1;

                Console.WriteLine(fileNotFoundException.Message);

            }

            catch (CommandLineArgumentException commandLineArgumentException)

            {

                ExitCode = 1;

                Console.WriteLine("{0}:\t{1}", commandLineArgumentException.Message, commandLineArgumentException.ParamName);

            }

            catch (Exception exception)

            {

                ExitCode = 1;

                Console.WriteLine("{0}{1}{2}", exception.Message, Environment.NewLine, exception.StackTrace);

            }
 

            Environment.Exit(ExitCode);

        }

    }

}

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24324112
I'm a little lost too....   What is this code and what are you trying to do with it :)
0
 

Author Comment

by:conceptdata
ID: 24326203
I have attached the solution.

The fil KJ_Build.bat makes a PDF-file from xML and xsl files...
The folder nfop_fw2\nfop\nfopapp\bin\debug\ have the nfop.exe file that I imported into another C# asp.net solution, and then I should "run" the reference like the KJ_Build.bat....

Any idea ??
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:conceptdata
ID: 24326586
UPS, here is the attached file
test1.txt
0
 

Author Comment

by:conceptdata
ID: 24326679
rename attached file to *****.rar and unrar
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24327519
Your Main() method in NFopApp class will need to be marked as public in order to call it from external code. Once you make that change (and add a reference to the executable), you can call it as demonstrated in the attached image (click to enlarge).
untitled1.JPG
0
 

Author Comment

by:conceptdata
ID: 24328893
Okay, where do I put my parameters :

-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf
0
 

Author Comment

by:conceptdata
ID: 24329416
One more thing.
I have to call til reference from a aspx website... maybe from the C# code behind.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24329721
For parameters, you could do something like in the snippet. Also, yes, you can call this function from within the code-behind.
NFopApp.NFopApp.Main("-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf".Split(new char[] {' '});

Open in new window

0
 

Author Comment

by:conceptdata
ID: 24329953
Something like this ??

But do it call the reference / exe file ?
namespace nFopWeb

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(string[] args)

        {

            

            NFopApp.NFopApp.Main("-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf".Split(new char[] {' '}));

        }

        }
 

    }

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 24330274
Yes, like that. You will not see a console window because you are not starting a process. You are actually calling the methods contained within NFopApp as if there were in the website project themselves. Since Main() is public and static, you don't have to create an object of type NFopApp in order to call the function Main().
0
 

Author Comment

by:conceptdata
ID: 24345523
Okay, I've tried it, but it is no working for me.

BUT, maybe it is possible that i don't manage to put the right directory for the file in the parameter ..

NFopApp.NFopApp.Main("-c ../conf/userconfig.xml -fo ../off.xsl.fo -pdf ../off.pdf".Split(new char[] {' '}));

Can I use server.mappath og something here ...
.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 24354602
Well, because you are now using your Console Application as code instead of a standalone program, the relative paths you are using are relative to the location of your web app, not the location of the Console Application. So something like "../conf/userconfig.xml" is saying:

   Go up one directory from the web application's directory, find the conf directory, find userconfig.xml

Is this where that xml file is located in relation to the directory of the website?
0
 

Author Closing Comment

by:conceptdata
ID: 31578437
Thanks - now it works
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

11 Experts available now in Live!

Get 1:1 Help Now