Windows Phone c# how to catch global hook - c#

I have simple app:
public MainPage()
{
InitializeComponent();
CameraButtons.ShutterKeyPressed += OnButtonFullPress;
}
private void OnButtonFullPress(object sender, EventArgs e)
{
//camera button pressed
}
When my app is active, all is great, but, when my app is not active, i cant catch, when camera button were pressed.
Is this possible with default SDK?

There is a way to set your application as a default camera app on windows phone 8, but this feature is only available for 'special' developers like Microsoft, Nokia or other hardware companies. So it's not possible for you, most probably.
http://forums.windowscentral.com/windows-phone-8/234751-changing-default-camera-app.html
Also if you look at the application lifecycle you'll see that after user minimizes your app it might be tombstoned and even unloaded from the memory. So, you loose the control over the device resources. That means that you can't get any events except the awake event.
The third option you have is to run the app under the lock screen, but it doesn't fit you as well. There is a limited set of api's you can use in this mode. (Location and Audio).
So I'm sorry, but you can't do anything so far.

Unfortunately, this is not possible for third-party developers. You can only access the hardware buttons while your applications is running.

Related

How to automatically close uwp applications

I have two questions, first: how to let the uwp application perform a function, automatically close the uwp through the code instead of letting the user click the close button on the top right; second: how to minimize the uwp application to the taskbar at startup, not directly on the desktop, thank you.
You can use the static exit method:
public void CloseApp()
{
CoreApplication.Exit();
}
or Use the non-static method ():
public void CloseApp()
{
Application.Current.Exit();
}
One thing you have to keep in mind is that using CoreApplication.Exit and Application.Current.Exit closes the app, but it does so without going through the normal app suspension - for the system it appears as a "unexpected termination". I also think the certification guidelines are against this approach.
As for taskbar minimization - this is currently not supported. If you need this functionality, you will need to build a WPF app packaged as UWP app using the Desktop Bridge.

How to find Windows Universal app windows from C#

I want to write my own program that lets me select one Window of ALL open windows and set the state of this window to TOP, so that this selected window will always show on top!
The problem... since Windows 8 there are APPs and actually the process comes up in the process explorer but my selection of my tool doesn't list it. (Its like there is no app)
My source looks like:
private void refreshWindowList(object sender, EventArgs e)
{
windowList.Items.Clear();
foreach (Process p in Process.GetProcesses().Where(pp => pp.MainWindowHandle != IntPtr.Zero && pp.ProcessName != "explorer"))
{
windowList.Items.Add(p.ProcessName);
}
}
This function is called when I open my combobox and actually refresh the Items every time when I view the list.
I find normal programs, but is there a way to find Win 8/10 apps ?
EDIT to clarify: Normal processes like notepad can be found. But Windows Universal apps like e.g. netflix can't. At least I don't know how to find them.
Some applications, e.g. Netflix, are written using HTML and JavaScript. These apps are hosted by WWAHost:
[...] Microsoft explains WWAHost as β€œan Internet Explorer-based rendering platform.”
You can check if this is the case for an app by right clicking it in the Task Manager and choosing Go to details:
To find out which app is being hosted, you can use MainWindowTitle
Console.WriteLine(p.ProcessName); // WWAHost
Console.WriteLine(p.MainWindowTitle); // Netflix

windows inputlanguage changed c#

I am working on a simple program in Visual studio(windows application) and I need to know when the user is changing the keyboard language(I don't care what was the language or what it is now).
I have tried this code:
private void MainForm_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
{
}
But it will only get the language change in the form when the form is active and focused! Example: I'll open Google chrome and change the language, this function will not be called.
I need to get the specific windows keyboard changed. Not the form inputlanguage changed..
it's very important for me I am breaking my head about a week for this so please help.
ty guys.
As far s I know, in Windows, input language is specific for process. So you get the InputLanguageChanged event for your application. If you want to be notified of language change globally, you should do it on your own. For example periodically check the active window and get keyboard layout using GetKeyboardLayout API function.

child controls disappear when running already running app

bit of a strange one... when my app is running I add a custom control to a StackPanel on the click of a button like so...
void btnAddPlayer_Click(object sender, EventArgs e)
{
PlayerControl sbItem = new PlayerControl();
ctlPanel.Children.Add(sbItem);
}
(ctlPanel is a StackPanel PlayerControl is a standard Control inherited from UserControl)
So I add one or two, not a problem... if I multi-task on the phone to a different app, then multi-task back to my app, not a problem... however, if I multi-task away (or hit the windows phone button) and then instead of multi-tasking back, I just click on the icon (as if Im running the app again) it reloads my app but without any PlayerControl's in the StackPanel ... and while debugging, it doesnt hit the InitializeComponents() method in the pages constructor (of course this could be because it might not debug it when you run it straight from the menu)
Anyone know if theres a way to only allow an app to be run once (and dont restart it if the user runs the app again)????
It does not work that way. You can save the "state" of your app and load that when you start the application. Look at "Tombstoning" for windows phones. Example, you can save in the local storage the current screen id and any other necessary data and when you launch the app, if you have something in local storage you load that screen, else you start with your homepage.

Block Control+Alt+Delete

I am doing an Online Quiz project in C#. The test client is a Windows Desktop Application running on Windows XP. I need to block the control+alt+delete key combination to prevent students from minimizing/closing the application.
Using PInvoke is okay for me.
I know this is definitely possible because I have seen three applications doing this. They are all proprietary, so I have no way of knowing how it was done.
I found a very ugly way of doing this (which works well).
If I open taskmgr.exe exclusively, then nothing happens when the user presses Ctrl+Alt+Del.
FileStream fs = new FileStream(System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"), FileMode.Open, FileAccess.ReadWrite, FileShare.None);
What I like the most about this solution that it has no permanent effects. For example if the application gets killed, then Ctrl+Alt+Del will just work again.
Drawback: One must have the Welcome screen enabled or Windows Security will popup instead of Windows trying to open taskmgr and silently failing. (β†’ It also won't work if the machine is in a domain, because being in a domain disables the Welcome screen.)
(Of course this won't work on Vista or W7.)
For Windows XP, the correct way to do this is to create your own Graphical Identification and Authentication Dynamic Link Library, or gina.dll for short. Here's an MSDN article about it. This DLL exports a set of functions that interact with the Winlogon process and provides the user interface to logon requests - the Secure Action Sequence event. The main logon request is the ctrl-alt-delete response. The standard gina.dll invokes the logon screen or the task manager/logoff dialog. It's not too difficult to create your own gina but it does require C/C++ coding and not C# and it is quite easy to make the system fail to boot. This does not stop people from pressing F8 at boot up and selecting the Safe Boot option, which won't load the custom gina.dll.
EDIT: I should also say that you don't need to implement all the functions the gina is required to implement, you can dynamically load the previous gina.dll and pass all the calls you're not interested in to the old gina.dll.
EDIT 2: This does not work with Vista/Win7 as they changed the architecture of the logon process. It is still possible to disable ctrl-alt-delete in Vista/Win7 but it requires a different mechanism - there are MSDN articles about it somewhere.
EDIT 3: Here's a ZIP file containing the source code to make a gina.dll It was built using DevStudio 2005. The file GinaInterface.cpp details the steps needed to install the new gina.dll library. This will disable the "Welcome" screen and replace it with the 'press crtl-alt-del' to login dialog. As it stands, there is no difference between this and a standard gina.dll, all the gina related calls are passed through to the original gina.dll file (called msgina.dll in the Windows\System32 folder). To disable the ctrl-alt-del key press, update the function WlxLoggedOnSAS in GinaInterface.cpp. To stop ctrl-alt-del whilst your application is running, you could create a named mutex (CreateMutex) and test for its presence in the gina.dll, stopping ctrl-alt-del if the mutex exists.
As other people have mentioned it's very hard to block Ctrl-Alt-Del, as it's a fundamental part of Windows security.
However you can block what can be done after Ctrl-Alt-Del has been pushed by adding the following registry keys.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System
DisableTaskMgr
DisableChangePassword
DisableLockWorkstation
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
NoLogoff
If you do this then when you push Ctrl-Alt-Del you will get a dialogue like the following (under XP):
It's not fool-proof, but it stops the user from doing some basic things.
Update: Just realised that this also blocks Ctrl-Shift-Esc, which I didn't realise before.
You can pre-run the hidden process taskmgr.exe
ProcessStartInfo psi = new ProcessStartInfo(System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"));
psi.RedirectStandardOutput = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
processTaskMgr = Process.Start(psi);
I achieved a similar goal, but with a different tactic in a Time Tracker tool I whipped up. This will give you a form which takes over the screen - doesn't allow windows to appear on top of it and will shoot down task manager if it is started.
Set your form TopMost = True.
override the Form.OnLoad method like so:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Location = SystemInformation.VirtualScreen.Location;
this.Size = SystemInformation.VirtualScreen.Size;
}
Create a timer, with a 500 millisecond interval, which looks for, and kills "taskmgr.exe" and "procexp.exe".
Override the Form.OnFormClosing:
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing || e.CloseReason == CloseReason.FormOwnerClosing)
{
MessageBox.Show("Nice try, but I don't think so...");
e.Cancel = true;
return;
}
base.OnFormClosing(e);
}
Override OnSizeChanged:
protected override void OnSizeChanged(EventArgs e) {
base.OnSizeChanged(e);
this.WindowState = FormWindowState.Normal;
this.Location = SystemInformation.VirtualScreen.Location;
this.Size = SystemInformation.VirtualScreen.Size;
this.BringToFront();
}
There're three ways of doing it (registry, administrative templates and hooks) described in this article.
The code is in C++, but it will be easy to port it to C# with P/Invoke.
What do you really want to do ? Disable the task manager ?
Hive: HKEY_CURRENT_USER
Key: Software\Microsoft\Windows\CurrentVersion\Policies\System
Name: DisableTaskMgr
Type: REG_DWORD
Value: 1 disable
But the user can still close your app with a third party task manager.
The proprietary application might "disable Ctrl+Alt+Del" using this registry key :
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\taskmgr.exe, Debugger, Hotkey Disabled
According to the Windows Internal book (4th edition), Ctrl-Alt-Del sequence cannot be intercepted by non-privileged applications. Also, it is said that this particular sequence cannot be intercepted and that the Winlogon process will always receive it (page 529).
I never tried to do this, however, but I would trust the book :)
As pointed out in the other answers, there is no secure way of accomplishing this without checking each student's computers, because they could always run a VM.
As an alternative, have you considered burning your app to a LiveCD and requiring students to boot the LiveCD?
That way, you control their OS as long as they are running your app. Once done, they can reboot and everything's back to normal.
Of course, students could reboot their laptops inbetween, but that would probably take long enough to be noticed by the supervisors.
The VM solution would still trick this, so you'd need to make sure everyone really boots from the CD; otherwise I cannot think of any way around this.
As a bonus, you'll be isolated from any weird OS problems on the student's laptops (maybe some installed Linux or OS X ;-) ) .
Alternate solution:
Make the app so you never need to use the alt or ctrl key.
Then, take out the Ctrl (or alt) key and put paper on the contacts.
Tada!
(just use another keyboard for maintenance)
You'll also want to block alt+F4 with something like this:
private void form_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
e.Handled = true;
base.OnKeyDown(e);
}
I wonder if you could install a filter on the keyboard driver?
I know on USB interfaces, you can install an upper or lower filter to the USB data to intercept it as it arrives on the computer, and perhaps a similar approach could be taken. Essentially, you would modify the key combinations coming from the keyboard as long as your testing application is running.
I did find an 'UpperFilters' registry key defined for my keyboard...
This USB sniffer comes with source code that implement a filter. It may be usable in the context of a keyboard sniffer / modifying filter. (Download link)
I would suggest other way to disable Task Manager, works well with Windows 7 yet it just affects task manager option. The solution is to set following registry key to irrelevant value, like "C:\"
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\taskmgr.exe\Debugger
You can also use this hack to start your own task manager, just by setting Debugger key to full path of your application. I have discovered this by analysing how ProcessExplorer replaces TaskMgr.
A very dumb solution (which assumes that the machine is dedicated your software) is to remap the Alt key (to nothing).
Drawbacks:
Permanent effect
Requires a restart/logoff to activate
Works on xp, Vista, W7.
You can use SharpKeys to remap/disable any key.
For info on what registry key SharpKey changes see this. One can also remap on a per user basis.
Warning: If you disable the Alt key and
login requires you to press
Ctrl+Alt+Del than you won't be able to
login. :)

Categories

Resources