using System.Diagnostics;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections.Generic;
public static List<Ue> UeList = new List<Ue>();
public static Timer UeTimer = new Timer(500);
private static void UeTimer_Elapsed(Object source, ElapsedEventArgs e)
foreach(Process p in Process.GetProcessesByName("ue"))
if(p.MainWindowHandle != IntPtr.Zero)
if(!UeList.Any(x => x.ProcessId == p.Id))
Ue test = new Ue(p.MainWindowHandle, p.Id);
for(int i = 0; i < UeList.Count(); i++)
if(!File.Exists(UeList[i].ExcelPath))
msg = "No Excel file found";
Console.WriteLine((i+1)+". "+UeList[i].Location + " - " + msg);
public static void Main()
UeTimer.Elapsed += UeTimer_Elapsed;
const int WM_GETTEXT = 0x0D;
const int WM_GETTEXTLENGTH = 0x0E;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, StringBuilder text);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
public static extern IntPtr GetWindowCaption(IntPtr hwnd, StringBuilder lpString, int maxCount);
public Ue(IntPtr hWnd,int id)
IntPtr hToolBar = FindWindowEx(MainWindowHandle, IntPtr.Zero, "Centura:FormToolBar", null);
IntPtr hEdit = FindWindowEx(hToolBar, IntPtr.Zero, "Edit", null);
StringBuilder sb = new StringBuilder(37);
int RetVal = SendMessage(hEdit, WM_GETTEXT, sb.Capacity, sb);
string sUserNameAndLocation = sb.ToString();
sUserName = sUserNameAndLocation.Split(' ')[0];
sLocation = sUserNameAndLocation.Split(' ')[4];
doJob.Elapsed += doElapsed;
public IntPtr MainWindowHandle {get; set;}
private string sUserName;
private string sLocation;
private void doElapsed(Object source, ElapsedEventArgs e)
public string ExcelPath {get
return Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\"+Location+".xlsx";
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(ExcelPath);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
Excel.Range excelRange = xlWorksheet.UsedRange;
public string UserName {get
public string Location {get
private string GetWindowTitle(IntPtr hwnd)
StringBuilder sb = new StringBuilder(256);
GetWindowCaption(hwnd, sb, 256);