How to Catch the User Input in Windows? Hooking!

Recently I got involved in a project as PM which deals with user input in Windows. The task was not easy so I hope to share some know-hows and tricks to implement the one.

There are two categories of user input: from character devices like keyboard and mouse, from inner process. An example of inner process is network packet-capturing. Anyway, I got to quote some code related to the first type of capture below. (Actually I don’t intend to post about the second type) Those implementations are actually in object-oriented structure. It is not that difficult so I just focused on the main issue rather than abstraction matter.

int idHook = NULL;
HHOOK hHook = NULL;

bool SetHook(HOOKPROC HookProc, HINSTANCE hInst) {

ClearHook();
hHook = SetWindowsHookEx(idHook, HookProc, hInst, 0);
return (hHook != 0);

}
bool SetHook(HOOKPROC HookProc, HINSTANCE hInst, DWORD dwThreadId) {

ClearHook();
hHook = SetWindowsHookEx(idHook, HookProc, hInst, dwThreadId);
return (hHook != 0);

}
void ClearHook() {

if (hHook) {

UnhookWindowsHookEx(hHook);
hHook = NULL;

}

}

It’s very simple idea to register or clear hook process. To get inputs from character devices, we can use WH_JOURNALRECORD for idHook variable. It hooks all the messages from keyboard and mouse, returns the specific values in HookProc event handler. SetHook function gets 2~3 arguments which are hook procedure, Window instance handle and not necessarily, thread ID. I set the HookProc like this.

LRESULT CALLBACK JournalRecordProc(int code, WPARAM wParam, LPARAM lParam) {

if (code >= 0) UserInteractionProc(wParam, lParam);
return CallNextHookEx(hHook, code, wParam, lParam);

}

JournalRecordProc seems to be simple and concise. UserInteractionProc represents an abstraction for disposal of the input message. That looks finished the implementation, however, there is a missing point. If user sends interrupt message, then the hook procedure would cease the operation. And the interruption easily occurs as Ctrl+ESC or Ctrl+Alt+Del key composition would make such an interruption. To retrieve the operation, you need to interleave a snippet of code lines.

// Here is message loop in Windows application
while (GetMessage(&msg, NULL, 0, 0)) {

// Retrieve the hook procedure here!
if (msg.message == WM_CANCELJOURNAL) SetHook(JournalRecordProc, hInst);

TranslateMessage(&msg);
DispatchMessage(&msg);

}

I checked out that it works well in Windows XP. For Vista and 7, additional authorization process needed. For more information, see the MSDN documentation here. It refers the User Interface Privilege Isolation (UIPI) and integrity in the lowest part of the document and you can get an answer for it.

Posted in DEVELOPMENT | Tagged , | Leave a comment

Food, Clothes and House Never Get Evolutionary Change

Since the ancient time of human-kind, people have lived with food, clothes and houses (and sex). Those are rarely changed in the market. Although the newest fashions might roll out and go away in turn, the basic element hasn’t changed that the history shows. When I consider the market of those categories, it seems to have no innovative engine.

Otherwise, the distribution system dealing with such products gets the evolution. Now we have more elegant transportations: car, ship and aircraft. And the prevalance of internet accelerates the process.

You’d rather not miss it! Transportation and internet tech don’t belong to any categories I’ve mentioned at the top of this article. What I intended to say is that daily necessities don’t greatly depend upon its content. It is certain that consumers may prefer something to other things, though, various kinds of foods, clothes and houses are affluent and have no scarcity; that is, no innovation available in themselves.

The virtue of those is that the market is stable so many restaurants, clothes shop and house distributors start up their business. Hoever, no big risk returns neither big fail nor big gain. Personally I’m not interested in this kind of market as they have no evolutionary change. If you want the change, then think about mixing those categories with other industries like web service and new delivery system. When you run the business of daily-used product, you may as well experience the whizzy matters to get an insight for expanding the business. And it will give you more gain than only delivering stable stuffs.

Posted in BUSINESS | Tagged , , | Leave a comment

Feedback How-to Matters

Project needs feedbacks. The manager ought to have a time for them with the members involved regularly. How often do you have the time? Or do you have the guideline?

The purpose of feedback is to redirect and rearrange the project. As many projects bump into uncertainty, the manager should make it clear and look for the right direction. And here are some tips which I suggest for feedback operation.

  • The proper term of regular feedback is about 1~2 weeks.
  • Unless it’s emergency, do not make any extra-feedback.
  • The feedback meeting should have all members involved (possibly).
  • Check the works going around and list the exposed problems (and explicit changes).
  • Discuss the solution and reassign the task together.

With the simple by-law, the feedback would not be under way in confusion.

Posted in PROJECT | Tagged | Leave a comment