write code to find the opening windows on desktop - c#

Is there anyway to find what kind of window is opened on the desktop in real time?
It may need C# or C++. What I have found right now is to use C# Process.GetProcesses() method to find the opened windows on desktop.
But the output cannot be changed when the users have opened any new windows. So, is there any method I can use to track the opening windows in real time?
Also, is there any way to track the mouse movement on a specific window by using our web service?
Many many thanks!

I do agree with tnw that this sounds sketchy (possibly an example would be useful to help here). However, you want to look at the windows api calls:
GetForeGroundWindow
GetCursorPos
The first will only give you the current window, and not all open windows, but it sounds like this is what you really want anyway?

Source code to Windows keystroke logger that does this (in C++): http://16s.us/16k/
Edit: It shows the open window the user is typing in (in real time), but not all open Windows, only the active window in use.

Related

How to detect and handle external process

I'd like to know with a .net language(C# or VB) if I can detect when a new app is launched.
e.g: an user opens Firefox.exe from desktop (not from my app!), is it possible to detect this event from my app? Also is it possible to "pause" it before running it?
So for example when an user double-clicks an application, my app comes first and then it displays a message if he is sure to open that file with Yes and No buttons.
I don't need all code, I just need to know how to catch that event that can happen anywhere in system.
There are some Windows API functions in user32.dll that you can use in your .NET application through System.Runtime.InteropServices.DllImport to get the information you're looking for. Take a look at GetWindowThreadProcessId. For sample code, see http://www.c-sharpcorner.com/UploadFile/satisharveti/ActiveApplicationWatcher01252007024921AM/ActiveApplicationWatcher.aspx. Because applications tend to become active when they are launched, you'll find that the techniques used in that example will cause it to detect new app launches.
That doesn't answer the question about letting the user confirm whether they want to run an app, but it will get you part of the way towards what you are trying to do.

Is there a way to open Windows date and time settings dialog from a WinForms application?

I'm working on an embedded software, where we would like to avoid exposing Windows features and interfaces as much as possible. However, users need the ability to change the system date and time, and perhaps also timezone or daylight saving mode.
I thought I would create our own simple Form for that and I tried digging into Win32 API calls, but realized that it's not a simple way.
So I found that basically the easiest way here would be to invoke the well-known Windows dialog from the software and let users changing stuff there. I couldn't find anything about it so far. Is there any executable for it (such as gpedit.msc for example), or is there any API calls to show that window? If not, or in case of any contras which I didn't take into count, can anyone suggest a better way for this task?
EDIT: Thank you for the timedate.cpl tip, I will try it as soon as I can.
On my desktop version of Windows you can show that dialog by executing timedate.cpl. I've no idea whether or not that will work on your embedded Windows.
Yes there is. Use this code and upon button clicking it will work:
System.Diagnostics.Process.Start("timedate.cpl");
It should work. If you want to open other windows controls you only have to change "timedata.cpl" to the command of your choice.
Good Luck!
Try this:
Process.Start(System.Environment.SystemDirectory + #"\TimeDate.cpl");
But I don't know if it works on your embedded Windows...

Running just an app on windows 7

I have to run my C# application that I wrote it recently on several computers with window 7 operating system. Here is a big challenge with it, Application must run on startup and user must not be able to work with anything else such as windows hotkeys, other applications, some directories and etc.
Considering I don’t want to kill any process or service as less as possible, Please give me the best solution.
Thanks and waiting
Maybe A little more information will help
did you see Devices like ATMs or Medical devices that window is running on? Those devices don't allow user to manipulate with desktop or anywhere else, I want their solution. . . my Application Will run as a device handler(A Medical Device in Operating room).
You probably shouldn't write such an application in the first place (nagware?).
Anyway. I think what you are looking for is actually "kiosk" software. Here is blog entry that describes how to lock down the computer to effectively run in "kiosk mode".
Note however, that not every application can (or should) be used in that mode. Either because it has loopholes that still allow you to do thing (for example the file open/save dialog still allows you to create directories or navigate the file system), or because they were simply not designed with that goal in mind.
This sounds actually like a (very) bad idea to me...
You could probably hook every Keyboard event so you disable OS shortcut (Windows + D, Windows + E...). See here.
Also hide the Windows taskbar.
Make sure your application starts with Windows.
This sounds a bit hacky to me...
A program with the properties you mention is called a (very restrictive flavour of) shell. So you need to register your program as a shell (instead of explorer.exe) for the poor, poor user you want to restrict.
You might look into using a local group policy to enforce this restriction. Check this out.

C# how to run applications on the Desktop?

Is it possible to run applications on the Windows Desktop? I mean... that it can only be seen in the system tray, and it should be able to run alongside the desktop.
I have no idea how to code it, please help me. I'm kind of new to these things, I am supposed to create something like a "Stardock Fence".
I have seen some examples, but they seem buggy, any strong alternative I could use?
Process.Start() can be used to start a windows application/console app from another win application. There are parameters that allow you to optionally hide the UI as well.
You want your application's windows to be always-on-bottom. In other words, your UI will always appear to be beneath any other open window and just above the Desktop's icons.
To accomplish that, see these related questions.
Once you have a window always on the bottom of the z-order, you'll probably want to remove the non-client window chrome (titlebar/min/max/close buttons) so that your UI can look like a more integrated part of the Desktop. There's plenty of examples around; Googling is left as an exercise for the reader.
Not entirely sure what you mean by background. I expect you mean a Windows Service which is a project type in visual studio, or you might (less likely) mean running a background thread.
Actually, if you want it in the system-tray, you don't want it entirely in the background.
If it was to be fully "in the background", then your best bet is to have it as a service.
System tray icons need a window, but you can just make it non-visible and non-taskbar and that's fine.
A common combo is a service that does the actual heavy-lifting, and a hidden-window application with a systray icon that reports on the service's status (possibly making that same window visible when further interaction is needed).
You want to run your application in the background? Is it on a windows machine? If so then you want to look into running your application as a windows service. Here's an msdn link:
Introduction to Windows Services
There's examples in the article I think - if not it's a good starting point. You can configure services to start automatically on startup of the machine etc. Your application will then run in the background.
Basically you craete your application as normal and then host it in a windows service rather than say a console app or a winforms app.

Prevent Process 'A' from spawning Process 'B' which then shows up on top of what should be the "TopMost" Process 'C'

I have a windows form application which needs to be the TopMost. I've set my form to be the TopMost and my application works as I'd like it to except for in one case.
There is a 3rd party application (referred to as player.exe) that displays SWF movie files on a portion of the screen that popup on top of my application.
Using Process Monitor I determined that player.exe application calls
flash.exe <PositionX> <PositionY> <Width> <Height> <MovieFile>
in my case:
flash.exe 901 96 379 261 somemovie.swf
Since flash.exe is being spawned in a new process after my form has been set to the TopMost it is appearing on top of my application.
First thing I did was make my application minimize the player.exe main application window hoping that this would prevent the Flash from appearing also. But, unfortunately it doesn't... even with the window minimized whenever the flash movie starts it shows up at the pixel location (901,96). I then tried creating a timer to keep setting the form.TopMost property to true every 10ms. This sort of works but you still see a very quick blip of the swf file.
Is there some type of Windows API call which can be used to temporarily prevent player.exe from spawning child processes which are visible? I admit it sounds a little far fetched. But, curious if anyone else has had a similar problem.
Addendum:
This addendum is to provide a reply to some of the suggestions layed out in Mathew's post below.
For the emergency situation described in the comments, I would look at possible solutions along these lines:
1) How does the third party application normally get started and
stopped? Am I permitted to close it
the same way? If it is a service, the
Service Control Manager can stop it.
If it is a regular application,
sending an escape keystroke (with
SendInput() perhaps) or WM_CLOSE
message to its main window may work.
Easiest way to close the app is to CTRL-ALT-DEL, then kill process. -OR-
The proper way is to Hold ESC while clicking the left mouse button... then input your username and password, navigate some menu's to stop the player.
There is no PAUSE command... believe it or not.
I don't think using WM_CLOSE will help since minimizing the application doesn't. Would that kill the process also? If not, how do you reopen it.
2) If I can't close it nicely, am I permitted to kill it? If so,
TerminateProcess() should work.
I can't kill the process for two reasons. 1) Upon relaunch you need to supply username/password credentials... There may be a way to get around this since it doesn't prompt when the machine is rebooted but... 2) Whenever I kill the process in task manager it doesn't die gracefully and asks if you want to send an error report.
3) If I absolutely have to leave the other process running, I would try
to see if I can programmatically
invoke fast user switching to take me
to a different session (in which there
will be no competing topmost windows).
I don't know where in the API to start
with this one. (Peter Ruderman
suggests SwitchDesktop() for this
purpose in his answer.)
I got really excited by this idea... I found this article on CodeProject which provides a lot of the API Wrapper methods. I stopped implementing it because I think that in order for desktop's to work you must have explorer.exe running (which I do not).
EDIT2: On second thought... maybe explorer.exe isn't needed. I'll give it a try and report back.
Edit3: Was unable to get the code in that article working. Will have to put this on hold for a moment.
Answer Summary
As one might have expected, there is no simple answer to this problem. The best solution would be to problematically switch to a different desktop when you need to guarantee nothing will appear over it. I was unable to find a simple C# implementation of desktop switching that worked and I had a looming doubt that I would just be opening a whole new set of worms once it was implemented. Therefore, I decided not to implement the desktop switching. I did find a C++ Implementation that works well. Please post working C# virtual desktop implementations for others.
Setting the TopMost property (or adding the WS_EX_TOPMOST style to a window) does not make it unique in the system. Any number of topmost windows may be created by any number of applications; the only guarantee is that all topmost windows will be drawn 'above' all non-topmost windows. If there are two or more topmost windows, the Z-order still applies. From your description, I suspect that flash.exe is also creating a topmost window.
Aside from periodically forcing your window to the top of the Z-order, I think there is little you can do. Be warned, however, that this approach is dangerous: if two or more windows are simultaneously trying to force themselves to the top of the Z-order, the result will be a flickering mess that the user will likely have to use the task manager to escape.
I recommend that your program not attempt to meddle with other processes on the computer (unless that is its explicit purpose, e.g. a task manager clone). The computer belongs to the user, and he may not value your program more highly than all others.
Addendum:
For the emergency situation described in the comments, I would look at possible solutions along these lines:
How does the third party application normally get started and stopped? Am I permitted to close it the same way? If it is a service, the Service Control Manager can stop it. If it is a regular application, sending an escape keystroke (with SendInput() perhaps) or WM_CLOSE message to its main window may work.
If I can't close it nicely, am I permitted to kill it? If so, TerminateProcess() should work.
If I absolutely have to leave the other process running, I would try to see if I can programmatically invoke fast user switching to take me to a different session (in which there will be no competing topmost windows). I don't know where in the API to start with this one. (Peter Ruderman suggests SwitchDesktop() for this purpose in his answer.)
You can use the Process class to start flash.exe directly - and use an appropriate ProcessStartInfo settings to show the window in a hidden state - or with a WindowStyle of hidden or minimized.
You could also consider using the SetWindowsHookEx API to intercept the process start API calls, and when the process is flash.exe run some code to restore you window to top-most status.
Matthew's answer is excellent, but I suspect you may be asking the wrong question. Why does your application need to be topmost? If you're trying to create a kiosk or some such, then topmost is not the way to go.
Edit: After reading your response to Matthew's comment, I'd suggest creating a new desktop and switching to it before displaying your alert. (See CreateDesktop and SwitchDesktop in MSDN.)

Categories

Resources