0

Im working on application for Windows Server 2008 R2 (.NET 3.5.1) that would work even after cold reboot, without requring someone to log on any account on the server.

Few words about application itself, it is written in c# application for registering employees work time at the company. Users (employees), have thier cards which are beeing scanned by barcode scanner, each scan means either "work started" or "work stopped", everything is serlialized into xml file which is later on modified and put into .csv but that doesn't matter.

Barcode scanner is working as a keyboard, so all codes are beeing "typed" like from a keyborad, to the PC. I made application read the keys despite the fact that console application is not in focus, or not visible at all.

What i need to do is to make that application work even after cold reboot, it has to be fully automatic.

So far i figured out 2 approaches to do it, one is to create a service which would keep another process alive (if its not working, just turn it on), i didin't have much luck with this one, i have already created service that launches another process for me, but the process is working differently, if i would run it myself, there is no communication with the process so i cannot even tell if its the right one.

Another one is to just put my app into registery /microsoft/windows/current version/run, and enable autologon for user with limited prividges. This actually could work but it is not perfect solution, because after all we do not want to have user logged in on server in company 24/7 right?

I know that most of you are way more experienced in programming than i am, so i would appriciate any solutions how to solve my problem

Lichoniespi

Lichoniespi
  • 55
  • 1
  • 8

2 Answers2

0

I would use the first approach, create a service, and to comunicate with the running application i would be using a network socket or pipe. For the service be sure that you're using an existing user account (not System) and allow it to interact with the Desktop.

Rafael
  • 2,827
  • 1
  • 16
  • 17
0

Your options depend on physical security of the system (whether passers-by can do much to it apart from scanning a barcode), but let us assume that it is an easily accessible desktop. In that case, you probably do not want a logged in user.

Use the service approach. You do NOT need a separate process for accessing the keyboard. Create a global hook of type WH_KEYBOARD_LL.

Declare your callback function like this and put it into place with SetWindowsHookEx.

Jirka Hanika
  • 13,301
  • 3
  • 46
  • 75
  • Thank you, and about keyboard access, i have it done exactly they way you suggested. And about server, no it is not accessable by any personel, its locked up under key. But i figured its bad practice overall to do it this way. And tbh, i have no clue how to create service that would communicate with other process. So far i have only done service that launches another service, but launched service isin't working properly. – Lichoniespi Sep 03 '12 at 20:08
  • I would appriciate more comments on the topic. – Lichoniespi Sep 03 '12 at 20:10
  • @Jirka: will this work across the session boundary? I thought hooks were per-session? – Harry Johnston Sep 03 '12 at 20:16
  • @HarryJohnston - Yes, hooks work across the session boundary. – Jirka Hanika Sep 03 '12 at 20:23
  • @Lichoniespi - Actually, installing a global keyboard hook is bad practice in general. Such hooks basically cannot be cleanly unloaded, in some cases it is difficult to determine the order of hooks and so on. However, if the "keyboard" is actually a barcode scanner and you never want to unload your hook, I would say that all these concerns go away. Re service launching another service: do not do that. Install both services and configure one to depend on another, if you really need two ones. – Jirka Hanika Sep 03 '12 at 20:27
  • Well, i don't think i do need two services, but somehow i cannot imagine having my entire application as service. Im not very into services, but i heard they cannot communicate very good. – Lichoniespi Sep 03 '12 at 20:29
  • So ... what happens if there's one or more Remote Desktop sessions active as well as someone at the keyboard? Do you see all the input simultaneously? Since the hook doesn't seem to receive any information about where the input came from, it seems to me that this would be completely useless - how could you tell which input was from your user and which was from someone else? – Harry Johnston Sep 03 '12 at 20:29
  • Well, i was trying to avoid that by coding the codes properly, it is not likely that somoene working on remote desktop would press combination of 10 digits, starting allways with 0 and ending with enter. But i am open for proposals, still need ot fix that problem – Lichoniespi Sep 03 '12 at 20:36
  • @HarryJohnston - The hook does not process Remote Desktop traffic. In fact, there is no remote desktop traffic until you *release* a key. – Jirka Hanika Sep 03 '12 at 21:55
  • @Lichoniespi - A service can communicate with any app just fine, however the "app" that you describe would fit much more elegantly inside the service. Apps are best used for user interaction; not for storing stuff generated by a service to the disk. You will see that it is actually quite easy. Notice also the typo fix in my answer. – Jirka Hanika Sep 03 '12 at 22:01
  • I guess you need administrator privilege to install a WH_KEYBOARD_LL hook then? It seems odd that the documentation doesn't mention it. – Harry Johnston Sep 03 '12 at 22:04
  • I will try to put my app inside of a service somehow, but to be honest, i have no idea if that is going to work. The app works like a big loop, in which each key pressed is one cycle, inside of it you have few cases to determine what to do depending on which button got pressed 0-9 and enter. 0 clears the stored line, 1-9 puts character into it, enter processes the code (checks if that ID is in database and so on, and in the end it updates the database which is serialized in XML file). If you guys think that would work as a service, i will try to make it work tommorow during my work hours. – Lichoniespi Sep 03 '12 at 22:14
  • @Lichoniespi - Yes that's it, except that you should better clear the stored line after seeing Enter and copying the stored line to a separate variable, rather than upon encountering a zero digit. All standard barcode formats may generally contain zeroes in the middle. – Jirka Hanika Sep 03 '12 at 22:26
  • I have my own barcode generator for the company needs. Anyway i have small problem with capturing keyboard events from a service. The service iteself works good (it does all things it has to do) except capturing keys from keyboard, i have WH_KEYBOARD_LL hook set, but it simply does not work. For the hook i have used the code available @ http://null-byte.wonderhowto.com/how-to/create-simple-hidden-console-keylogger-c-sharp-0132757/ What is wrong with my service, why it doesn't capture keys pressed? – Lichoniespi Sep 04 '12 at 06:15
  • @Lichoniespi - Check return codes from all functions that return any. And follow the answer to this question: http://stackoverflow.com/questions/3671673/setwindowshookex-failing-in-net-4-0-on-32-bit-machine-with-module-not-found to make sure that you are supplying a valid module handle. – Jirka Hanika Sep 04 '12 at 08:27