Can I disable window autoplay function programatically with C#/.NET? - c#

Does anybody know a way to deactivate the autoplay function of windows using c#/.NET?

A little summary, for all the others looking for a good way to disable/supress autoplay.
So far I've found 3 methods to disable autoplay programatically:
Intercepting the QueryCancelAutoPlay message
Using the Registry
Implementing the COM Interface IQueryCancelAutoPlay
In the end I chose the 3rd method and used the IQueryCancelAutoPlay interface because the others had some signifcant disadvantages:
The first method
(QueryCancelAutoPlay) was only able
to suppress autoplay if the
application window was in the foreground, cause only the foreground window receives the message
Configuring autoplay in the registry worked even if the application window was in the background. The downside: It required a restart of the currently running explorer.exe to take effect...so this was no solution to temporarily disable autoplay.
Examples for the implementation
1. QueryCancelAutoPlay
Suppressing AutoRun Programmatically (MSDN article)
CodeProject: Preventing a CD from Auto-Playing
Canceling AutoPlay from C#
Note: If your application is using a dialog box you need to call SetWindowLong (signature) instead of just returning false. See here for more details)
2. Registry
Using the registry you can disables AutoRun for specified drive letters (NoDriveAutoRun) or for a class of drives (NoDriveTypeAutoRun)
Using the Registry to Disable AutoRun (MSDN article)
How to Enable / Disable Autorun for a Drive (using Registry)
Windows 7 AutoPlay Enable | Disable
3. IQueryCancelAutoPlay
Reference for the IQueryCancelAutoPlay interface on MSDN
IQueryCancelAutoPlay only called once? (Example implementatio, also read comments)
AutoPlayController (another implementation, not tested)
Some other links:
Enabling and Disabling AutoRun (MSDN article)
Autoplay in Windows XP: Automatically Detect and React to New Devices on a System (an old but extensive article on Autoplay)

RegisterWindowMessage is a Win32 API call. So you will need to use PInvoke to make it work..
using System.Runtime.InteropServices;
class Win32Call
{
[DllImport("user32.dll")]
public static extern int RegisterWindowMessage(String strMessage);
}
// In your application you will call
Win32Call.RegisterWindowMessage("QueryCancelAutoPlay");
From here (The Experts-Exchange link at the top). There is additional help on that site with some more examples that may be a little more comprehensive than the above. The above does however solve the problem.

Some additional links that might be helpful:
Preventing a CD from
Auto-Playing shows some example
vb.net code, showing the usage of
"QueryCancelAutoPlay" on CodeProject.
Enabling and Disabling AutoRun on MSDN.

Try this code work for me :) For more info check out this reference link : http://www.pinvoke.net/default.aspx/user32.registerwindowmessage
using System.Runtime.InteropServices;
//provide a private internal message id
private UInt32 queryCancelAutoPlay = 0;
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
/* only needed if your application is using a dialog box and needs to
* respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
*/
protected override void WndProc(ref Message m)
{
//calling the base first is important, otherwise the values you set later will be lost
base.WndProc (ref m);
//if the QueryCancelAutoPlay message id has not been registered...
if (queryCancelAutoPlay == 0)
queryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay");
//if the window message id equals the QueryCancelAutoPlay message id
if ((UInt32)m.Msg == queryCancelAutoPlay)
{
/* only needed if your application is using a dialog box and needs to
* respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
SetWindowLong(this.Handle, 0, 1);
*/
m.Result = (IntPtr)1;
}
} //WndProc

Related

C# Detect Keystrokes and Send to Background Windows

I've looked around for six hours today in search of a method to complete the task I'm looking to accomplish. However with little luck every method I've tried has come out not working.
So the program I'm working on is a multiboxing application for video games. Essentially I want to have my created application running in the background. The user will check on checkbox's to state which keys they want to be captured, so not every key is being captured. Then while they are playing the main game, the application will send the keys that are checked to the games running in the background.
I've tried global hotkeys however never could get more than one key working. I've also tried to hook keys but for some reason, couldn't get that functional. I also dabbled into sendmessage with little luck there either.
Was just curious if anyone else had some ideas for going about doing this. To give an example of another program that does this same thing would be HotKeyNet, KeyClone, and ISboxer. I know there are more out there but that gives you an idea of what I'm trying to do with my application.
Alright, after quite a bit of research into different methods for sending keystrokes and reading keystrokes. I finally was able to splice to different types of coding together to provide the results I was looking for.
I'm posting up the answer, so anyone that is looking for the answer to this question later down the road has it available to them.
My two references for splicing this code together are the following:
http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
Send combination of keystrokes to background window
I used the global low level hook and postmessage for sending keystrokes to the background application.
So first you will need to follow the instructions from the first
link, to get the starting code working.
Download the working source code from link one, and use the
globalKeyboardHook.cs in your application.
Then in references place the following:
using System.Runtime.InteropServices; //Grabs for your DLLs
using Utilities; //Grabs from the file you added to your application.
Now you will want to place the following code in your class:
globalKeyboardHook gkh = new globalKeyboardHook();
[DllImport("user32.dll")] //Used for sending keystrokes to new window.
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)] //Used to find the window to send keystrokes to.
public static extern IntPtr FindWindow(string className, string windowName);
Now go ahead place your keystrokes you want to be grabbed this I
found is best in Form1_Loaded:
gkh.HookedKeys.Add(Keys.A);//This collects the A Key.
gkh.HookedKeys.Add(Keys.B);//This collects the B Key.
gkh.HookedKeys.Add(Keys.C);//This collects the C Key.
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown); //Event for pressing the key down.
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp); //Event for the release of key.
After that you will want to go ahead and place in the following in
your code as well:
void gkh_KeyUp(object sender, KeyEventArgs e) //What happens on key release.
{
lstLog.Items.Add("Up\t" + e.KeyCode.ToString());
e.Handled = false; //Setting this to true will cause the global hotkeys to block all outgoing keystrokes.
}
void gkh_KeyDown(object sender, KeyEventArgs e) //What happens on key press.
{
lstLog.Items.Add("Down\t" + e.KeyCode.ToString());
e.Handled = false;
}
Once that is in place just put this little bit in the gkh_KeyDown to
get your keystrokes to send to another window of your choosing:
const uint WM_KEYDOWN = 0x100;
IntPtr hWnd = FindWindow(null, "Example1"); //Find window Example1 for application.
switch (e.KeyCode)
{
case Keys.A: //Makes it so it only sends that key when it's pressed and no other keys.
if(chkA.Checked == true)
{
PostMessage(hWnd, WM_KEYDOWN, (IntPtr)(Keys.A), IntPtr.Zero); //Sends to key A to new window assigned hWnd which equals Example1.
}
break;
}
}
The code that I have provided is setup so people can use checkbox's to tell the program which keys they want to send over to the second application.
If you have any questions regarding to this post just let me know, and I will do my best to walk you through the process. Hope this helps someone out later down the road.
My suggestion is to go with "mapped memory" (in operating system concepts: shared memory)
First process creates (may be your check state program) creates a mapped memory and writes values to it.
All other game process reads the value from memory map created by first process.
Here is a very nice example regarding how to do it.
https://msdn.microsoft.com/en-us/library/dd267552(v=vs.110).aspx

Send Windows key keypress into application [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect the currently pressed key?
(EDIT: For what it's worth, this is not a duplicate... not sure why it was voted as such)
I have a small timer application that we use in the office to track the hours spent on a project. There is a start and stop button, and a project and task fields. When we go on break and to lunch and other things, we stop the timer for that project, then restart it. This is a repetitive task that generally involves digging the window out from behind several other Windows, then the same after the break.
What I want to do is assign WindowKey+W to the work timer application and have it not only bring the timer application to the front and focus it, but also have it toggle the Start/Stop.
I have tried a number of searches, but I can't seem to narrow down the examples to what I want. I do know that you can open the properties of a Windows shortcut and assign a shortcut key to launch a program, and (I guess?) if you have that app already open and it is set to allow only one instance of the program that it will bring that program to the front??? maybe..
Anyway.. but that method will not accept the WindowsKey as a valid key combo. And I don't know if it can somehow pass that key combo in to the program.
I appreciate any help or direction here!!
EDIT - Answer Update
Thank you #huadianz for your answer! I converted your code to VB:
Public Const MOD_WIN As Integer = &H8
Public Const KEY_W As Integer = &H57
<DllImport("user32.dll")> _
Public Shared Function RegisterHotKey(hWnd As IntPtr, id As Integer, fsModifiers As Integer, vlc As Integer) As Boolean
End Function
<DllImport("user32.dll")> _
Public Shared Function UnregisterHotKey(hWnd As IntPtr, id As Integer) As Boolean
End Function
Public Sub RegisterKeys()
RegisterHotKey(Me.Handle, 1, MOD_WIN, KEY_W)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If (m.Msg = &H312) Then
Me.TopMost = True
Me.PlayPauseTimer()
Me.TopMost = False
End If
End Sub
On an interesting note, Me.BringToFront() would not actually bring the application to the front in this scenario in Win7, nor did Me.Focus(). However, Me.TopMost = True worked, but it has the secondary effect of making the window always on top. Setting it to True, toggling the timer, then setting it back to False works great!
If you want full operating system intergration, you can hook into the kernel input functions by using PInvoke.
What you are looking for is the explorer.exe Windows API function, described in detail here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx
Using PInvoke, you can invoke this C++ function
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public const int MOD_WIN = 0x00000008;
public const int KEY_W = 0x00000057
public static void RegisterKeys()
{
RegisterHotKey((IntPtr)this.Handle, 1, MOD_WIN, KEY_W);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
this.Visible = !this.Visible;
}
I would also suggest looking at Auto Hotkey: http://www.autohotkey.com/. If all you're after is sending a window to front on key-press then you may find it faster to learn AHK and write a script to do so (be only 1 or 2 lines) than writing something in C# or VB.
*edit*As regards pressing a button automatically, if it's set as the default button then you can just send the enter keystroke to it. Otherwise you may need to send a mouse click even to the window. Neither is particularly difficult once you have the first part done.
#W::MsgBox "This is a message box. You just need to use something like send to send keystrokes and other commands to the window you want to control, instead of the this message box."
You can use Windows SendKeys. If you look at that link it shows the codes and syntax for applying a ctrl, alt or shift key. I imagine you can send these keys to your application to simulate a shortcut press.

How to prevent Windows from entering idle state?

I am working on a C# application which runs in the background without any Windows control.
I want to notify Windows that my application is still alive to prevent Windows from going into the idle state.
Are there any APIs available to call from my application which notify the Windows OS that my application is still alive?
Thanks in advance.
You've to use SetThreadExecutionState function. Something like this:
public partial class MyWinForm: Window
{
private uint fPreviousExecutionState;
public Window1()
{
InitializeComponent();
// Set new state to prevent system sleep
fPreviousExecutionState = NativeMethods.SetThreadExecutionState(
NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
if (fPreviousExecutionState == 0)
{
Console.WriteLine("SetThreadExecutionState failed. Do something here...");
Close();
}
}
protected override void OnClosed(System.EventArgs e)
{
base.OnClosed(e);
// Restore previous state
if (NativeMethods.SetThreadExecutionState(fPreviousExecutionState) == 0)
{
// No way to recover; already exiting
}
}
}
internal static class NativeMethods
{
// Import SetThreadExecutionState Win32 API and necessary flags
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}
You have a couple of options:
Use SetThreadExecutionState, which:
Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.
Where you could use the ES_SYSTEM_REQUIRED flag to
Forces the system to be in the working state by resetting the system idle timer.
Use SendInput to fake keystroke, mouse motion/clicks
Another alternative would be to change your app to be a Windows service.
SetThreadExecutionState example
// Television recording is beginning. Enable away mode and prevent
// the sleep idle time-out.
SetThreadExecutionState(
ES_CONTINUOUS |
ES_SYSTEM_REQUIRED |
ES_AWAYMODE_REQUIRED);
// Wait until recording is complete...
// Clear EXECUTION_STATE flags to disable away mode and allow the system
// to idle to sleep normally.
SetThreadExecutionState(ES_CONTINUOUS);
You can use SetThreadExecutionState described here:
SetThreadExecutionState Function
Since it is a Win32 API function, to use it from C# you'll need to PInvoke it. The steps are described here, including a sample method PreventSleep to temporarily disable sleep mode:
PInvoke.net: setthreadexecutionstate (kernel32)
I don't think there's any way to do this directly in managed code.
A quick search reveals this post from 2 years ago. Basically you'd need to do some interop to call a raw windows API.
Here is SetThreadExecutionState C# implementation

Alternative to GetWindowModuleFileName to get other processes ApplicationFileName?

In order to get the application name of the foreground Window (or the name of application file) I want to use GetActiveWindow with GetWindowModuleFileName.
I found a similar question relating to GetWindowText here
That implementation of GetWindowText works fine, but GetWindowModuleFileName only returns a value for visual studio (when I click inside the devenv) for all other applications it stays blank.
Any hint how I can find out what goes wrong? Might this have to do with permission/security of my application querying the applicationfilename of another process?
EDIT: http://support.microsoft.com/?id=228469 looks like this doesn't work under Win >=XP
Any alternatives how to get the application file name?
In order to get the application name of the foreground Window (or the name of application file) I want to use GetActiveWindow with GetWindowModuleFileName.
... querying the applicationfilename of another process ...
In my opinion your problem with use of GetActiveWindow() function. It is used for gathering information from the calling thread/process only. If calling thread is inactive GetActiveWindow return 0;
From MSDN:
GetActiveWindow Retrieves the window handle to the active window attached to the calling thread's message queue.
Try to use GetForegroundWindow() function instead of GetActiveWindow()
By chance do you have UAC turned off?
Starting with Vista, if your code touches an HWND in another process, your process needs to be run at the same privilege level.
In other words, if the window is hosted in a process running as administrator, your app must also run as administrator.
I found a workaround using this:
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
IntPtr handle = IntPtr.Zero;
handle = GetForegroundWindow();
uint processId;
if (GetWindowThreadProcessId(handle, out processId) > 0)
{
Console.WriteLine(Process.GetProcessById((int)processId).MainModule.FileName);
}

c# SendKey to a hidden window

I have a window which is hidden and I would like to send a keypress to it. An example of what I'm trying to achieve is an app that will send the key F5 to a web browser which wasn't the active window. The web browser would know to refresh the current page when the F5 keystroke is received.
I would also like to send a combination of keys to an application, e.g. Ctrl+S. One example of this usage could be a timed auto-save feature to use with applications which don't have autosave. This would spare me having to remember to save every 5 mins.
C# is my technology, does this sound realistic?
This CodeProject article shows how to send keystrokes to an external application (with C# source code).
You can use WinApi for this purpose. SendMessage or PostMessage method to send desired message to your application.
Here's C# definition of SendMessage
[DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
long wParam, // first message parameter
long lParam // second message parameter
);
and define the message that you want to send like:
public const uint %WM_MESSAGE_HERE% = %value%;
Check PInvoke - great resource containing WinApi method definitions for .NET
This should be possible, look at www.pinvoke.net and search for sendinput, that page should tell you everything you need to know, also how to find the window (look for findwindow)

Categories