How to activate a Google Chrome tab item using UI Automation - c#

I am using this code from a C# application to find a tab in Google Chrome:
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process chrome in procsChrome)
{
// the chrome process must have a window
if (chrome.MainWindowHandle == IntPtr.Zero)
{
continue;
}
AutomationElement root = AutomationElement.FromHandle(chrome.MainWindowHandle);
/*
Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "Nueva pestaña");
AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
// get the tabstrip by getting the parent of the 'new tab' button
TreeWalker treewalker = TreeWalker.ControlViewWalker;
AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab);
*/
// loop through all the tabs and get the names which is the page title
Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
foreach (AutomationElement tabitem in root.FindAll(TreeScope.Descendants, condTabItem))
{
Console.WriteLine(tabitem.Current.Name);
// I NEED TO ACTIVATE THE TAB HERE
break;
}
Condition condUrl = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
foreach (AutomationElement edit in root.FindAll(TreeScope.Descendants, condUrl))
{
string value = ((System.Windows.Automation.ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value;
Console.WriteLine(value);
}
}
I need to select certain tab item using UI Automation. How can I do it?

For those desperate souls, still searching for an answer. Here is my method, based solely on UI Automation API, without focusing windows and sending click events or hotkeys. To use the code below you need to use interop reference for UIAutomationCore.dll as described by Guy Barker.
Process[] allChromeProcesses = Process.GetProcessesByName("chrome");
Process[] mainChromes = allChromeProcesses.Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray();
//...
//Here you need to check if you have found correct chrome instance
//...
var uiaClassObject = new CUIAutomation();
IUIAutomationElement chromeMainUIAElement = uiaClassObject.ElementFromHandle(mainChromes[0].MainWindowHandle);
//UIA_ControlTypePropertyId =30003, UIA_TabItemControlTypeId = 50019
IUIAutomationCondition chromeTabCondition = uiaClassObject.CreatePropertyCondition(30003, 50019);
var chromeTabCollection = chromeMainUIAElement.FindAll(TreeScope.TreeScope_Descendants, chromeTabCondition);
//UIA_LegacyIAccessiblePatternId = 10018, 0 -> Number of Chrome tab you want to activate
var lp = chromeTabCollection.GetElement(0).GetCurrentPattern(10018) as IUIAutomationLegacyIAccessiblePattern;
lp.DoDefaultAction();
The only thing you need to remember is that searching of tabs for minimized Chrome window is impossible.

I needed to solve similar problem. Since Chrome doesn't fully implement Windows Automation features, it has to be implemented differently.
Thanks to this GitHub project I was able to activate the correct Chrome tab. The trick is to press Ctrl+tab index to activate the tab in the case its position is between 1 and 8 (9 switches to the last tab, see Chromebook keyboard shortcuts). For tabs appearing further in the collection Ctrl+Tab is pressed repeatedly until the desired tab is reached.
However, it is not that easy, since sometimes the tabs can appear in the UI automation collection out of order. I have fixed this by calling the TryGetClickablePoint method for each tab and sorting the tabs by the X coordinate of the point returned.
bool ActivateChromeTab(string title)
{
Process[] procsChrome = Process.GetProcessesByName("chrome");
foreach (Process proc in procsChrome)
{
if (proc.MainWindowHandle == IntPtr.Zero)
{
continue;
}
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
TreeWalker treewalker = TreeWalker.ControlViewWalker;
AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab);
Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
var index = 0;
var tabItems = elmTabStrip.FindAll(TreeScope.Children, condTabItem);
var coll = new List<AutomationElement>();
foreach (AutomationElement element in tabItems)
{
coll.Add(element);
}
bool NameMatch(string name)
{
return name == title || name.StartsWith(title + " ");
}
// short-circuit the search when no searched string cannot be found
if (!coll.Any(e => NameMatch(e.Current.Name)))
{
continue;
}
var t = new Stopwatch();
t.Start();
var withPoints = coll.AsParallel().Select(e =>
{
var point = new System.Windows.Point(int.MaxValue, int.MaxValue);
if (e.TryGetClickablePoint(out point))
{
}
return new
{
Name = e.Current.Name,
Element = e,
Point = point
};
}).OrderBy(e => e.Point.X);
foreach (var tabItem in withPoints)
{
index++;
var name = tabItem.Name;
if (NameMatch(name))
{
SetForegroundWindow(proc.MainWindowHandle); // activate window
Select(index); // select tab
return true;
}
}
}
return false;
}
And the method to select the tab:
public void Select(int tabIndex)
{
const int maxShortcutNumber = 8;
if (tabIndex <= 0) { return; }
KeyDown(LCtrl);
if (tabIndex <= maxShortcutNumber)
{
KeyPress(GetKeyNumber(tabIndex));
}
else
{
KeyPress(GetKeyNumber(maxShortcutNumber));
for (var i = 0; i < tabIndex - maxShortcutNumber; i++)
{
i.Dump();
const int timeToDigestPreviousKeyPress = 75;
Thread.Sleep(timeToDigestPreviousKeyPress);
KeyPress(Tab);
}
}
KeyUp(LCtrl);
}
And keyboard handling methods (adapted from KeyboardSend class)
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public static byte GetKeyNumber(int number)
{
if (number < 0 || number > 9)
throw new ApplicationException("Invalid number for key press.");
return (byte)(0x30 + number);
}
public static void KeyDown(byte vKey)
{
keybd_event(vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
public static void KeyUp(byte vKey)
{
keybd_event(vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
public static void KeyPress(byte vKey)
{
KeyDown(vKey);
KeyUp(vKey);
}
public static byte LCtrl = 0xA2; //VK_LCONTROL
public static byte LWin = 0x5B; //VK_LWIN
public static byte LAlt = 0xA4; //VK_LMENU
public static byte Tab = 0x09; //VK_TAB
private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;

Related

loop through chrome tabs and close page depending on web address

I would like to be able to loop through all the tabs on a chrome page and close any tabs which are youtube pages.
I have done some googling & found the code below. There are two (well probably more) issues. Firstly I have create a WPF application and added the System.Windows.Automation namespace (using visual studio 2015 .net 4.5) but AutomationElement is not recognised.
Also I am unsure of how to loop through the tabs and test if a page is a youtube page.
Process[] procsChrome = Process.GetProcessesByName("chrome");
if (procsChrome.Length <= 0)
return null;
foreach (Process proc in procsChrome)
{
// the chrome process must have a window
if (proc.MainWindowHandle == IntPtr.Zero)
continue;
// to find the tabs we first need to locate something reliable - the 'New Tab' button
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (SearchBar != null)
{
AutomationPattern[] patterns = SearchBar.GetSupportedPatterns();
if(patterns.Length > 0)
{
ValuePattern val = (ValuePattern)SearchBar.GetCachedPattern(patterns[0]);
if (val.Current.Value.Contains("youtube.com") || val.Current.Value.Contains("youtube.co.uk"))
proc.Close();
}
}
}
System.Windows.Automation is in UIAutomationClient.dll.Did you add UIAutomationClient.dll as a reference to your project?
Check for value "youtube".
if (SearchBar != null)
{
AutomationPattern[] patterns = SearchBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)SearchBar.GetCurrentPattern(patterns[0]);
if(val.Current.Value.Contains("youtube.com"))
proc.Close();
}
}
Based on your question, I wrote a small program to achieve this. Let me know if it works.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
class Program
{
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main()
{
Process[] procs = Process.GetProcessesByName("chrome");
if (procs.Length == 0)
{
Console.WriteLine("Google Chrome is not currently open");
return;
}
List<string> titles = new List<string>();
IntPtr hWnd = IntPtr.Zero;
int id = 0;
int numTabs = procs.Length;
foreach (Process p in procs)
{
if (p.MainWindowTitle.Length > 0)
{
hWnd = p.MainWindowHandle;
id = p.Id;
break;
}
}
bool isMinimized = IsIconic(hWnd);
if (isMinimized)
{
ShowWindow(hWnd, 9); // restore
Thread.Sleep(100);
}
SetForegroundWindow(hWnd);
SendKeys.SendWait("^1"); // change focus to first tab
Thread.Sleep(100);
int next = 1;
string title;
while (next <= numTabs)
{
try
{
title = Process.GetProcessById(id).MainWindowTitle.Replace(" - Google Chrome", "");
if (title.ToLower().Contains("youtube"))
{
SendKeys.SendWait("^{w}"); // close tab.
Thread.Sleep(100);
}
next++;
SendKeys.SendWait("^{TAB}"); // change focus to next tab
Thread.Sleep(100);
}
catch (Exception ex)
{
// Chrome internal process, doesn't have tab.
}
}
if (isMinimized)
{
ShowWindow(hWnd, 6); // minimize again
Thread.Sleep(100);
}
hWnd = Process.GetCurrentProcess().MainWindowHandle;
SetForegroundWindow(hWnd);
Thread.Sleep(100);
Console.WriteLine("Closed youtube tabs");
Console.ReadKey();
}
}
It can be done easily with AutoHotkey
Here is a script that will open Chrome, loop all tabs and close the YouTube ones, then minimize Chrome again
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, 2
IfWinExist, ahk_class Chrome_WidgetWin_1
{
WinActivate, Chrome
WinGetActiveTitle, ActiveWindowOld
Loop {
Send, ^{Tab}
Sleep, 500
WinGetActiveTitle, ActiveWindow
if (ActiveWindow = ActiveWindowOld)
{
break
}
IfInString, ActiveWindow, YouTube - Google Chrome
{
Send, ^{w}
}
}
WinMinimize, Chrome
}
Credit: https://autohotkey.com/board/topic/148742-cycling-through-all-chrome-tabs-and-closing-a-specific-tab/
if (SearchBar != null)
{
bool valuePatternExist = (bool)SearchBar.GetCurrentPropertyValue(AutomationElement.IsValuePatternAvailableProperty);
if (valuePatternExist)
{
ValuePattern val = SearchBar.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
if (val.Current.Value.Contains("youtube.com") || val.Current.Value.Contains("youtube.co.uk"))
proc.Close();
}
}
Killing just Chrome tabs with YouTube doesn't really seem like a solution to the actual problem here. I think it would fair easier and more reliable to force all the workstations to go to sleep.
Something from this post ought to do the trick.
rundll32.exe powrprof.dll,SetSuspendState 0,1,0 perhaps?

USBDeviceInfo/ManagementObjectSearcher gives Arguments that where´nt asked for

I have some code i wrote for finding the COM-Port of a certain Motor-Controller, based off of this Article:
Getting Serial Port Information
Since this Code doesn´t explicitely look for ComPorts, but for Device Names and Infos, I played around a bit and found out that when you look for a certain device in the resulting list, referenced by name, and write that to a *.txt file, the COMPort shows up in (brackets) after the name, like so: Devicename (COM5).
This I used to write that information to a string using Array.Find, and from then on it´s just selecting the number to connect to.
My Problem is that i don´t want to loop through ALL devices, but just until the one I´m looking for is found, and then break the loop.
This, however, results in a string[1] Array without the COMPort attached to entries, opposed to a string[2] Array with ComPort attached if I don´t break the loop.
I want to know why the COMPort gets attached in the first place (my guess is collection.Dispose), and why breaking the loop kills that "function/bug/whatever".
{
public static void ComPortSelectionMain(/*string[] args*/)
{
string ComDummy; // ComPort, ComPortString;
int ComNumber = 0;
var usbDevices = GetUSBDevices();
var ComList = new List<string>();
foreach (var usbDevice in usbDevices)
{
if (usbDevice.Description == "PI C-863")
{
//Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}", usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
ComList.Add(usbDevice.Name); //Add USB Device
}
}
var ComArray = ComList.ToArray();
var target = "PI C-863 (COM";
// var target2 = ")";
int arrayindex = 0;
int start = 0, stop = 0;
string targetname = "PI C-863";
string test = (Array.Find(ComArray, element => element.Contains(targetname)));
//Console.WriteLine("Test: " + test);
if (test == targetname) //look if comarray contains target
{
//int index = Array.FindIndex(ComArray, item => item == "("); // row.Contains(targetname));
int indexsigned = Array.IndexOf(ComArray, target);
int index = Math.Abs(indexsigned);
start = ComArray[index].IndexOf('(') + 1;
arrayindex = index;
stop = ComArray[index].IndexOf(')');
}
int COMlength = 0, portlength = 0, pos = 0;
COMlength = stop - start;
portlength = stop - start - 3;
pos = start + 3;
//Console.WriteLine("COM: {0}", ComArray[arrayindex]);
ComDummy = ComArray[arrayindex].Substring(start, COMlength);
//Console.WriteLine("ComDummy: {0}", ComDummy);
ComNumber = Convert.ToInt32(ComArray[arrayindex].Substring(pos, portlength));
Console.WriteLine("ComPort Number: {0}", ComNumber);
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(#"Select * From Win32_PnPEntity"))
collection = searcher.Get();
bool kappa = true;
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description"),
(string)device.GetPropertyValue("Name"),
(string)device.GetPropertyValue("Service")
));
***//if ((devices.Exists(element => element.Description == "PI C-863")) && kappa)
//{
// Console.WriteLine("Found PI C-863!");
// kappa = false;
// //break;
//}*** <<< THIS IF() BREAKS MY CODE
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name, string var)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
this.Name = name;
this.Var = var;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
public string Name { get; private set; }
public string Var { get; private set; }
}
The if(devices.Exist) question breaks my code if I use it and let if break the loop, can somebody explain why, and what i could do?
(Code works fine so far, but not being able to break the loop slows it down quite a bit)
Thanks in advance :)
Regards, Cinnabar
You should try :
static List<USBDeviceInfo> GetUSBDevices()
{
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(#"Select * From Win32_PnPEntity"))
collection = searcher.Get();
foreach (var device in collection)
{
if(device.GetPropertyValue("Description").Equals("PI C-863"))
{
Console.WriteLine("Found PI C-863!");
break;
}
}
collection.Dispose();
return devices;
}
I did not test it, but i think iterating over a List while adding into it could be the issue.
SerialPort.GetPortNames() reads the comports on computer. Use following code to read the comports and fill combo box (dropdown).
private void refreshPortChoices()
{
int selectedPortIndex = this.cbbPortName.SelectedIndex;
this.cbbPortName.Items.Clear();
this.cbbPortName.Items.AddRange(SerialPort.GetPortNames());
if (selectedPortIndex < this.cbbPortName.Items.Count)
{
this.cbbPortName.SelectedIndex = selectedPortIndex;
}
if (this.cbbPortName.Items.Count == 0)
{
this.cbbPortName.Items.Add("none");
this.btnConnection.Enabled = false;
}
else
{
this.btnConnection.Enabled = true;
}
}

Opera - driver.WindowHandles returns wrong count

In my scenario, I'm verifying whether clicking on a link navigates to another page (verifying for the page title). IE, FF and chrome return 2 as expected but Opera returns 4. I didn't have any other Opera instances opened at the time of running tests. It clicks on the link and required page is opened but WindowHandles returns 4.
Code:
string BaseWindow = Drivers._driverInstance.CurrentWindowHandle;
Drivers._driverInstance.SwitchTo().Frame(Drivers._driverInstance.FindElement(By.ClassName("iframe-fix")));
if (Drivers._driverInstance.GetType().Name.ToString() == "InternetExplorerDriver")
{
IJavaScriptExecutor js = (IJavaScriptExecutor)Drivers._driverInstance;
js.ExecuteScript("arguments[0].click();", Drivers._driverInstance.FindElement(By.LinkText("Professional Services.")));
}
else
{
Drivers._driverInstance.FindElement(By.LinkText("Professional Services.")).Click();
}
System.Collections.ObjectModel.ReadOnlyCollection<string> handles = Drivers._driverInstance.WindowHandles;
if (handles.Count == 2)
{
foreach (string handle in handles)
{
if (handle != BaseWindow)
{
string title = Drivers._driverInstance.SwitchTo().Window(handle).Title;
Assert.AreEqual("title of the page", Drivers._driverInstance.Title);
}
}
}
else
{
Assert.Fail("WindowHandles returns " + handles.Count + " instead of 2");
}
Drivers._driverInstance.SwitchTo().Window(BaseWindow);
Can someone suggest why Opera returns 4 instead of 2.
Thanks.
The Opera driver doesn't return the right number of handles. This issue has already been reported to the project but it seems that the project is no longer maintained:
https://github.com/operasoftware/operachromiumdriver/issues/15
I encountered the same thing as you with Opera driver, plus (if I remember it right), the CurrentWindowHandle property doesn't work either.
Workaround:
public static void SwitchToPopup(TestTarget target, bool toPopup)
{
if (target.IsOpera)
{
if (toPopup)
{
_windowIndex += 3;
new WebDriverWait(target.Driver, TimeSpan.FromSeconds(DefaultTimeoutInSeconds)).Until(d => d.WindowHandles.Count > _windowIndex);
}
else
{
_windowIndex -= 3;
}
target.Driver.SwitchTo().Window(target.Driver.WindowHandles[_windowIndex]);
}
else
{
IEnumerable<string> windowHandles = toPopup ? target.Driver.WindowHandles : target.Driver.WindowHandles.Reverse();
bool bFound = false;
foreach (string windowHandle in windowHandles)
{
if (bFound)
{
target.Driver.SwitchTo().Window(windowHandle);
break;
}
bFound = windowHandle == target.Driver.CurrentWindowHandle;
}
}
}

Display CPU usage of all running processes, programmatically?

Question
How do I get the cpu usage of each process into PopulateApplications()?
What's happening
getCPUUsage() gives me the same value for each process. It's like it's getting the cpu usage for only one process.
The rest of the code seems to work fine.
getCPUUsage() from class Core:
public static double getCPUUsage()
{
ManagementObject processor = new ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
processor.Get();
return double.Parse(processor.Properties["PercentProcessorTime"].Value.ToString());
}
What I've tried
In form1, I have a method by which I display information about processes like icons, name, and statuses (i.e. running/not running).
void PopulateApplications()
{
DoubleBufferedd(dataGridView1, true);
int rcount = dataGridView1.Rows.Count;
int rcurIndex = 0;
foreach (Process p in Process.GetProcesses())
{
try
{
if (File.Exists(p.MainModule.FileName))
{
var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
Image ima = icon.ToBitmap();
ima = resizeImage(ima, new Size(25, 25));
ima = (Image)(new Bitmap(ima, new Size(25, 25)));
String status = p.Responding ? "Running" : "Not Responding";
if (rcurIndex < rcount - 1)
{
var currentRow = dataGridView1.Rows[rcurIndex];
currentRow.Cells[0].Value = ima;
currentRow.Cells[1].Value = p.ProcessName;
currentRow.Cells[2].Value = cpuusage;
currentRow.Cells[3].Value = status;
}
else
{
dataGridView1.Rows.Add(
ima, p.ProcessName,cpuusage, status);//false, ima, p.ProcessName, status);
}
rcurIndex++;
}
}
catch ( Exception e)
{
string t = "error";
}
}
if (rcurIndex < rcount - 1)
{
for (int i = rcurIndex; i < rcount - 1; i++)
{
dataGridView1.Rows.RemoveAt(rcurIndex);
}
}
}
I added this line:
currentRow.Cells[2].Value = cpuusage;
cpuusage is double-type variable.
I changed this line, also, to include addition of cpuusage:
dataGridView1.Rows.Add(
ima, p.ProcessName,cpuusage, status);
Now I have a background worker event, dowork, whereby I use cpuusage to get the cpu usage values:
this.Invoke(new Action(() => cpuusage = Core.getCPUUsage()));
Maybe I don't need to call the method getCPUUsage() through backgroundworker.
This is what i see when im running the program:
All the processes have the same cpu usage ? Not logic.
Then when there is an update i see:
Again all the cells have the same cpu usage value. But on the left there are many processes each should have it's own cpu usage.

Get selected items of folder with WinAPI

I try to get the selected files of a folder which the user is using. I have the following code which is already running, but only on desktop files:
private string selectedFiles()
{
// get the handle of the desktop listview
IntPtr vHandle = WinApiWrapper.FindWindow("Progman", "Program Manager");
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
vHandle = WinApiWrapper.FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");
//IntPtr vHandle = WinApiWrapper.GetForegroundWindow();
//Get total count of the icons on the desktop
int vItemCount = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMCOUNT, 0, 0);
//MessageBox.Show(vItemCount.ToString());
uint vProcessId;
WinApiWrapper.GetWindowThreadProcessId(vHandle, out vProcessId);
IntPtr vProcess = WinApiWrapper.OpenProcess(WinApiWrapper.PROCESS_VM_OPERATION | WinApiWrapper.PROCESS_VM_READ |
WinApiWrapper.PROCESS_VM_WRITE, false, vProcessId);
IntPtr vPointer = WinApiWrapper.VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
WinApiWrapper.MEM_RESERVE | WinApiWrapper.MEM_COMMIT, WinApiWrapper.PAGE_READWRITE);
try
{
for (int j = 0; j < vItemCount; j++)
{
byte[] vBuffer = new byte[256];
WinApiWrapper.LVITEM[] vItem = new WinApiWrapper.LVITEM[1];
vItem[0].mask = WinApiWrapper.LVIF_TEXT;
vItem[0].iItem = j;
vItem[0].iSubItem = 0;
vItem[0].cchTextMax = vBuffer.Length;
vItem[0].pszText = (IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)));
uint vNumberOfBytesRead = 0;
WinApiWrapper.WriteProcessMemory(vProcess, vPointer,
Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
Marshal.SizeOf(typeof(WinApiWrapper.LVITEM)), ref vNumberOfBytesRead);
WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMW, j, vPointer.ToInt32());
WinApiWrapper.ReadProcessMemory(vProcess,
(IntPtr)((int)vPointer + Marshal.SizeOf(typeof(WinApiWrapper.LVITEM))),
Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
vBuffer.Length, ref vNumberOfBytesRead);
string vText = Encoding.Unicode.GetString(vBuffer, 0,
(int)vNumberOfBytesRead);
string IconName = vText;
//Check if item is selected
var result = WinApiWrapper.SendMessage(vHandle, WinApiWrapper.LVM_GETITEMSTATE, j, (int)WinApiWrapper.LVIS_SELECTED);
if (result == WinApiWrapper.LVIS_SELECTED)
{
return vText;
}
}
}
finally
{
WinApiWrapper.VirtualFreeEx(vProcess, vPointer, 0, WinApiWrapper.MEM_RELEASE);
WinApiWrapper.CloseHandle(vProcess);
}
return String.Empty;
}
I tried to get the window handle with GetForegroundWindow() and then call the SHELLDLL_DefView without success.
So how can I change the first 3 rows to get me the handle of the current folder in use?
That's a lot of hacking to do something that is explicitly supported by the various shell objects and interfaces. Granted the documentation doesn't make it easily discoverable, but the functionality is there. Raymond Chen wrote a great article about using these interfaces. There doesn't appear to be a way to get the "current" folder, though I guess you could get the HWNDs and see if any is the foreground window.
thank you very much. You gave me the right direction. It is possible to get the selected files of a folder:
/// <summary>
/// Get the selected file of the active window
/// </summary>
/// <param name="handle">Handle of active window</param>
/// <returns></returns>
public String getSelectedFileOfActiveWindow(Int32 handle)
{
try
{
// Required ref: SHDocVw (Microsoft Internet Controls COM Object)
ShellWindows shellWindows = new SHDocVw.ShellWindows();
foreach (InternetExplorer window in shellWindows)
{
if (window.HWND == handle)
return ((Shell32.IShellFolderViewDual2)window.Document).FocusedItem.Path;
}
}
catch (Exception)
{
return null;
}
return null;
}

Categories