I'm trying to make a little application to enumerate in console every window name from each process thread.
I'm currently using this code that works fine on a windows 7 machine but for some reason it stucks at windows 10 without any stack trace or error message, I tried to add almost everything inside a try-catch but I didn't get any message as well so I'm lost.
Here is the code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
private delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
static void Main(string[] args)
{
Process[] processes = Process.GetProcesses();
Console.WriteLine("Detected: " + processes.Length + " processes.");
foreach (Process process in processes)
{
IEnumerable<IntPtr> windowHandles = null;
try
{
windowHandles = EnumerateProcessWindowHandles(process);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
Console.WriteLine("Checking process" + process.ProcessName);
foreach (var handle in windowHandles)
{
StringBuilder message = new StringBuilder();
try
{
SendMessage(handle, 0x000D, message.Capacity, message);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
if (message.Length == 0)
{
continue;
}
Console.WriteLine("Window name: " + message.ToString());
}
}
Console.WriteLine("Finished!");
Console.ReadLine();
}
private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(Process process)
{
List<IntPtr> handles = new List<IntPtr>();
ProcessThreadCollection threads = null;
try
{
threads = process.Threads;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
return handles;
}
foreach (ProcessThread thread in threads)
{
try
{
EnumThreadWindows(thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
}
return handles;
}
}
}
Any idea about why it could stuck?
UPDATE
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
private delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr handle);
static void Main(string[] args)
{
Process[] processes = Process.GetProcesses();
Console.WriteLine("Detected: " + processes.Length + " processes.");
foreach (Process process in processes)
{
IEnumerable<IntPtr> windowHandles = null;
try
{
windowHandles = EnumerateProcessWindowHandles(process);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
Console.WriteLine("Checking process: " + process.ProcessName);
foreach (IntPtr handle in windowHandles)
{
if (handle == null)
{
continue;
}
if (!IsWindowVisible(handle))
{
continue;
}
StringBuilder message = new StringBuilder();
int capacity = 0;
try
{
capacity = message.Capacity;
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
try
{
SendMessage(handle, 0x000D, capacity, message);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
if (message.Length == 0)
{
continue;
}
Console.WriteLine("Window name: " + message.ToString());
}
}
Console.WriteLine("Finished!");
Console.ReadLine();
}
private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(Process process)
{
List<IntPtr> handles = new List<IntPtr>();
ProcessThreadCollection threads = null;
try
{
threads = process.Threads;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
return handles;
}
foreach (ProcessThread thread in threads)
{
if (thread == null)
{
continue;
}
int threadId = 0;
try
{
threadId = thread.Id;
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
try
{
EnumThreadWindows(threadId, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
}
return handles;
}
}
}
Thank you all.
Try running your console application as administrator if the problem still arises I suggest using the API GetLastError that's what it does:
Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.
Try reading the value to know the problem , try this link for guidence on how to use it:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx
Related
An object reference is required for the non-static field, method, or property 'injector.SI(uint, string)' I dont really understand what is the error or the mistake (Visual Studio C# windows form app it would be a really big help if anyone can fix this because I cant find the solution at all Ive been searching for quite a bit of time but still cant find any solution
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Hoshiyomi.functions
{
public partial class injector : Form
{
public injector()
{
InitializeComponent();
Process[] PC = Process.GetProcesses().Where(p => (long)p.MainWindowHandle !=0).ToArray();
comboBox1.Items.Clear();
foreach (Process p in PC)
{
comboBox1.Items.Add(p.ProcessName);
}
}
private void injector_Load(object sender, System.EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
}
private static string DLLP { get; set; }
private void flatButton1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog OFD = new OpenFileDialog();
OFD.InitialDirectory = #"C:\";
OFD.Title = "NotWeeb | Locate DLL";
OFD.DefaultExt = "dll";
OFD.Filter = "DLL Files (*.dll)|*.dll";
OFD.CheckFileExists = true;
OFD.CheckPathExists = true;
OFD.ShowDialog();
textBox1.Text = OFD.FileName;
DLLP = OFD.FileName;
}
catch (Exception ed)
{
MessageBox.Show(ed.Message);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
DLLP = textBox1.Text;
}
private void flatButton3_Click(object sender, EventArgs e)
{
Process[] PC = Process.GetProcesses().Where(p => (long)p.MainWindowHandle != 0).ToArray();
comboBox1.Items.Clear();
foreach (Process p in PC)
{
comboBox1.Items.Add(p.ProcessName);
}
}
static readonly IntPtr INTPTR_ZERO = (IntPtr)0;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenProcess(uint dwDesiredAccess, int bInheritHandle, GraphicsUnit dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
static extern int CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetProcAddress(IntPtr Module, string lpProcName);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpBaseAddress, byte[] Buffer, uint size, int lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
static extern int WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, int lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttribute, IntPtr dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
public static int Inject(string PN, string DLLP)
{
if (!File.Exists(DLLP)) { return 1; } //1 not exist
uint _procId = 0;
Process[] _procs = Process.GetProcesses();
for (int i = 0; i < _procs.Length; i++)
{
if (_procs[i].ProcessName == PN)
{
_procId = (uint)_procs[i].Id;
}
}
if (_procId == 0) { return 2; } //2 not exist
if (!SI(_procId, DLLP))
{
return 3; //3 inject fail
}
return 4; //4 inject success
}
public bool SI(uint P, string DLLP)
{
IntPtr hndProc = OpenProcess((0x2 | 0x8 | 0x10 | 0x20 | 0x400), 1, P);
if (hndProc == INTPTR_ZERO) { return false; }
IntPtr lpAddress = VirtualAllocEx(hndProc, (IntPtr)null, (IntPtr)DLLP.Length, (0x1000 | 0x2000), 0x40);
if (lpAddress == INTPTR_ZERO)
{
return false;
}
byte[] bytes = Encoding.ASCII.GetBytes(DLLP);
if (WriteProcessMemory(hndProc, lpAddress, bytes, (uint)bytes.Length, 0) == 0)
{
return false;
}
CloseHandle(hndProc);
return true;
}
private IntPtr OpenProcess(int v1, int v2, uint p)
{
throw new NotImplementedException();
}
private IntPtr VirtualAllocEx(IntPtr hndProc, IntPtr intPtr, IntPtr length, int v1, int v2)
{
throw new NotImplementedException();
}
private void flatButton2_Click(object sender, EventArgs e)
{
int Result = Inject(comboBox1.Text, DLLP);
if (Result == 1)
{
MessageBox.Show("File doesn't exist");
}
else if (Result == 2)
{
MessageBox.Show("Process doesn't exist");
}
else if (Result == 3)
{
MessageBox.Show("Injection fail");
}
else if (Result == 4)
{
MessageBox.Show("Injection Succeeded");
}
}
}
}
enter image description here
I'm searching a way to disable alt and ctrl key input while I'm pressing it.
I'm actually developping a Macro Configurer and when I press my macro (for example Alt+Ctrl+NumPad0) and I want that the keys H+E+L+L+O are sent, it won't work because I'm pressing Alt. The computer probably tries to execute unknown shortcuts for Alt+Ctrl+H, Alt+Ctrl+E ...
So I want that when I press my macro and the program detects I pressed it, it disable temporary alt while the output keys are sent, and enable again Alt when it's finished.
I use a LowLevelKeyboardProc to detect when i press a key, but I can't prevent the Alt pressing because i'm actually pressing it to execute my macro.
//The code from my form
private void Listener_OnKeyUp(object sender, KeyUpArgs e)
{
KeyAction.RemoveKeyDown(e.KeyUp);
}
private void Listener_OnKeyPressed(object sender, KeyPressedArgs e)
{
try
{
KeyAction.PutKeyDown(e.KeyPressed);
foreach (MacroEntree macro in macros)
{
if (macro.Activated)
if (macro.Action != null)
if (macro.isMacroDown())
{
listener.OnKeyPressed -= new EventHandler<KeyPressedArgs>(Listener_OnKeyPressed);
new Thread(delegate ()
{
while (IsAltCtrlDown()) ;
macro.ExecuteAction();
Thread.Sleep(100);
listener.OnKeyPressed += new EventHandler<KeyPressedArgs>(Listener_OnKeyPressed);
}).Start();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Erreur : " + ex.Message);
}
}
private bool IsAltCtrlDown()
{
if (KeyAction.isKeyDown(Key.LeftAlt) || KeyAction.isKeyDown(Key.RightAlt))
return true;
if (KeyAction.isKeyDown(Key.LeftCtrl) || KeyAction.isKeyDown(Key.RightCtrl))
return true;
return false;
}
//the code from my class that checks if the macro is down
public class KeyAction
{
static List<Key> keyDown = new List<Key>();
public static bool isKeyDown(Key k)
{
return keyDown.Contains(k);
}
public static void PutKeyDown(Key k)
{
if (!keyDown.Contains(k))
keyDown.Add(k);
}
public static void RemoveKeyDown(Key k)
{
keyDown.Remove(k);
}
}
You could set a low-level keyboard hook to swallow keyboard input messages in windows.
Every native Windows program bases on a so called message loop that is basicly this.
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
With an low-level keyboard hook you can handle messages in this loop on the desktop process and with that prevent keys from being pressed.
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(
int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
private const int WH_KEYBOARD_LL = 13;
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
return SetWindowsHookEx(
WH_KEYBOARD_LL,
proc,
0, // hook on all input (https://stackoverflow.com/questions/7715480/is-zero-ever-a-valid-handle)
0); // for all threads
}
You can use the code like the following. When you do not call CallNextHookEx the key event will not have any impact.
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
private const int WM_KEYDOWN = 0x0100;
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
// check if it is a key down message
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
// check if the pressed key is the `Alt` key
if((Keys)vkCode == Keys.Alt)
{
// return 1 for handled if its the alt key
return (IntPtr) 1;
}
}
// let the message bubble if its not a keydown message or it wasn't the alt key which was pressed
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
// with this line you can set the `HookCallback` into the message loop
IntPtr hookID = SetHook(HookCallback);
And don't forget to unset the hook.
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
UnhookWindowsHookEx(hookID);
Thanks to #Manfred Radlwimmer I solved the problem. I send a Alt and Ctrl key up before executing my action and it works fine.
private void Listener_OnKeyPressed(object sender, KeyPressedArgs e)
{
try
{
KeyAction.PutKeyDown(e.KeyPressed);
foreach (MacroEntree macro in macros)
{
if (macro.Activated)
if (macro.Action != null)
if (macro.isMacroDown())
{
listener.OnKeyPressed -= new EventHandler<KeyPressedArgs>(Listener_OnKeyPressed);
new Thread(delegate ()
{
KeyboardOperations.SendKeyUp(KeyboardOperations.KeyCode.LALT);
KeyboardOperations.SendKeyUp(KeyboardOperations.KeyCode.LCONTROL);
macro.ExecuteAction();
Thread.Sleep(100);
listener.OnKeyPressed += new EventHandler<KeyPressedArgs>(Listener_OnKeyPressed);
}).Start();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Erreur : " + ex.Message);
}
}
To clear things up before you mention the most obvious choice, I am already calling ShowDialog and not the Show method!!!
I would like to block the close (invoked from a different thread) off an WPF window if a OpenFileDialog is open.
Here is the code I have (reduced to show my problem):
public class FooWindow : Window
{
public FooWindow()
{
InitializeComponent();
this.Closing += OnClosing;
}
public void OpenDialogAndCloseMe()
{
var ofd = new OpenFileDialog();
Thread th = new Thread(() => CloseMe());
th.Start();
ofd.ShowDialog(this);
}
public void CloseMe()
{
System.Threading.Thread.Sleep(2000); //give the OpenFileDialog time to pop up...
//since this method gets called from a different thread invoke it...
this.Dispatcher.Invoke(() => this.Close());
}
private void OnClosing(object sender, CancelEventArgs e)
{
//check if OpenFileDialog is still open and block the close...
e.Cancel = true;
}
}
The problem I am facing is the OnClosing part, how would I get the OpenFileDialog (or any other Dialog in that case).
I have searched the web and found Win32 methods like:
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumedWindow callback, ArrayList lParam);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
I have tried them both but they return 0 children, any ideas what's going wrong?
Here is the full code I tried so far:
//replace the above OnClosing with this implementation... all 3 return false
private void OnClosing(object sender, CancelEventArgs e)
{
//check if OpenFileDialog is still open and block the close...
var hWnd = new WindowInteropHelper(this).Handle;
if (WindowHandling.GetChildren(hWnd).Any())
e.Cancel = true;
if (WindowHandling.GetChildrenV2(hWnd).Any())
e.Cancel = true;
if (WindowHandling.GetChildrenV3(hWnd).Any())
e.Cancel = true;
}
public static class WindowHandling
{
private delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumedWindow callback, ArrayList lParam);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
private static bool GetWindowHandle(IntPtr windowHandle, ArrayList windowHandles)
{
windowHandles.Add(windowHandle);
return true;
}
public static IEnumerable<IntPtr> GetChildren(IntPtr hWnd)
{
if (hWnd == IntPtr.Zero)
return Enumerable.Empty<IntPtr>();
var x = new WindowHandleInfo(hWnd);
return x.GetAllChildHandles();
}
public static IEnumerable<IntPtr> GetChildrenV2(IntPtr hWnd)
{
var windowHandles = new ArrayList();
EnumedWindow callBackPtr = GetWindowHandle;
EnumChildWindows(hWnd, callBackPtr, windowHandles);
return windowHandles.OfType<IntPtr>();
}
public static IEnumerable<IntPtr> GetChildrenV3(IntPtr hParent)
{
var result = new List<IntPtr>();
var ct = 0;
var maxCount = 100;
var prevChild = IntPtr.Zero;
while (true && ct < maxCount)
{
var currChild = FindWindowEx(hParent, prevChild, null, null);
if (currChild == IntPtr.Zero)
break;
result.Add(currChild);
prevChild = currChild;
++ct;
}
return result;
}
//http://stackoverflow.com/questions/1363167/how-can-i-get-the-child-windows-of-a-window-given-its-hwnd
private class WindowHandleInfo
{
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);
private readonly IntPtr _mainHandle;
public WindowHandleInfo(IntPtr handle)
{
_mainHandle = handle;
}
public IEnumerable<IntPtr> GetAllChildHandles()
{
var childHandles = new List<IntPtr>();
var gcChildhandlesList = GCHandle.Alloc(childHandles);
var pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList);
try
{
var childProc = new EnumWindowProc(EnumWindow);
var x = EnumChildWindows(this._mainHandle, childProc, pointerChildHandlesList);
if (x == false)
{
var error = Marshal.GetLastWin32Error();
}
}
finally
{
gcChildhandlesList.Free();
}
return childHandles;
}
private static bool EnumWindow(IntPtr hWnd, IntPtr lParam)
{
var gcChildhandlesList = GCHandle.FromIntPtr(lParam);
if (gcChildhandlesList.Target == null)
return false;
var childHandles = gcChildhandlesList.Target as List<IntPtr>;
if (childHandles != null)
childHandles.Add(hWnd);
return true;
}
}
}
You can solve this with a boolean value tracking if it's open or not:
bool dialogOpen = false;
public void OpenDialogAndCloseMe()
{
var ofd = new OpenFileDialog();
Thread th = new Thread(() => CloseMe());
th.Start();
dialogOpen = true;
ofd.ShowDialog(this);
dialogOpen = false;
}
private void OnClosing(object sender, CancelEventArgs e)
{
//check if OpenFileDialog is still open and block the close...
if(dialogOpen)
{
e.Cancel = true;
}
}
The last day i've tried to make this work: Wake up my computer from sleep or hibernation, using a WPF application. Nothing i've tried worked.
So far I've tried the most popular examples on the net.. For example:
[DllImport("kernel32.dll")]
public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes,
bool bManualReset,
string lpTimerName);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWaitableTimer(SafeWaitHandle hTimer,
[In] ref long pDueTime,
int lPeriod,
IntPtr pfnCompletionRoutine,
IntPtr lpArgToCompletionRoutine,
bool fResume);
public static void SetWaitForWakeUpTime()
{
DateTime utc = DateTime.Now.AddMinutes(2);
long duetime = utc.ToFileTime();
using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer"))
{
if (SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true))
{
using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset))
{
wh.SafeWaitHandle = handle;
wh.WaitOne();
}
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
// You could make it a recursive call here, setting it to 1 hours time or similar
Console.WriteLine("Wake up call");
Console.ReadLine();
}
And the usage:
WakeUp.SetWaitForWakeUpTime();
I've also tried this example (see how I use the method after the code):
public event EventHandler Woken;
private BackgroundWorker bgWorker = new BackgroundWorker();
public WakeUp()
{
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
}
public void SetWakeUpTime(DateTime time)
{
bgWorker.RunWorkerAsync(time.ToFileTime());
}
void bgWorker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (Woken != null)
{
Woken(this, new EventArgs());
}
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
long waketime = (long)e.Argument;
using (SafeWaitHandle handle =
CreateWaitableTimer(IntPtr.Zero, true,
this.GetType().Assembly.GetName().Name.ToString() + "Timer"))
{
if (SetWaitableTimer(handle, ref waketime, 0,
IntPtr.Zero, IntPtr.Zero, true))
{
using (EventWaitHandle wh = new EventWaitHandle(false,
EventResetMode.AutoReset))
{
wh.SafeWaitHandle = handle;
wh.WaitOne();
}
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}
}
And the usage:
WakeUp w = new WakeUp();
w.Woken += new EventHandler(w_Woken);
w.SetWakeUpTime(alarmDate.Subtract(new TimeSpan(0,0,0,20)));
Nothing seems to wake up my PC.
I know it is possible, as several other alarm clocks can do it. They manage to wake up my computer with no problems, so there must be some mistake.
The problem was not all computers support this issue. There is apparently not much to do if your computer doesn't support it.
I am trying to minimize the window with the title located in the string p (specified by the user at runtime). The window is guaranteed to be a main window, because the user can only choose from main windows. I have tried showCmd, flags, and a mixture of the two, but every time, regardless of whether the window is minimized, I am shown the dialog box that states "Minimizing". How can I fix it?
private void button1_Click(object sender, EventArgs e)
{
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains(p))
{
IntPtr handle = proc.Handle;
Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT();
Program.GetWindowPlacement(handle, ref wp);
if ((wp.showCmd & 2) == 2)
{
wp.showCmd = 9;
MessageBox.Show("Restoring");
}
else
{
wp.showCmd = 2;
MessageBox.Show("Minimizing");
}
wp.length = Marshal.SizeOf(wp);
Program.SetWindowPlacement(handle, ref wp);
break;
}
}
}
What I'm using as p -
string p;
PictureBox i;
bool windowShowing = false;
bool minimized = false;
public TaskbarItem(string window)
{
InitializeComponent();
p = window;
button1.Text = window;
}
To get the window:
class WindowEnumerator
{
public delegate bool EnumDelegate(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
public static LinkedList<IntPtr> EnumMethod2(bool includeChild)
{
LinkedList<IntPtr> pss = new LinkedList<IntPtr>();
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
string s = p.MainWindowTitle;
if (s.Equals("") || s.Equals("Mod_Taskbar"))
{
continue;
}
LinkedListNode<IntPtr> node = new LinkedListNode<IntPtr>(p.MainWindowHandle);
pss.AddLast(node);
if (includeChild){
List<IntPtr> l = GetChildWindows(p.MainWindowHandle);
foreach (IntPtr i in l)
{
StringBuilder stb = new StringBuilder(255);
WindowEnumerator.GetWindowText(i, stb, 255);
if (stb.ToString().StartsWith("Address:") || stb.ToString().EndsWith("View") || stb.ToString().EndsWith("Control") ||
stb.ToString().EndsWith("Bar") || stb.ToString().EndsWith("bar") || stb.ToString().Contains("Host"))
{
continue;
}
if (Vars.excludes.Contains(stb.ToString()))
{
continue;
}
pss.AddAfter(node, i);
}
}
}
return pss;
}
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
list.Add(handle);
return true;
}
}
And:
private void LoadWindows(object sender, EventArgs e)
{
panel1.Controls.Clear();
foreach (IntPtr p in WindowEnumerator.EnumMethod2(false))
{
StringBuilder stb = new StringBuilder(255);
WindowEnumerator.GetWindowText(p, stb, 255);
TaskbarItem t = new TaskbarItem(stb.ToString());
t.Dock = DockStyle.Left;
panel1.Controls.Add(t);
}
}
Did you check what value the wp.showCmd has?
Try with this code:
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains(p))
{
IntPtr handle = proc.Handle;
Program.WINDOWPLACEMENT wp = new Program.WINDOWPLACEMENT();
Program.GetWindowPlacement(handle, ref wp);
if ((wp.showCmd & 2) == 2)
{
wp.showCmd = 9;
MessageBox.Show("Restoring");
}
else
{
wp.showCmd = 2;
MessageBox.Show("Minimizing");
}
wp.length = Marshal.SizeOf(wp);
Program.SetWindowPlacement(handle, ref wp);
break;
}
}
this.WindowState = FormWindowState.Minimized;