windows not in focus
-
Although it's rather unwieldy task I guess I know a way to perform it. To interact with an application that is not in focus I recommend you to use windows application log. Create the window service to handle BC input and write it down to the Log. In the application - check the Log for new entries on timer event and if it exists put it into your textbox. So despite your application might be not in focuse it still can achieve data from BC.
i think a better way is to create a windows service that will read the data from the Barcode reader and then put in in the clipboard. But i don't know how to create such service and how to read from this device (USB port)
-
Hello, I have a textbox in my window application. I want to be able to write into the textbox even if the application window not in focus (not "first" on the screen). How can i do it? If i need to use events for it , i don't know exacly how to write an event and where to put each code piece (the event handler etc.)
To capture Keyboard stream use hooks Read this http://msdn.microsoft.com/en-us/library/ms632589(VS.85).aspx[^] Simple example
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; namespace hook { class Program { private const int WH_KEYBOARD_LL = 13; private const int WM_KEYDOWN = 0x0100; private static LowLevelKeyboardProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; static void Main() { _hookID = SetHook(_proc); Application.Run(); UnhookWindowsHookEx(_hookID); } private static IntPtr SetHook(LowLevelKeyboardProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if ((nCode >= 0) && (wParam == (IntPtr)WM_KEYDOWN)) { int vkCode = Marshal.ReadInt32(lParam); if (((Keys)vkCode == Keys.LWin) || ((Keys)vkCode == Keys.RWin)) { Console.WriteLine("{0} blocked!", (Keys)vkCode); return (IntPtr)1; } } return CallNextHookEx(_hookID, nCode, wParam, lParam); } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
-
i think a better way is to create a windows service that will read the data from the Barcode reader and then put in in the clipboard. But i don't know how to create such service and how to read from this device (USB port)
-
To capture Keyboard stream use hooks Read this http://msdn.microsoft.com/en-us/library/ms632589(VS.85).aspx[^] Simple example
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; namespace hook { class Program { private const int WH_KEYBOARD_LL = 13; private const int WM_KEYDOWN = 0x0100; private static LowLevelKeyboardProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; static void Main() { _hookID = SetHook(_proc); Application.Run(); UnhookWindowsHookEx(_hookID); } private static IntPtr SetHook(LowLevelKeyboardProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if ((nCode >= 0) && (wParam == (IntPtr)WM_KEYDOWN)) { int vkCode = Marshal.ReadInt32(lParam); if (((Keys)vkCode == Keys.LWin) || ((Keys)vkCode == Keys.RWin)) { Console.WriteLine("{0} blocked!", (Keys)vkCode); return (IntPtr)1; } } return CallNextHookEx(_hookID, nCode, wParam, lParam); } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
how do i use this code? where do i put in? in the program.cs ? and is it a windows service?
-
I think that you need create a windows service. When the computer got data, the service app show the data on the form's textbox, even the form unfocused.
-
OK how can i do it using my application?
-
-
OK. But i have another question- If i create a windows service to manage the data from the barcode reader (the data will go to clipboard and then set focus to my application), How can i install this windows service on a user computer (without installutil etc.)?
-
OK. But i have another question- If i create a windows service to manage the data from the barcode reader (the data will go to clipboard and then set focus to my application), How can i install this windows service on a user computer (without installutil etc.)?
You could install the windows service like this, if u are using .Net FrameWork 3.5. I found that the file named "System.Configuration.Install.dll" in .Net 2.0 is different from the .Net 3.5's. There are some simple code.
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;/// <summary> /// TODO: Is the service is existed and return true or false. /// </summary> /// <param name=" NameService ">Name of the windows service.</param> /// <returns>If the service is existed then return true,or return false.</returns> private bool isServiceIsExisted(string NameService) { ServiceController\[\] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName.ToLower() == NameService.ToLower()) { return true; } } return false; } /// <summary> /// TODO: Install windows service. /// </summary> /// <param name="stateSaver">Collection(default is Null)</param> /// <param name="filepath">Full path of the windows service file</param> private void InstallmyService(IDictionary stateSaver, string filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Install(stateSaver); AssemblyInstaller1.Commit(stateSaver); AssemblyInstaller1.Dispose(); } /// <summary> /// TODO: Uninstall windows service. /// </summary> /// <param name="filepath">Full path of the windows service file</param> private void UnInstallmyService(string filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Uninstall(null); AssemblyInstaller1.Dispose(); }
Good Luck!
-
You could install the windows service like this, if u are using .Net FrameWork 3.5. I found that the file named "System.Configuration.Install.dll" in .Net 2.0 is different from the .Net 3.5's. There are some simple code.
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;/// <summary> /// TODO: Is the service is existed and return true or false. /// </summary> /// <param name=" NameService ">Name of the windows service.</param> /// <returns>If the service is existed then return true,or return false.</returns> private bool isServiceIsExisted(string NameService) { ServiceController\[\] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName.ToLower() == NameService.ToLower()) { return true; } } return false; } /// <summary> /// TODO: Install windows service. /// </summary> /// <param name="stateSaver">Collection(default is Null)</param> /// <param name="filepath">Full path of the windows service file</param> private void InstallmyService(IDictionary stateSaver, string filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Install(stateSaver); AssemblyInstaller1.Commit(stateSaver); AssemblyInstaller1.Dispose(); } /// <summary> /// TODO: Uninstall windows service. /// </summary> /// <param name="filepath">Full path of the windows service file</param> private void UnInstallmyService(string filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Uninstall(null); AssemblyInstaller1.Dispose(); }
Good Luck!
Will this code start the service too?
-
Will this code start the service too?
If set the windows service autorun, when you restart computer, the service will start. Of course, you can start the service in manual like this.
/// <summary> /// TODO: Start windows service. /// </summary> /// <param name="name">The name of windows service.</param> /// <returns>If success return true,or return false.;</returns> private bool StarmyService(string name) { ServiceController sc = new ServiceController(name); if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending ) { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10)); } else { } sc.Close(); return true; }
And stop the service like this
/// <summary> /// TODO: Stop windows service. /// </summary> /// <param name="name">The name of windows service.</param> /// <returns>If success return true,or return false.;</returns> private bool StopmyService(string name) { ServiceController sc = new ServiceController(name); if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.StartPending) { sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10)); } else { } sc.Close(); return true; }
-
If set the windows service autorun, when you restart computer, the service will start. Of course, you can start the service in manual like this.
/// <summary> /// TODO: Start windows service. /// </summary> /// <param name="name">The name of windows service.</param> /// <returns>If success return true,or return false.;</returns> private bool StarmyService(string name) { ServiceController sc = new ServiceController(name); if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending ) { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10)); } else { } sc.Close(); return true; }
And stop the service like this
/// <summary> /// TODO: Stop windows service. /// </summary> /// <param name="name">The name of windows service.</param> /// <returns>If success return true,or return false.;</returns> private bool StopmyService(string name) { ServiceController sc = new ServiceController(name); if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.StartPending) { sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10)); } else { } sc.Close(); return true; }
OK and how can i set the service to start automatically ? i mean in the service code. Also how can i check if the service is already running?
-
OK and how can i set the service to start automatically ? i mean in the service code. Also how can i check if the service is already running?
OK, the simple code to check if the service is running like this.
/// <summary> /// TODO: Is the windows service running. /// </summary> /// <param name="name">The name of windows service.</param> private bool IsRunning(string name) { bool IsRun = false; if (!isServiceIsExisted(name)) // this function was named previously. { return false; } ServiceController sc = new ServiceController(name); if (sc.Status == ServiceControllerStatus.StartPending || sc.Status == ServiceControllerStatus.Running) { IsRun = true; } sc.Close(); return IsRun; }
Set the service to start automatically: Change the value of property named "StartType" of the windows service to "Automatic". BTW, owing to the limitation of my knowledge, the code are not very strong.I suggest that you can research them by yourself. I think that after you reserach them, you will learn more.
-
OK, the simple code to check if the service is running like this.
/// <summary> /// TODO: Is the windows service running. /// </summary> /// <param name="name">The name of windows service.</param> private bool IsRunning(string name) { bool IsRun = false; if (!isServiceIsExisted(name)) // this function was named previously. { return false; } ServiceController sc = new ServiceController(name); if (sc.Status == ServiceControllerStatus.StartPending || sc.Status == ServiceControllerStatus.Running) { IsRun = true; } sc.Close(); return IsRun; }
Set the service to start automatically: Change the value of property named "StartType" of the windows service to "Automatic". BTW, owing to the limitation of my knowledge, the code are not very strong.I suggest that you can research them by yourself. I think that after you reserach them, you will learn more.
OK , and how can i set the service to start automaticallu ? i mean programatically? also how do i check if the service is already running? or it's not matter?
-
OK , and how can i set the service to start automaticallu ? i mean programatically? also how do i check if the service is already running? or it's not matter?
The function "IsRunning" will check if the windows service is running. I think that when creating a windows service just like creating a application, so I do not know how to create it programatically. We must set the windows service before we release it.
-
-
I feel that using a socet connection between service and application is not a good idea because this connection can be terminated by firewall etc ps use logging instead