Read listbox content of from another application C# - c#

This old stackoverflow post is asking pretty much the same question that I have, however using Spy++ I have obtained the controls handle ID. Now what? :)
I am not sure what this process is called where I can obtain the contents of another applications control from a .net application, therefore I am not having much success with results on the old google machine.
I have an MFC application with a listbox that contains data I need to automate a task using a WPF C# application. I would prefer not to use an external lib and don't think it would be too labour intensive once I have found the process and have my C# app take visibility of the respective list control to do what I need.
Can anyone please point me in the right direction as to what I should be looking up or provide some code to get me started. At this point I'm stuck and my little project relies pretty heavily on this. I don't want to use an OCR either.
Thanks,
Ash

To get text from Win32 ListBox control you have to use messages and functions specially for that control, here is a reference :
http://msdn.microsoft.com/en-us/library/windows/desktop/ff485971%28v=vs.85%29.aspx
In your case you should first see how many items are in the listbox with LB_GETCOUNT, and then for each item get text with LB_GETTEXT.
Here is the method that will return items in a list, parameter is ListBox control window handle :
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, StringBuilder lParam);
const int LB_GETCOUNT = 0x018B;
const int LB_GETTEXT = 0x0189;
private List<string> GetListBoxContents(IntPtr listBoxHwnd)
{
int cnt = (int)SendMessage(listBoxHwnd, LB_GETCOUNT, IntPtr.Zero, null);
List<string> listBoxContent = new List<string>();
for (int i = 0; i < cnt; i++)
{
StringBuilder sb = new StringBuilder(256);
IntPtr getText = SendMessage(listBoxHwnd, LB_GETTEXT, (IntPtr)i, sb);
listBoxContent.Add(sb.ToString());
}
return listBoxContent;
}

This question's answer should get you started. Google P/Invoke and FindWindow() / GetWindowText() family of methods.
Hope that helps.

Related

How do I arrange or sort the desktop icons in c#?

You know when you right-click the desktop and there's a "Sort by" option that allows you to sort the icons by "Name", "Size", "Item Type", or "Date Modified"? Well, I want to find a way to sort the desktop's icons with a push of a button.
I saw a similar question being asked here on stackoverflow but it's old and the code didn't work for me. The link to the question is: Arranging desktop icons with C# . I'm trying to achieve this in Windows 10.
There was a comment on there that said that the LVM_* and LVA_* values are stored in the commctrl.h file which comes with the SDK. I couldn't find that file for some reason.
Here's what i'm using:
//sort desktop
public const int LVM_ARRANGE = 4118;
public const int LVM_ALIGNLEFT = 1;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
//end of sort desktop
private void organizeBtn_Click(object sender, EventArgs e)
{
var DesktopHandle = GetDesktopWindow();
MessageBox.Show(GetDesktopWindow().ToString());
SendMessage(GetDesktopWindow(), LVM_ARRANGE, LVM_ALIGNLEFT, 0);
}
I've been digging for info or some sort of direction about this topic, especially regarding windows 10, but I can't find much. Please help?
In Windows 10, the desktop (not tile world!) is still a SysListView32, but the GetDesktopWindow API call will return the handle of its grandparent, a Progman window - reminiscence of the ancient "Program Manager" of Windows 3.0. Then there is a shim of the SHELLDLL_DefView class, and below that you find buried the listview you're after.
Use the information from this answer to move down from the shell window to the folder view, which you can eventually send the LVM_ARRANGE message.
This is a brittle approach, as it relies on undocumented properties of the operating system, which may change at any time with updates or new versions. It will probably break also when a user uses a slideshow as desktop background, because Windows then rearranges the desktop window stack. Hack to deal with this here.
Another approach which is documented and less likely to break in future versions, with the downside of involving COM and a nightmare from C#, is via the IFolderView of shell automation, two relevant finds here and here.

Read all text from external Application

I have a hotkey window application in C# and I want all the text from the focused window of other application on pressing hotkey like notepad, browser, command window(cmd), Turbo c++, Pascal etc.
So Is it possible?
If any one have idea please help me with code example.
I have attach screen shot. I want to read text from this window. On pressing hotkey I want to read text "This is my test text".
There is a GetWindowText() in user32 API,
but if you need to get text from a control in another process, GetWindowText() won't work.
You have to use SendMessage() with WM_GETTEXT instead:
const UInt32 WM_GETTEXT = 0x000D;
const UInt32 WM_GETTEXTLENGTH = 0x000E;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);
static string GetWindowTextRaw(IntPtr hwnd)
{
// Allocate string length
int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
StringBuilder sb = new StringBuilder(length + 1);
// Get window text
SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
return sb.ToString();
}
Application that call themselves "Screen Reader" (for visually impaired people) do that kind of things, sort of.
They use the old MSSA (Microsoft Active Accessibility) APIs and/or the new UIAutomation APIs.
With the two APIs, if you have a "Main Window" HWND, you can then browse the tree of the componants making the app. You can then retrieve properties, such as "Text" or "Name" and so on.
If the application doesn't support Accessive technologies, you fall back on case by case solutions, which means eventually awful hacks (as APIs hooking) or more regular methods (as DLL injection and use of the JNI Invocation API in the JAVA case).
Its is not directly possible through C#,
Still microsoft provides with WMI services which can utilized to get at max information on the machine and processes. Kindly check MSDN
You can download WMI tool from here and possible check Win32 classes and methods, you may find useful information for your requirement

Why is the text from the title of another application window incorrect?

I'm running a small tool (on Windows 7, 32 Bit) that I would like to see what document is open in another application I've tried this, which works for NotePad on Windows.
var myProcess = Process.GetProcessesByName("NotePad");
string title = myProcess[0].MainWindowTitle;
MessageBox.Show(title);
the output is:
"New Text Document - Notepad"
Now, if I try another application it doesn't always give me the correct title but I noticed that most microsoft applications seem to be fine - NotePad, WordPad, EXCEL etc. It's this other software that is an issue. It has a long title but just returns a very simple name.
Here's what I get from my application which has processName = "FooBar"
The actual running window has this up the top:
"FooBar Software Verson 1.2 - [Results]"
and my code gives:
"FooBar"
any ideas?
[EDIT} 2012-11-19
The very crux of this issue is that I was trying to get the name of the open file from the window. It now seems that the software I'm using doesn't display it there. What I've discovered is that a program called "AutoIT3 Window Spy" can get the text I need as the text of the open file is on the window and not only in the title. I downloaded the source (it's part of http://www.autohotkey.com/ which is open source. It seems to rely on many of the suggestions already made but I'm not able to figure it out just yet.) The source code that I"m looking at is c++ and is located here https://github.com/AutoHotkey/AutoHotkey
So I think the solution to my problem may lay elsewhere. This one may go unanswered.
The main window title is what you see when you go in to task manager and look at the Description column, not the window title itself.
It's the title of the process, not the title of a particular window in the process. A given process may have any number of windows open at one time.
If you need the actual window title, you have to hook user32 something like this:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Security;
namespace Application
{
public class Program
{
public static void Main ( )
{
IntPtr hwnd = UnsafeNativeMethods.FindWindow("Notepad", null);
StringBuilder stringBuilder = new StringBuilder(256);
UnsafeNativeMethods.GetWindowText(hwnd, stringBuilder, stringBuilder.Capacity);
Console.WriteLine(stringBuilder.ToString());
}
}
[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern int GetWindowText ( IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount );
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow ( string lpClassName, string lpWindowName );
}
}
It's possible that the 'title' you're seeing is some kind of owner-drawn UI element which is overriding the visual representation of the title, while the Windows APIs are likely to ignore that sort of thing.
I'd recommend examining the window with a tool like Spy++ to see if that's the case.
It's also possible that the author of the application decided to override the WM_GETTEXT message and is returning that instead of what's actually in the titlebar, although I'm not 100% sure if GetWindowText() is being called here and whether or not it sends the message explicitly or works some other way.
The developer has stated the following:
"I think the failure may be due to a discontinued property Form.Caption in VB 6.0 that was replaced with a Form.Text in. NET"
Thank you all for your valuable suggestions along the way!

How to get a process window class name from c#?

How can I get the window class name of a certain process?
I want to achieve this in c#.
I've tried the process class in c# but I can only get the window name of the process.
Thanks
I assume you mean you want to get the class name of the main window of a process.
To do this, you will need to get the handle to the main window using the MainWindowHandle of your Process object, and then use the following interop method to obtain the class name:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
see pinvoke.net for sample code and MSDN for details on the function.
You can also use the windows ui automation framework to achieve this without getting into pinvoke.
int pidToSearch = 316;
//Init a condition indicating that you want to search by process id.
var condition = new PropertyCondition(AutomationElementIdentifiers.ProcessIdProperty,
pidToSearch);
//Find the automation element matching the criteria
AutomationElement element = AutomationElement.RootElement.FindFirst(
TreeScope.Children, condition);
//get the classname
var className = element.Current.ClassName;

how to use ORIYA font in smart device development?

I am developing an application for WINDOWS CE5.0 based device. It requires ORIYA language(INDIAN REGIONAL LANGUAGE) to be used completely. As visual studio use ENGLISH as standard language, please tell me how to proceed? I tried to copy the font in WINDOWS CE device's WINDOWS/FONTS folder but as i restart the device that font file disappears. I developed the application in c# and changed labels text into oriyaa in Development system. It looks fine on the development system but as i deployed it into device, All label text appears in ENGLISH. I dont know whats happening? I also need to set the LABEL.TEXT property in ORIYA language. Is it possible? How to take user input in ORIYA? Please help..... Thanks...
Not very sure as what you meant by browser but for the Forms you can go about using PrivateFontCollection
you can load the font from a folder in your app and then use
AddFontFile or AddMemoryFont as per your need. So now the Client can see the controls in the font you set and its available to it irrespective of its installed or not
I have used the following approach with English-based fonts, but I am not sure if it will work on your case. The original source of this approach is a nice post from Chris Tacke (SO user #ctacke) with some modifications.
[DllImport("coredll.dll")]
private static extern int AddFontResource(string lpszFilename);
[DllImport("coredll.dll", SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
static IntPtr HWND_BROADCAST = (IntPtr)0xFFFF;
const int WM_Fontchange = 0x001D;
private static void RegisterFont(string aFontPath, string aTargetFontPath)
{
IntPtr thir = (IntPtr)0;
IntPtr fourth = (IntPtr)0;
try
{
if (!System.IO.File.Exists(aTargetFontPath))
System.IO.File.Copy(aFontPath, aFontTargetPath);
}
catch { throw; }
int _Loaded = AddFontResource(aFontTargetPath);
if (_Loaded != 0)
SendMessage(HWND_BROADCAST, WM_Fontchange, thir, fourth);
}

Categories