473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Keyboard logging functionality

I need to write a program that launches Internet Explorer, then counts
how many <ENTER> keys are pressed inside IE. I've seen a lot of
references to the SetWindowsHookEx() API call, but all sources I found
seem to be piece-meal and incomplete. I'm using VC++ .NET, and would
rather avoid using the WinAPI, if possible. Is there an easier way
(perhaps using .NET) to do this?
Nov 17 '05 #1
15 1668
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
I see that I can use WH_KEYBOARD_LL to create a global hook WITHOUT having
a separate DLL, but I can't find any examples anywhere.
Hmm, where do the docs say that? In the help entry for SetWindowsHookEx()
their is a table in which the WH_KEYBOARD_LL hook is listed as having only
global scope.

Then in the remarks for the HOOKPROC parameter in the call there is this

<quote>
[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or
specifies the identifier of a thread created by a different process, the
lpfn parameter must point to a hook procedure in a dynamic-link library
(DLL). Otherwise, lpfn can point to a hook procedure in the code associated
with the current process.
</quote>

Taken together that means that a DLL is required, no?
is for VB or C#. Do either of you know of any?? I'd even take a "with the
DLL" example if nothing better can be found...


There is old C code for a global hook in the file Hook.c in the Platform
SDK's directory

Samples\multimedia\gdi\WinCap32

Note its use of the hook is canonical in that the hook detects a condition
but does NOT handle it. It posts a message back to the application which
planted the hook and that application is the one that handles it. This is
the right way because

a) Global hooks should have small footprints as they bloat _every_ windowed
application
b) Their code runs in the context of the application being hooked. An errant
hook can crash an otherwise fine application

Regards,
Will
Nov 17 '05 #2
William DePalo [MVP VC++] wrote:
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
I see that I can use WH_KEYBOARD_LL to create a global hook WITHOUT having
a separate DLL, but I can't find any examples anywhere.

Hmm, where do the docs say that? In the help entry for SetWindowsHookEx()
their is a table in which the WH_KEYBOARD_LL hook is listed as having only
global scope.

Then in the remarks for the HOOKPROC parameter in the call there is this

<quote>
[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or
specifies the identifier of a thread created by a different process, the
lpfn parameter must point to a hook procedure in a dynamic-link library
(DLL). Otherwise, lpfn can point to a hook procedure in the code associated
with the current process.
</quote>

Taken together that means that a DLL is required, no?

is for VB or C#. Do either of you know of any?? I'd even take a "with the
DLL" example if nothing better can be found...

There is old C code for a global hook in the file Hook.c in the Platform
SDK's directory

Samples\multimedia\gdi\WinCap32

Note its use of the hook is canonical in that the hook detects a condition
but does NOT handle it. It posts a message back to the application which
planted the hook and that application is the one that handles it. This is
the right way because

a) Global hooks should have small footprints as they bloat _every_ windowed
application
b) Their code runs in the context of the application being hooked. An errant
hook can crash an otherwise fine application

Regards,
Will


In experts exchange, as well as a few other sites, people mentioned that
with that hook, you didn't have to have your code in a separate DLL.

So, the main code to be managed, and I guess the DLL code needs to be
unmanaged?? What type of a project is the DLL?

Is there no code that is designed for VC++ .NET?
Nov 17 '05 #3
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:OA*************@TK2MSFTNGP14.phx.gbl...
William DePalo [MVP VC++] wrote:
In experts exchange, as well as a few other sites, people mentioned that
with that hook, you didn't have to have your code in a separate DLL.
Hmm. To hook a process other than the one in which the SetWindowsHookEx()
call is made. Sounds hokey to me.
So, the main code to be managed, and I guess the DLL code needs to be
unmanaged?? What type of a project is the DLL?
The DLL would be native. If you are using VS.Net 2003 you first select C++
projects and then click on the Win32 icon. Then click Application Settings
and choose the DLL option. A
Is there no code that is designed for VC++ .NET?


As far as I know, no. Realize that I don't claim to be a .Net expert.

Regards,
Will

Nov 17 '05 #4
William DePalo [MVP VC++] wrote:
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:OA*************@TK2MSFTNGP14.phx.gbl...
William DePalo [MVP VC++] wrote:
In experts exchange, as well as a few other sites, people mentioned that
with that hook, you didn't have to have your code in a separate DLL.

Hmm. To hook a process other than the one in which the SetWindowsHookEx()
call is made. Sounds hokey to me.

So, the main code to be managed, and I guess the DLL code needs to be
unmanaged?? What type of a project is the DLL?

The DLL would be native. If you are using VS.Net 2003 you first select C++
projects and then click on the Win32 icon. Then click Application Settings
and choose the DLL option. A

Is there no code that is designed for VC++ .NET?

As far as I know, no. Realize that I don't claim to be a .Net expert.

Regards,
Will


Ok, well with the callback code in a separate DLL, how will I get the
"ENTER count" back into my managed code?
Nov 17 '05 #5
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:e6**************@TK2MSFTNGP09.phx.gbl...
Ok, well with the callback code in a separate DLL, how will I get the
"ENTER count" back into my managed code?


You adopt some interprocess communication mechanism between the DLL (which
runs in the applications being hooked) and your application which planted
the hook (presumably via some form of interop). The sample to which I
pointed posts a WM_COMMAND message. You code do that.

Of course, that begs the question as to how you retrieve the message on the
managed side. It's not something that I've done so I can't offer any advice.
IMO, what you are trying to do is best done natively, anyway.

Regards,
Will
Nov 17 '05 #6
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:OM*************@TK2MSFTNGP12.phx.gbl...
I need to write a program that launches Internet Explorer,
See CreateProcess(). It will return a process identifier and a thread
identifier which will come in handy shortly.
then counts how many <ENTER> keys are pressed inside IE. I've seen a lot
of references to the SetWindowsHookEx() API call, but all sources I found
seem to be piece-meal and incomplete.
:-) Well, they probably demonstrate how to intercept keystrokes but not how
to implement your task.
I'm using VC++ .NET, and would rather avoid using the WinAPI,
if possible. Is there an easier way (perhaps using .NET) to do this?


I don't believe so.

One way to attack the problem would be with a system wide keyboard hook
which must necessarily be implemented in a DLL. You'd only be interested in
notifications where the WPARAM parameter of the notification is VK_ENTER and
where the high bit (#31) of the LPARAM parameter is 0. When that's the case
you'd call GetFocus() to find the active window (that's the destination of
the keystroke) and then GetWindowThreadProcessId() to find either the
identifier of that the owning process or thread. You'd compare either
against what you got from CreateProcess() and on a match increment your
count.

That method is not at all specific to IE but would work with any process you
create.

IE does permit itself to be extended with add-ons so there _may_ be some
easier way inside one of these extensions. You might want to search for a
group devoted to IE to see if there is some easier way.

By the way, why do you need to count the ENTER keys pressed?

Regards,
Will

Nov 17 '05 #7
>> Yes, I was just thinking to myself: "Whatever on earth for??" Sounds
like an interesting application.


Some of my users log onto a website that uses an ActiveX control to log
onto an AS/400 (within the web browser). We get charged for every "screen"
sent from the server, which happens whey they hit ENTER. To avoid having
to make them keep track of how many screens they see, I can just count the
ENTER keys.


You may consider another approach to handle this case (case of using ActiveX
in the Internet Explorer):
1. Create alternative ActiveX, which mimics interface of the original
ActiveX and forwards all requests to it.
2. Implement there your tracing logic.
3. Register ActiveX substitution for the Internet Explorer in the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX
Compatibility\{original guid}]
"AlternateCLSID"="{substitution guid}"
"Compatibility Flags"=dword:00000000

--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com

Nov 17 '05 #8
William DePalo [MVP VC++] wrote:
By the way, why do you need to count the ENTER keys pressed?


Yes, I was just thinking to myself: "Whatever on earth for??" Sounds
like an interesting application.
Nov 17 '05 #9
Joel Whitehouse wrote:
William DePalo [MVP VC++] wrote:
By the way, why do you need to count the ENTER keys pressed?

Yes, I was just thinking to myself: "Whatever on earth for??" Sounds
like an interesting application.


Some of my users log onto a website that uses an ActiveX control to log
onto an AS/400 (within the web browser). We get charged for every
"screen" sent from the server, which happens whey they hit ENTER. To
avoid having to make them keep track of how many screens they see, I can
just count the ENTER keys.

I see that I can use WH_KEYBOARD_LL to create a global hook WITHOUT
having a separate DLL, but I can't find any examples anywhere. The only
code I see is for VB or C#. Do either of you know of any?? I'd even take
a "with the DLL" example if nothing better can be found...
Nov 17 '05 #10
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
I see that I can use WH_KEYBOARD_LL to create a global hook WITHOUT having
a separate DLL, but I can't find any examples anywhere.
Hmm, where do the docs say that? In the help entry for SetWindowsHookEx()
their is a table in which the WH_KEYBOARD_LL hook is listed as having only
global scope.

Then in the remarks for the HOOKPROC parameter in the call there is this

<quote>
[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or
specifies the identifier of a thread created by a different process, the
lpfn parameter must point to a hook procedure in a dynamic-link library
(DLL). Otherwise, lpfn can point to a hook procedure in the code associated
with the current process.
</quote>

Taken together that means that a DLL is required, no?
is for VB or C#. Do either of you know of any?? I'd even take a "with the
DLL" example if nothing better can be found...


There is old C code for a global hook in the file Hook.c in the Platform
SDK's directory

Samples\multimedia\gdi\WinCap32

Note its use of the hook is canonical in that the hook detects a condition
but does NOT handle it. It posts a message back to the application which
planted the hook and that application is the one that handles it. This is
the right way because

a) Global hooks should have small footprints as they bloat _every_ windowed
application
b) Their code runs in the context of the application being hooked. An errant
hook can crash an otherwise fine application

Regards,
Will
Nov 17 '05 #11
William DePalo [MVP VC++] wrote:
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
I see that I can use WH_KEYBOARD_LL to create a global hook WITHOUT having
a separate DLL, but I can't find any examples anywhere.

Hmm, where do the docs say that? In the help entry for SetWindowsHookEx()
their is a table in which the WH_KEYBOARD_LL hook is listed as having only
global scope.

Then in the remarks for the HOOKPROC parameter in the call there is this

<quote>
[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or
specifies the identifier of a thread created by a different process, the
lpfn parameter must point to a hook procedure in a dynamic-link library
(DLL). Otherwise, lpfn can point to a hook procedure in the code associated
with the current process.
</quote>

Taken together that means that a DLL is required, no?

is for VB or C#. Do either of you know of any?? I'd even take a "with the
DLL" example if nothing better can be found...

There is old C code for a global hook in the file Hook.c in the Platform
SDK's directory

Samples\multimedia\gdi\WinCap32

Note its use of the hook is canonical in that the hook detects a condition
but does NOT handle it. It posts a message back to the application which
planted the hook and that application is the one that handles it. This is
the right way because

a) Global hooks should have small footprints as they bloat _every_ windowed
application
b) Their code runs in the context of the application being hooked. An errant
hook can crash an otherwise fine application

Regards,
Will


In experts exchange, as well as a few other sites, people mentioned that
with that hook, you didn't have to have your code in a separate DLL.

So, the main code to be managed, and I guess the DLL code needs to be
unmanaged?? What type of a project is the DLL?

Is there no code that is designed for VC++ .NET?
Nov 17 '05 #12
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:OA*************@TK2MSFTNGP14.phx.gbl...
William DePalo [MVP VC++] wrote:
In experts exchange, as well as a few other sites, people mentioned that
with that hook, you didn't have to have your code in a separate DLL.
Hmm. To hook a process other than the one in which the SetWindowsHookEx()
call is made. Sounds hokey to me.
So, the main code to be managed, and I guess the DLL code needs to be
unmanaged?? What type of a project is the DLL?
The DLL would be native. If you are using VS.Net 2003 you first select C++
projects and then click on the Win32 icon. Then click Application Settings
and choose the DLL option. A
Is there no code that is designed for VC++ .NET?


As far as I know, no. Realize that I don't claim to be a .Net expert.

Regards,
Will

Nov 17 '05 #13
William DePalo [MVP VC++] wrote:
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:OA*************@TK2MSFTNGP14.phx.gbl...
William DePalo [MVP VC++] wrote:
In experts exchange, as well as a few other sites, people mentioned that
with that hook, you didn't have to have your code in a separate DLL.

Hmm. To hook a process other than the one in which the SetWindowsHookEx()
call is made. Sounds hokey to me.

So, the main code to be managed, and I guess the DLL code needs to be
unmanaged?? What type of a project is the DLL?

The DLL would be native. If you are using VS.Net 2003 you first select C++
projects and then click on the Win32 icon. Then click Application Settings
and choose the DLL option. A

Is there no code that is designed for VC++ .NET?

As far as I know, no. Realize that I don't claim to be a .Net expert.

Regards,
Will


Ok, well with the callback code in a separate DLL, how will I get the
"ENTER count" back into my managed code?
Nov 17 '05 #14
"Stephen Corey" <swc@_NOSPAM_wardandsmith.com> wrote in message
news:e6**************@TK2MSFTNGP09.phx.gbl...
Ok, well with the callback code in a separate DLL, how will I get the
"ENTER count" back into my managed code?


You adopt some interprocess communication mechanism between the DLL (which
runs in the applications being hooked) and your application which planted
the hook (presumably via some form of interop). The sample to which I
pointed posts a WM_COMMAND message. You code do that.

Of course, that begs the question as to how you retrieve the message on the
managed side. It's not something that I've done so I can't offer any advice.
IMO, what you are trying to do is best done natively, anyway.

Regards,
Will
Nov 17 '05 #15
>> Yes, I was just thinking to myself: "Whatever on earth for??" Sounds
like an interesting application.


Some of my users log onto a website that uses an ActiveX control to log
onto an AS/400 (within the web browser). We get charged for every "screen"
sent from the server, which happens whey they hit ENTER. To avoid having
to make them keep track of how many screens they see, I can just count the
ENTER keys.


You may consider another approach to handle this case (case of using ActiveX
in the Internet Explorer):
1. Create alternative ActiveX, which mimics interface of the original
ActiveX and forwards all requests to it.
2. Implement there your tracing logic.
3. Register ActiveX substitution for the Internet Explorer in the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX
Compatibility\{original guid}]
"AlternateCLSID"="{substitution guid}"
"Compatibility Flags"=dword:00000000

--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com

Nov 17 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vikas | last post by:
Hi y'all, I need logging functionality to provide me following: - logging levels that can be set at run-time. - able to output to different files for different levels - thread safe - line,...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
16
by: Einar Høst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some...
3
by: Stephen Corey | last post by:
I need to write a program that launches Internet Explorer, then counts how many <ENTER> keys are pressed inside IE. I've seen a lot of references to the SetWindowsHookEx() API call, but all sources...
2
by: philip.mckee | last post by:
Hi Looking for some very general advice. I have a classic asp content management application which I am porting to ASP.NET. The database is SQL server, and it is hoped that we can leverage this...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
16
by: dfaber | last post by:
Hi all, I have been searching for a keyboard and mouse tracker on linux. I've read solutions (watch at sourceforge) which look at /proc/interrupts to check keyboard or mouse activity. I also read...
0
by: rshekhtm | last post by:
Hi everyone, I would like to get your opinion on a technique I came up with when faced with the problem of redundant code in every web method (authentication, logging, exception handling)....
1
by: pavanjosh | last post by:
Hello All, I want to develop an application which is a banking application. I have already developed a application in .NET , C#. The major functionality of this application is automatic logging...
3
by: nicholas.petrella | last post by:
I am currently trying to use the python logging system as a core enterprise level logging solution for our development and production environments. The rotating file handler seems to be what I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.