Has my application been started by hand? - c#

I am developing a Forms application that is either supposed to start on Windows boot (by registering a key in the registry) or by the actual user.
In case the user starts it by hand (i.e. clicking the executable file), I want the form to show up. In case Windows triggered the event, I want the application to be hidden (into the tray bar).
Is there a way to programmatically determine which of those cases occured?

I would suggest adding a command line parameter, something like -minimized and pass that when starting it on boot.
See this thread if you are looking for how to read the arguments.

You can start you application upon Windows startup with a certain parameter, while a user will start it without this parameter.

The easiest way to do that, that I can think about, is:
When you register to run in Windows boot, pass a parameter (some default parameter), in case when user will run it, parameter will be absent.
In this way you would be able, depending on presence or absence of a paremeter, to determine the startup option of your program.

Related

How to launch my C# application when any application starts in Windows 8?

I want to launch my application, like the windows security prompt, before any application is launched in Windows 8.
Is there any event handler, which gets notified whenever any application is launched?
My use Case : I want an App similar to a child lock(Lets call it myCustomApp).
When any user runs a game(say Solitare), i want myCustomApp to check the process name, and kill the process immediately.
P.S. : i am quite new to programming.
Thanks in advance!
Is there any event handler, which gets notified whenever any application is launched?
Yes: you get use WMI events to detect new instances of Win32_Process.
But these are created with process creation, not before.
Doing something between the call to ProcessCreate that creates the new process, and the process actually being created is going to be, at best hard (you might need to do it in the kernel), but quite possibly impossible.
Why do you want to do this? What problem are you trying to solve? This really does sound like an X-Y problem.
Edit:
The term you need to use is hook: the interception of some operation on windows. Eg. "Is it possible to hook the creation of windows globally so I can control where the windows are placed on the screen?"
There is a direct way in the kernel: PsSetCreateProcessNotifyRoutine
There are helpers in user mode (eg. EasyHook), but these require injected your code into each process (which anti-malware tools are likely to object to).
But you should still start out by looking for better approaches to you underlying problem.

How to supply command line arguements to a form on launch?

I have a a simple GUI App, which need to perform a certain function on start-up, but not on manual launch from desktop or start menu. To incorporate this function, I thought of supplying command line arguments to the application, so that depending on the command line argument, we can differentiate between launch on start up and manual launch.
My Question is that how do I make sure that whenever the user clicks the applications icon on his desktop, the required command line arguments are given to the program.
I am using C# to program my application and want to run it on windows 7
The common design pattern for Windows applications which can be launched either at startup (i.e., automatically) or manually (i.e., when a user launches the application) is to pass a command line argument when the application is launched at startup but not when a user launches it manually. Why? Because you control the mechanism by which the application is started automatically (for example, because you create a registry key in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run). So, for example, you could add a value of "c:\program files\MyCompany\MyApp.exe" /Startup And then you could check for the startup argument in your code:
bool isStartupLaunch;
foreach (string arg in args)
isStartupLaunch |= (arg.toLower() == "/startup");
By contrast, you never really can control how a user is going to launch your application. Maybe they double click on a shortcut, but maybe they double click on your actual executable, or maybe they open a command prompt and launch that way. So you don't want to rely on getting a specific command line argument when the user launches your app. Much safer to look for the automatic launch, because you control how that is done, and therefore can control the command line arguments passed in.

Modify the default web browser for all child processes

On Windows, is there any way to modify the default browser in current application domain, but not system wide?
I'm developing an IDE using C# and I need that all child processes that try to launch the default web browser, see my IDE as the default web browser. But I need to do so in a way that it does not affect the rest of the system configuration.
Not sure if I'm explaining the issue clearly...
Anyone has any ideas how this could be done?
Actually there is no way to make it for a certain process. This value is stored in registry, and can be changed for a different user.
But in general are several options.
Run debuggee using a special user account, which has a different registry value for default browser.
Register your IDE as default browser in a reg key HKEY_CLASSES_ROOT\http\shell\open\command. If IDE could determine if the caller is not a debuggee, invoke original browser (don't forget to backup initial value).
If you use something like host process - intercept calls to RegQueryValue or other registry functions, and hijack the result for a debuggee. (if there is no host process, try to inject an interceptor). Alternatively you may intercept CreateProcess or ShellExecute.
I'm afraid that's not an option, but - you're able to register a dummy protocol (like myhttp://) and pretend it's http :) Your IDE will be a handler for it.
On which Windows are you? In windows 7 and 8 you can find the default program settings in the control panel. Just start the control panel and type 'default programs' in search box in the upper right hand corner.

How to force a process to be started by another process

I have an AutoUpdater application that launches another application. Is there anyway to force that second application to ONLY run if it was started by the AutoUpdater?
The problem is, some end-users will create a shortcut to the main app file on their desktop. This becomes a problem because it's supposed to check for updates to the app before it launches, and they don't receive our updates.
One thought I had was to create an IPC channel via WCF and issue a very simple command from the AutoUpdater to the other application. If the other app doesn't receive that command within 2 or 3 seconds it would close itself.
This seems like more code/overhead than what should be needed though. Is there an easier way?
Thanks!
Windows forms applications also have a main method, which can take parameters. You could read some parameter, and if it doesn't conform to your rules, you can either keep the form from opening (therefore the user wouldn't see a thing), or you can chastise the user, I mean, give a message that they shouldn't open your app that way. I think this is simpler than using WCF.
You can run your App by using
System.Diagnostics.Process.Start("", "TestString");
The first string is the path of App you want to start, something like "C:/AppName.exe" and the second string is any text you want but it must be the same string that you check in the 'if' below.
In the App you need to check the text given trough the second string.
static void Main(string[] args)
{
if(!args.Length == 1 || !args[0]=="TestString") Environment.Exit(0);
//↑ lenght check to avoid exception, then check for the string itself
//the OS gives the string to args, you don't need to take care about that
//rest of your code
}
I hope I helped to solve it.

Auto logon windows at a specific time using c#

Auto logon windows at a specific time. How can i do this in c# ?
Any ideas please...
Purpose : I need to perform some tasks at a specific time in midnight. which needs the windows to be logged on.
Take a look at the Scheduled Task. On the task panel you un-check the option "only after the current user logs on". That's it.
Update
So to make this with a little more explanation:
This was your question:
Purpose : I need to perform some tasks at a specific time in midnight. which needs the windows to be logged on
So to get this to work, you have to split your task a little bit up. At first write your task that should be performed (a batch-script, a self-written application, a third-party application with a configuration file or some command line parameters, etc.).
If you got your task up and you can run it on a single finger tip (e.g. click on a lnk file on your desktop, enter a single command on the command-line, etc.), it's time to automate your task for a specific time schedule. For this purpose Microsoft already provided a powerful tool, called Scheduled Tasks. It is located in the Control Panel. Here you can define Tasks which should be started with dozens of options and time plans. Just step through the wizard and after that make a double click or right click - Properties on the created task. Now step through all the TabPages and take a close look on all the available options. Everything you need can be solved here.
Update 2
So after reading your comment (and a few other):
If my system is locked, then i need it to get unlocked at a point of time
My first question would be: What do you mean with locked?
There are two states in which a system can be locked:
The system is freshly started, no session is running and you need to provide a username and a password to get a session to run.
A user has already logged in and the system is currently locked (cause the user pressed WinL, the screen saver is configured to lock, etc.).
The difference between those two states is, in the first you need to provide a username and a password, in the second one you only need to provide the password only.
What both have in common is you have to press CtrlAltDel to get the needed Dialog and this is not possible from an application.
If you really need to log-on or unlock the screen at a specific time you should start a search about gina.dll and how to replace or enhance it so that you can send a message to it, to do whatever you like. But this dll replacement can't be done in C#. It has do be done in C/C++. And i forgot, that the gina.dll approach only works on XP. For Vista/Win7 they changed the log-on process and the procedure to intercept it, but a search for the provided keywords should reveal the needed informations.
look at this thread.
You can replace your app with userinit.exe.
it runs before Explorer.

Categories