Hi

Since i am a newbie to win32 programming i am facing difficulty in understanding how to use these GetMessage(), SendMessage(), PostTHreadMessage() apis, please help me ...

Here is the code

#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "conio.h"
#include "windef.h"
#include "winbase.h"
HINSTANCE hins;
HOOKPROC hkprc;
HHOOK hk;
HANDLE hnd;
FILE *fp;
char c;
int flag=0;
int i;
HWND wndhnd;
DWORD threadid;
BOOL bret;
MSG msg;
TCHAR buf[1024];
LPARAM lp;
int _tmain(int argc, _TCHAR* argv[])
{
	
	//Sleep(1000);	
	
	HWND checkproc(HWND);
	void install_hook(DWORD,HWND);	
		
	wndhnd=FindWindow(_T("NotePad"),NULL);
	install_hook(threadid,wndhnd);
	
	getch();
}

HWND checkproc(HWND hnd)
{
	
	while(hnd==NULL)
	{
		Sleep(3000);
		printf("Waiting for NOTEPAD thread\n");
		wndhnd=FindWindow(_T("NotePad"),NULL);
		hnd=wndhnd;
	}
	return hnd;
}
void install_hook(DWORD threadid,HWND windowhandle)
{
	HWND wnd;
	bool windowalive;
	if(windowhandle==NULL)
	{
	wndhnd=checkproc(NULL);
	}   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);
				GetModuleFileName(hins,buf,1024);
wprintf(_T("%s\n"),buf);
//RegisterHotKey(0,1,MOD_CONTROL,0x41);
MSG msg;
				while(GetMessage(&msg,0,0,0))
{
					switch(msg.message)
{
case WM_CLOSE: 
					/*if(msg.wParam==1)*/
FILE *f3=fopen("C:\\text.txt","a+");
fprintf(f3,"Hotkey pressed");
break;
}
}
if(threadid==0)
{
printf("Unhooked\n");
UnhookWindowsHookEx(hk);
}
else 
{
printf("hooked to threadid-->%lu:\n",threadid);
}
while(1)
{
windowalive=IsWindow(wndhnd);
if(!windowalive)
{
					//PostThreadMessage(threadid,WM_CLOSE,999,lp);
install_hook(NULL,NULL);
break;
}
}
}

This is my console application code written in vc++ VS 2008 which hooks on to notepad. I have one DLL for recording keystrokes.

Now where should i implement my GetMessage() in the console application exe or the dll.
I have to send a message to my console application when my active notepad window is closed so tat i print "WINDOW CLOSED" on my console.How to communicate between notepad and my console app using these api's. I have used GetMessage in the above code but not working. Please explain how to invoke Getmessage(), when will it be actually invoked, from where to where and how it sends messages


THank you

Try adding some spaces and lining up brackets.. put some spaces between your comparisons like: Meh = bleh.. not meh=bleh.. thats hard as hell to read..

also instead of doing C:\\bleh\\meh.. you can do C:/bleh/meh.. much easier.
I don't like GetMessage so I changed it to PeekMessage below as getmessage waits forever (if u need this just change it to getmessage) until a message is received.. I also added code as an example how you'd be using your hotkeys.. I used F9 as I didn't know what 0x41 represented.

You don't really have a callback procedure so its hard to tell you how to use getmessage/peekmessage as this isn't a winapi/win32 program.. its a console prog. If you don't wanna write a win32prog, u can always add a thread to the program which checks if notepad is running or not.. I added example code below to show how to check which processes are running.

I'm actually pretty sure that Console Programs don't get messages at all.. but I could be wrong so I left the peek message in the application below the standard callback proc..

Standard CallBack Proc (Win32/API):

MSG msg;
    while(GetMessage(&msg, NULL, 0, 0 ))
    { 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
    } 
    
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
       switch(msg.message)
       {
           case WM_HOTKEY:
                if(msg.wParam == F9_KEYID)
                {
                              //Blehh.... Do stuff here..
                }
                                        
           case WM_CLOSE: 
                    FILE *f3=fopen("C:/text.txt","a+");
                    fprintf(f3,"Hotkey pressed");
              break;

           default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
       }
}
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "conio.h"
#include "windef.h"
#include "winbase.h"
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "user32.lib")
HINSTANCE hins;
HOOKPROC hkprc;
HHOOK hk;
HANDLE hnd;
FILE *fp;
char c;
int flag=0;
int i;
HWND wndhnd;
DWORD threadid;
BOOL bret;
MSG msg;
TCHAR buf[1024];
LPARAM lp;

DWORD CountProcesses(CHAR *pProcessName) 
{
    DWORD dwCount = 0;
    HANDLE hSnap = NULL;
    PROCESSENTRY32 proc32;

    if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
        return -1;
    proc32.dwSize=sizeof(PROCESSENTRY32);
    while((Process32Next(hSnap, &proc32)) == TRUE)
        if(_stricmp(proc32.szExeFile,pProcessName) == 0)
            ++dwCount;
    CloseHandle(hSnap); 
    return dwCount;
}


int _tmain(int argc, _TCHAR* argv[])
{
	
	//Sleep(1000);	
	
	HWND checkproc(HWND);
	void install_hook(DWORD,HWND);	
		
	wndhnd=FindWindow(_T("NotePad"),NULL);
	install_hook(threadid,wndhnd);
	
	getch();
}

HWND checkproc(HWND hnd)
{	
	while(hnd == NULL)
	{
		Sleep(3000);
		printf("Waiting for NOTEPAD thread\n");
		wndhnd = FindWindow(_T("NotePad"),NULL);
		hnd = wndhnd;
	}
	return hnd;
}
void install_hook(DWORD threadid,HWND windowhandle)
{
	HWND wnd;
	bool windowalive;
	
	if(windowhandle == NULL)
	{
       wndhnd = checkproc(NULL);
	}
       
    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);
	GetModuleFileName(hins,buf,1024);
    wprintf(_T("%s\n"),buf);

enum {F9_KEYID = 1, F10_KEYID = 2};  //add or remove as many as you like..
//RegisterHotKey(0, F9_KEYID, 0x4000, 0x78))	   //F9 = 0x78

//For this Process u might wanna create a separate thread so that ur program continues instead of being in a loop..
char cProcess[20] = "Notepad.exe";
DWORD dwReturn;
dwReturn = CountProcesses(cProcess);

while(1000)
{
    if(dwReturn != -1)
    {
        if((dwReturn < 1))
        {
            //MessageBox... or printf("NOTEPAD is NOT RUNNING\n");
            dwReturn = CountProcesses(cProcess);
        }
    }
}



    MSG msg;
    while(PeekMessage(&msg, 0, 0, 0x0001))   //0x0001 = PM_REMOVE
    {
       switch(msg.message)
       {
           case WM_HOTKEY:
                if(msg.wParam == F9_KEYID)
                {
                              //Blehh.... Do stuff here..
                }
                                        
           case WM_CLOSE: 
					             /*if(msg.wParam==1)*/
                    FILE *f3=fopen("C:/text.txt","a+");
                    fprintf(f3,"Hotkey pressed");
              break;
       }
    }
    if(threadid == 0)
    {
         printf("Unhooked\n");
         UnhookWindowsHookEx(hk);
    }
    else 
    {
         printf("hooked to threadid-->%lu:\n",threadid);
    }
    
    while(1)
    {
        windowalive = IsWindow(wndhnd);
        if(!windowalive)
        {
					//PostThreadMessage(threadid,WM_CLOSE,999,lp);
            install_hook(NULL,NULL);
            break;
        }
    }
}

Thx a lot friend. I am sorry for my editing :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.