| Цитата(gambit @ 3.12.2008, 09:57) | | В 17 строке ошибка. |
глубоко извиняюсь что не раскрыл всё
в общем суть таково что нужно создать сервис, который опрашиваеть какие окна и какие программы сейчас находятся в фокусе у пользователя
в общем сделал такой код -
| Код | using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.ServiceProcess; using System.Timers; using System.Runtime.InteropServices; using System.Threading;
namespace TestSQL { public partial class Service1 : ServiceBase {
/// Подключение внешних библиотек с API функциями взятия окна [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern int GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
IntPtr handle; uint lpwdProcessId; Process process; string user; int currentSessionId;
public Service1() { InitializeComponent(); }
protected override void OnStart(string[] args) { this.timer1.Enabled = true; this.LogMessage("Service Started"); }
protected override void OnStop() { this.timer1.Enabled = false; this.LogMessage("Service Stopped"); }
private void timer1_Elapsed(object sender, EventArgs e) { this.LogMessage(GetWindow()); }
private void LogMessage(string Message) { { SqlConnection connection = null; SqlCommand command = null; try { connection = new SqlConnection("Server=SIT-PPO-01;Database=ProgramLog;Integrated Security=false;User Id=User;Password=;"); command = new SqlCommand("INSERT INTO MyServiceLog (vc_Status, dt_Created) VALUES ('" + Message + "',getdate())", connection); connection.Open(); int numrows = command.ExecuteNonQuery(); } catch( Exception ex ) { System.Diagnostics.Debug.WriteLine(ex.Message); } finally { command.Dispose(); connection.Dispose(); } } } private string GetWindow() { handle = GetForegroundWindow(); GetWindowThreadProcessId(handle, out lpwdProcessId); process = Process.GetProcessById((int)lpwdProcessId); return process.ProcessName.ToString() + " " + lpwdProcessId.ToString(); } } }
|
Всё хорошо сервис запускается, пишет в базу. Но вот только запускается он от System (хотя это значения не имеет, пробывал и из под себя) и соответственно возвращает Idle 0 То есть то что есть у System. Так вот как мне сделать что бы функция GetWindow(), возвращала окна и проги которые сейчас запущены у пользователя? потоком или как это вообще? |