Close winforms system when closing windows session - c#

I have a winforms system that uses the same user and password to start windows session using the Active Directory credentials (This to not create a user and password for each system that is made).
What I want to do is that when the user for example closes his windows session or type the Ctrl + Alt + Del keys and select Lock or Switch User at the same time he closes the winforms system and returns the login the next time he logs in windows or unlock your session.
How can I do that?
The system is made in C # using VS2012 professional.
If you need more information, please let me know.
I hope you understand what I'm looking for.
EDITED
I have this code for Lock the session of windows, so I only look for when this event is launched in the current session and send call for closing my system or Form.
[DllImport("user32")]
public static extern void LockWorkStation();
LockWorkStation();

Related

How to launch an application on Login(Not Startup) in Windows Server

Ok i am working on an application which is basically an activity monitor.
This application will automatically log out a user if there is no activity for some time. I have successfully done the logout part.
But i am trying to automatically start the application when another or same user logged in again.
I tried startup code but it only works when windows starts up.
private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("My_App", Application.ExecutablePath);
}
So is there any way to know when someone login in the system.
I am using winform and c#.
Let me know if someone needs more information
Thanks.
You can place a shortcut to your application in the "Startup" folder for all users. The location of this depends on your operating system.
Windows 10: press windows key+R, then type "shell:common startup". Put a shortcut to your application in here.
On Windows 7 it was at "C:\Documents and Settings\All Users\Start Menu\Programs\Startup".
If it's neither of those, add a comment...
Each time someone logs on, the application will be run.

Send username and password to another application

I would like to send a user name and a password to another application, and process the input.
so I have Application A which has a window that requires a username and a password.
and we have Aplication B that is running. Application B needs to search for application A, login window, and send the user name to a textbox in it and the password, and then process those unputs through the Ok button.
Are there any libraries that can handle those sorts of requirements?
Any help weather it be website or dll references or examples would be great
NOTE:-
APPLICATION A is not something I built, or have access to its code or anything, I can start it, thats about it.
here is the process just to make things clear since some are confused:-
Application B is an EXE application, when clicked, it does some logic, then it starts Application A.
As soon as Application A starts, the user will prompted with a dialog box to enter user name, and password This is not something I made, it is what the application does. My question is can I access this dialog window, and send inputs to it.
FORM CODE
public partial class Form1 : Form {
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
public Form1() {
InitializeComponent();
var process = new ProcessStartInfo(#"arcmap.exe");
var pr=Process.Start(process);
SetForegroundWindow(pr.Handle);
SendKeys.Send("ne{TAB}ne{ENTER}");
}
}
}
This doesn't sound like a proper design strategy to me. Why not merging the two applications to one and passing the requested values between different application forms?
If for some reason you need to use two different applications, simply open application B AFTER the user has entered his login credentials in application A, and pass those values as parameters to your second app.
You can also consider using a TCP class in order to virtually connect the two of your apps using sockets.
You'll need to start Arcmap (this is a fairly easy task using Process.Start(string path);, then give it some time to boot using Thread.Sleep(int miliseconds), and then it gets tricky.
You'd have to get the Arcmap process (probably by name) and set it as foreground window by importing this method:
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
And then calling it this way:
Process process = Process.GetProcessesByName("arcmapProcessName").FirstOrDefault();
SetForegroundWindow(process);
And later you just send keys using
SendKeys.Send("login{TAB}password{ENTER}");
Then you would have to reference the SendInput function to programatically inject keyboard keypresses into the input stream. Perhaps, since it's a lot of hassle to reinvent the wheel and you're asking for an external library anyway, you could use C# Input Simulator.

How to detect when an internet-browser is open in Windows Forms and do an action if an URL was fired?

I want to write a program which recognizes when a browser is open and which do every time an action, when the user went to a website. For example:
The program is running as a system tray and starting automatically on windows startup. (this works)
Now the program runs an function, if the client open a random internet-browser (IE, Chrome, ...) which have the example-code MessageBox.Show("You opened a browser!").
If the user types for example "www.google.com" in the address bar and push [enter] the program should open an example-function like MessageBox.Show("You entered " + enteredURL) before the Website is loaded.
Thanks in advance for your help!
Take a look at the Navigating event:
Occurs before the WebBrowser control navigates to a new document.
It is possible to connect to an existing instance of IE, but you'll need to work with underlying the COM API (see here).
For other browsers there is no general mechanism: you'll need to work out if some API even exists browser by browser.

How do I lock and force the user to re-enter one's windows password?

I want to lock the accessing of current operating user, as if the user
clicked on Start → pointed to shutdown → click on LOCK
How to do that in C#?
http://jessn.blogspot.com/2009/05/lock-my-computer-programatically-in-c.html This article says it best:
A much better design that isn't hard coded to your windows system
paths, etc:
using System.Runtime.InteropServices;
[DllImport("user32.dll")] public static extern void LockWorkStation();
Then just call LockWorkStation();
System.Diagnostics.Process.Start("shutdown.exe -l");
it will LOG OFF your computer and now if the account has secure with some password then windows will ask for password otherwise not.

CreateProcessAsUser() not showing CreateWindow of running Process [C#]

My system has 2 accounts (USER and ADMIN) and a service (Service.exe) running under LocalSystem.
The user logs into the USER account, the LocalService can then launch a process (CreateProcessAsUser(...)) as the ADMIN user.
The process the Service runs (Tool.exe) is a legacy C++ application that performs a job and also displays information to the user by using CreateWindow(...), but when launching it by the Service the Window does not show...
When the process is created by the Service I first load the Profile & Environment of the ADMIN user so that the correct context is used ...
(was still hoping the Window would display to USER)
Now, initialy I thought this would cause a problem as ADMIN is running the process so why would the CreateWindow(...) output in the USER desktop, so I tried with a simple command-line test.exe app and when launched as ADMIN the Command Prompt window appeared - so why does that work fine where-as the CreateWindow(...) doesn't display correctly?
Any clues on how I can get the STATUS (using CreateWindow in Tool.exe) running under ADMIN to show in the USER logged-on session?
Can I use the ENVIRONMENT somehow, I tried the following thinking it might work but didn't:
startInfo.lpDesktop = #"WinSta0\Default";
startInfo.dwFlags = STARTF_USESHOWWINDOW;
startInfo.wShowWindow = SW_SHOW;
The Window is created as follows (in Tool.exe):
HWND hwnd = CreateWindow
(
"Tool",
"Tool WINDOW",
WS_POPUP | WS_VISIBLE,
0,0,uWidth,uHeight,
NULL,
NULL,
hInstance,
Text
);
Is the lpDesktop wrong (not exactly sure how this works)?
I know how crazy this sounds - I just would rather not have to launch another ToolDisplay.exe as USER which communicates with Tool.exe as ADMIN as a display when the Tool.exe used to handle everything on its own - so checking to see if there isn't some nice way to handle this...
Any help would be much appreciated...
Thanks,
[Simplified Question:]
My system has 2 accounts (USER and ADMIN), the user is always logged on as USER but at some specific times a process (Tool.exe) is launched under the ADMIN account (by a LocalSystem Service using CreateProcessAsUser(...)), almost everything works fine except for the fact that the process (Tool.exe) is supposed to display status to the user using CreateWindow(...).
When Tool.exe is running (as ADMIN) and the user is logged-on as USER the window is not shown (obviously)...
Is there a way to show the window of Tool.exe running under ADMIN to the user logged-on as USER?
Any help would be much appreciated...
Thanks,
Just in case you want a window / GUI spawned by a service to be visible,
Go to-> run prompt -> services.msc -> go to your service's property, check "Allow service to interact with desktop".
Then restart your service.
Note that in Vista, the prompt will NOT be visible even after above. This is due to sessions issue. However there is a service which helps support prompts from services (I think it is Interactive Services Detection service).

Categories

Resources