Determine whether the class library is called from a windows based application - c#

This is based on C#. I am having a classlibrary in which I would like to know specifically if its being called from a windows based application
I searched a lot but all I found was code to know if it was called from a Console application.
I only want to know if its called from a windows based application or not
Please Help

If you want to know whether the application is running as a service or system app and thus cannot display a dialogue, test the state of Environment.UserInteractive.
There is no easy way to determine if the application is a windows or cmd-based application. For example, I might build a winforms-based application that doesn't create a window and instead runs as command-line style application by not opening a window.
Two (not necessarily reliable) ways of testing if it's a windows-based app would be to test Application.OpenForms (if it contains forms, then it's a windows app) or Environment.GetCommandLineArgs() (if 0 args, assume it's a windows app).
If you absolutely need to know whether the app is a console or windows one, then you need to step out of the world of managed code and start delving into the exe file using unmanaged C++ code. There is just one byte difference between the two in the PE header of the exe file: Subsystem is set to 2 for a windows app and 3 for a cmd app. See http://msdn.microsoft.com/en-us/library/ms809762.aspx and http://support.microsoft.com/kb/90493/en-us for details if you really want to do this.

Related

Running my UI app (WPF App) inside Worker Service with C# .Net Core

I'm new in programming with .Net and C# and, as said in the title, I have a WPF app which is accessible in a system tray icon and I want to run it a windows service.
Typically, I want an output like it was described in an answer provided in a discussion here.
If you want it in the system tray I think what you'll have to do is make it a Windows service. I've only written 1 Windows Service and that was years ago, but I believe that's what you'll have to do. If I'm correct about writing a Windows service, then what I would suggest you do is create a new Visual Studio solution and add two projects to it. One would be a DLL which would run as a Windows service. The second project would be a WPF project that will be your UI the user interacts with. Then you'll have to use some messaging system to communicate between the two. For the action messages that would mimic what Outlook does, I've used some WPF toast messages to accomplish that. If you Bing/Google "WPF toast popup" you'll get lots of results.
I have many searched in Internet and find some helpful answers like:
URL1
You can't, not directly, because the windows service will necessarily start when the machine does, not when a user logs in. The service will also be running in a different context, likely as a different user. What you can do is to write a separate system tray based "controller" that interacts with the service.
URL2
It needs some effort to achieve. Well, just two hints: 1) use static property System.Environment.UserInteractive to detect in which mode your application is running, see http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx; 2) get rid of app.xaml, because it will force starting WPF Application in all cases; instead, create and run and instance of System.Windows.Application (or better, a specially derived class) explicitly and only for interactive mode, see http://msdn.microsoft.com/en-us/library/system.windows.application.aspx.
And, I could not apply their instructions.
Thanks advance!

Alternate running a console Application as Exe and WinExe [duplicate]

This question already has answers here:
Show Console in Windows Application?
(12 answers)
Closed 6 years ago.
I have a C# console application that I am running with its output type set to "Windows Application" to prevent the console from being seen during normal use. However, I would like the option to alternatively run the program as a console application at will, in case the user wanted to troubleshoot and view the console's output.
Is it possible to pass a command-line argument to the executable to either run the application in "Console" mode or "Windows Application" mode depending on the user's desire? If not, is there any other way to change on the fly if an application will show the console or not?
I think you have an X-Y Problem. The root problem is that you want the user to be able to run either a console or WinForms program that both do the same thing. One solution would be to have a single program that can run as both, but as #roryap pointed out, this is impossible.
A second solution would be to put the business logic of your program into a separate library. You can then write a console program that accesses this library and a WinForms GUI that also accesses this library. The user then decides which program to run. This is a tried-and-true method of separating an application into multiple layers. I recommend you go this route. You'll find that your user-facing layers (console, WinForms) are small. In the future, if you want to make a Web front-end, or a WPF front-end, you only need to write the front-end part, the business logic layer won't change.
No, you can't. There's a fundamental difference between a console application and a winforms application that goes very deep. Once the application is compiled, it cannot be changed at run time.

Connection and communication between console and forms project in the same solution

I have a quite small (so far) project which consists of
Core
GUI
TUI
The TUI (console application) is communicating with another console application in order to send and retrieve certain information.
The purpose of the GUI is to make it easier.
However, I am stuck on how would I make my TUI communicate with my GUI. So far, I know I can start my TUI from the GUI like this:
System.Diagnostics.Process.Start(#"cmd.exe", #"/k C:\project\TUI\bin\Debug\TUI.exe");
But now, I do not have any reference to the console application and I do not know to send information forth and back. I tried to search for the answer, but my search would only lead to how to start them in a normal way (not both, together, communicating).
So all in all, my question is: How to start a console application from windows forms project so those two to be able to communicate?
You can use the Input and Outut Streams of the Process.
Read this :
https://msdn.microsoft.com/de-de/library/system.diagnostics.process.standardinput(v=vs.110).aspx

Create a process not an application in C#

I've created a simple application that I wish to be a process and not show up as an application inside taskmanager, simply because it is not an application. It was intended to be a process.
You might want to read about Windows Services.
Walkthrough: Creating a Windows Service Application in the Component Designer
Creating a C# Service Step-by-Step: Lesson I
You don't give much information about what you application does, but either Console or Forms will be displayed on the TaskManager, and even as a process it will be shown over the Processes tab so I'm not sure what are your intentions with this.

How to embed a Console application inside a Winforms application

I'm developing an application which acts as a GUI for Minecraft Server (runs as a console Java application).
I have finished it and I also want to add a console inside the Winforms application because I want to give users more control over the program. But using streams (Process.StandardOutput) I can't simulate a console as it sometimes changes the cursor position, clears the console, etc...
So, I want to embed the process into the application somehow. The first solution I tried was removing the borders and positioning it accordingly to the form's position but unfortunately I couldn't do it.
Any working code snippets would be greatly appreciated!
You cannot target both subsystem gui and console in the same module (msdn).
Instead, you could add a separate console application that uses SOAP to communicate with your application. Take a look at WCF to achieve this task.

Categories