Hi everyone

I have an application which hooks to notepad and records the keyboard strokes, this works fine only when i have an notepad open before executing my application. Once the appl is running and then i open notepad my appl doesnt work. My ques is suppose i have first executed my appl and then starts my notepad, how can i hook to this notepad.

Second ques is suppose i have opened say some 5 notepads, and my appl is already running how can i record keystrokes of the active notepad. I mean, first i may work with notepad 5, then i minimize it or close it and starts working with 2 nd notepad so on .... How to capture keystrokes of the current active notepad.

The code is

#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "conio.h"
HINSTANCE hins;
HOOKPROC hkprc;
HHOOK hk;
HANDLE hnd;
FILE *fp;
char c;
int flag=0;
int i;
int _tmain(int argc, _TCHAR* argv[])
{
	//Sleep(1000);	
	HWND wndhnd;
	DWORD threadid;
	system("notepad.exe");
	wndhnd=FindWindow(NULL,_T("Untitled - Notepad"));
	threadid=GetWindowThreadProcessId(wndhnd,NULL);
	hins=LoadLibrary(TEXT("C:\\Documents and Settings\\manu\\My Documents\\Visual Studio 2008\\Projects\\globalkeyhook\\Debug\\globalkeyhook.dll"));
	hkprc=(HOOKPROC)GetProcAddress(hins,"KeyboardProc");
	hk=SetWindowsHookEx(WH_KEYBOARD,hkprc,hins,threadid);
				
	if(threadid==0)
	{
	        printf("Unhooked\n");
		UnhookWindowsHookEx(hk);
	}
	else 
	{
		printf("hooked to threadid-->%lu:",threadid);
				}

PLease help me ...i am newbie to system programming.....

If you use CreateProcess() instead of system() to spawn Notepad.exe then CreateProcess() will give you the thread id.

Here is an example. The PROCESS_INFORMATION structure will contain the data you are looking for.

#include <Windows.h>
#include <iostream>
using std::cout;

int main()
{
    PROCESS_INFORMATION pinfo;
    STARTUPINFO sinfo;
    memset(&sinfo,0,sizeof(sinfo));
    sinfo.cb = sizeof(STARTUPINFO);

    if( CreateProcess(NULL,"Notepad.exe myfile.txt",NULL,NULL,
            FALSE,0,NULL,NULL,&sinfo,&pinfo) == 0)
    {
        cout << "Error calling Notepad\n";
        return 1;
    }
    return 0;
}

Edited 5 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.