How to compile a GUI application using mono? - c#

I wrote a C# program and I will compile like this:
C:\Users\Administrator\Documents\Visual Studio 2005\Projects\GUITest\GUITest>mcs *.cs /r:System.Data,System.Drawing,System.Windows.Forms,..\HtmlAgilityPack.dll
But the output application has a console window.
Is there a way to compile the program so that I can get a application without a console window?

There are two different "modes" or types of Windows applications: console applications and GUI applications. The same goes for managed applications, regardless of how you build them.
Console applications will always display a console window on startup, automatically. You can also write code to display a GUI window (e.g., a form), but this is optional. Either way, the console window will always be displayed.
GUI applications do not display anything on startup. Generally, you write code that displays a GUI window (e.g., a form), but you do not have to. If you do not display anything, you have created what people often refer to as a "background application" because it runs in the background without displaying any UI. That is not possible with a console application, because it displays that ugly console window.
So if you don't want the console window, you don't want a console application. You want a regular GUI application.
Now, the challenge is figuring out how to achieve this using the Mono compiler. Visual Studio exposes this option as a project-level setting. The Mono compiler needs a flag, /target to tell it what type of application to build.
/target:exe (the default option) will build a console application
/target:winexe will build a GUI application
/target:library will build a library (which is not an executable application, but just a chunk of reusable code)
So, change the command you're running at the command line to:
mcs *.cs /target:winexe /r:System.Data,System.Drawing,System.Windows.Forms,...
I believe that you will also need to make sure that you're running a relatively recent version of Mono. The older versions did not include support for creating GUI applications (the /target:winexe switch was not implemented). I know that this is fully supported as of version 2.8, but there's little reason not to use the latest version available.
In case my answer is not authoritative enough for you, you'll find the same quick fix (without the rationale) documented in the Mono WinForms FAQ.

Related

Visual Studio 2015, C# and creating releases

This is a question for which I need someone to give me some pointers and point me in the right direction. Not a "suggest-a-sulution-code" question:
How do I create a stand-alone release of a C# Windows Froms application that uses a couple of NuGet packages AND an external library (AutoIT)?
The application is a simple forms application which uses Selenium Webdriver for opening and accessing a browser. In addition to this, it uses a library called AutoIt, for handling windows operations.
In my VS project I've installed Selenium Webdriver with NuGet, and added the AutoIt libraries manually as externals.
The resulting .exe file created after build does not work on another computer when I just copy it over. The GUI and functionality works, but the AutoIt library is not included. This is probably logical, and due to my lack of knowledge regarding building windows applications. But how do I do this? How do I build a release which can be used by others, which includes any external libraries used by the application? Do I have to manually get all DLL files relatedt to the project, or is there a way to package things to make it easier?

C# Winform Compilation Error

I have a winform c# app with a real text box that I use for logging output. When I compile the app with "anycpu", i can view the main form (with the text box on it) just fine. However, when I compile as x64 (which I need to do for ms database engine), it states could not find my logging control. As soon as I go back to anycpu, again the form opens in designer just fine.
What am I missing?
Apparently, in configuration manager I had active solution platform: x64 and the project platform as x64 and that was not working. Switching the project platform to Any CPU at least for now enables me to view the form in design view so I guess it's fixed?

.NET C# App running good on Development PC but doesn't run on Production computer

So it's basically as stated in the title. I've created a WPF app in Visual Studio 2013 using some external libraries.
Application works flawlessly on my Development machine (Windows 8.1 x64 + Visual Studio 2013) but doesn't run at all on Production device (Tablet with Windows 8 (NOT 8.1)). App is developed under .NET 4.5, doesn't matter if I try to run Debug or Release version. The proccess just hangs a while and then closes without any errors or messages.
If anyone would know what to do or how to fix this I would be very happy.
Thanks in advance :).
The first thing too look up in this situation, would be the Windows event log. When a .NET application crashes badly, the .NET Runtime will log an event there.
Most of the time, these events will log the stack trace which lead to the crash. This will give you a hint as to what happened.
Such logs are found in the "Application" category, and the source name you need to look for is ".NET Runtime". Usually, there will be another entry with the source name "Application Error", but this one is much less likely to help you.
Another useful technique, is to add a Console.WriteLine call at the start of your program, in order to see if that line gets to run at all.
Depending on what you find using these techniques, you may also want to use tools such as Dependency Walker or ILSpy, as suggested by Mike Dinescu.
Most likely a binding error occurs at start-up but the production machine you're testing on is configured to silently report these errors and it doesn't display the typical dialog box that would inform you that the process crashed.
Don't fret though, that message would probably not help you too much anyway.
What you need to do is inspect the main executable with a tool that can generate a dependency tree and figure out which DLLs are missing. Most likely these will be native binaries, not managed. The usual suspects are the VC runtime, or MFC or ATL libraries but there could be others too. That's why you need to use a tool such as DependencyWalker or RedGate's Reflector to find all dependencies for the main executable.
It is because your external libraries are not compiled into final .exe file. So you either need to include them in the same folder or merge them into exe but this process can be quite tricky. Look for ILMerge. However it is way easier to just copy them with your exe file.

Getting GTK# app to run on windows

I am unable to get a GTK# app to run on windows, and I would appreciate any help I can get. I tried googleing around, but was unable to find a solution for my problem.
I wrote a small app in monodevelop using GTK#, which just opens a window with a button.
It runs fine on linux.
However I am unable to get it to run on windows.
I tried installing the GTK+ and the GTK# redistributables, the GTK# SDK, but it still does not work. And it does not show any exceptions thrown (in the command line), which would be helpful for debugging the problem.
After invoking the .exe, it sits there for a few seconds, and after that the shell reappears waiting for the next command, no output whatsoever.
Can anyone please walk me through what do I have to install on a virgin windows install to be able to run GTK# applications?
[edit] I understand mono is not needed to run them, and would like to avoid it, if possible.
Mono.Posix is a required part of the GTK# system - it contains the gettext library interface used to handle internationalized text. By writing in MonoDevelop, there is an option to allow internationalized text when creating the GUI interfaces, which will automatically call this library as part of the generated Build function (it seems to be called when setting user-displayed text, like the title of your form). This means you only need the Mono.Posix.dll library (which will work on Microsoft .NET), not the full Mono runtime.
The exception may be getting written to stderr instead of stdout, which the Windows command line does not show by default. Try:
myapp.exe 2> my.log
and see if that writes the error to a logfile called my.log.
Much like you I had an issue running on Windows 10. The issue in my case was resolved after another Google Result to a blog which mentions that the Mono.Posix references are indeed the cause. However, the blog indicated that you must also delete/convert global::Mono.Unix.Catalog.GetString (if you are using MonoDevelop's Stetic GUI creator). Those will be usually on your Labels.
ie:
dev#laptop:/tmp/myproject$ rg "global::Mono.Unix."
96: this.label2.LabelProp = global::Mono.Unix.Catalog.GetString("25 ROUNDS OF DEAF");
changes to
96: this.label2.LabelProp = "25 ROUNDS OF DEAF"
Since Stetic GUI creator usually puts Partial class files in the subfolder gtk-gui of the project directory. That's probably the best place to check for strays. Although ripgrep (the rg command used up there does recursive fast directory searching of all child folders so might as well use that)
After this you must install the GTKSharp Runtime for .NET (as of today it's file gtk-sharp-2.12.45.msi from mono-project.com for GTK+ also known as GTK2. GTK3 is different and it may be different for future Googler's)
After that recompile the application in MonoDevelop and you can run it on Windows
Source of steps: https://www.martyndavis.com/?p=432
Actually application can run without Mono (console apps, for example) on Windows, but if you have reference to Mono.Posix in code, it can use some platform-specific functions that couldn't be executed on Windows.
On Windows in MonoDevelop you can choose between frameworks (Mono or MS.Net). Try to recompile app on Win with MS.Net framework selected.

Mono on Windows (And General Questions)

I installed MonoDevelop on Linux and wrote a shell GTK+ application. When I compile and run the application shows up correctly in Linux. When I move the .exe over to Windows, I get a crash "Windows Not Responding", so there is no stack trace. Then I figured I had to install Mono For Windows, so I did that, but no luck - I still crash.
More generally, I am having a hard time picturing what is going on here. Please disabuse me of whatever I get wrong:
People who started the Mono project wrote their own compiler (likely from scratch) to compile C# code. They modeled this after looking at the .NET Framework
They also wrote a CLR for their mono implementation of the byte code. Any architecture that wants to run this code will need the Mono Runtime, as only the Mono Runtime can run the code.
Do I have it right? Why doesn't my program run? Thanks in advance.
Three things stand out to me:
GTK+. GTK isn't part of windows or mono on windows by default. You must install it separately.
Shell Application. That means something special in the windows world (an app that runs as part of (extends) the windows desktop shell, rather than as it's own program), and I wouldn't be at all surprised to hear that these don't work cross platform very well or even at all. I'd assume you mean "Console Application" instead, but then why the need for GTK+?
Mono. Make sure you invoke the program using mono. Just because mono is installed doesn't mean that's how your program will run vs Microsoft's own .Net implementation.
Hey, I'm only vaguely familiar with Mono myself but I -think- there is an analyzer tool that will show possible portability issues. If you run through that does it show any issues?
Few things you also check.
Is mono installer contains GTK# libraries.
If yes, check that is the assemblies are in GAC. The assemebiles names be gtk-sharp.dll, gdk-sharp.dll
If no, copy the referenced assemblies in your bin directory. you can see that in your mono develop project.
I assume you mean you wrote a GTK# application. In that case, to run it on Windows, you need to make sure you install "GTK# for .NET" from the Mono Downloads page
On Windows, you can capture a crash dump and see what causes the crash. For example, use WinDbg to execute this application.
http://www.microsoft.com/whdc/devtools/debugging/default.mspx

Categories

Resources