Recent

Author Topic: [SOLVED] Process Messages in console application  (Read 8264 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1046
[SOLVED] Process Messages in console application
« on: June 11, 2011, 11:09:47 pm »
Hi,

I have console application for windows which waiting for message sent from other application with SendMessage (so it's blocking until my application response). I have simple loop which waiting for this message. Console application create dummy non visible window (CreateWindowEx()) which handle this message:
Code: Pascal  [Select]
  1. program Test;
  2.  
  3. uses Windows;
  4.  
  5. var
  6.   ServiceWnd: HWND;
  7.   ServiceWndClass: Windows.TWndClass;
  8.  
  9.   { Queue message handling }
  10.   function ServiceWndProc(HWindow: HWnd; Message: UINT; WParam: WPARAM; LParam: LPARAM): Longint;stdcall;
  11.   begin
  12.     Result:=0;
  13.     { Client response }
  14.     if (Message=MyMessage) then
  15.     begin
  16.       Writeln('Got Message');
  17.       Result := 1;
  18.     end else
  19.     { Default }
  20.     Result:=DefWindowProc(HWindow,Message,WParam,LParam);
  21.   end;
  22.  
  23. begin
  24.   ServiceWndClass.lpfnWndProc    := @ServiceWndProc;
  25.   ServiceWndClass.hInstance        := HInstance;
  26.   ServiceWndClass.lpszClassName := 'DemoClass';
  27.   ServiceWnd := CreateWindowEx(WS_EX_TOOLWINDOW, PChar('DemoClass'),
  28.     'Demo', WS_POPUP {!0}, 0, 0, 0, 0, 0, 0, HInstance, nil);
  29.   if (ServiceWnd=0) then
  30.     Exit;
  31.  
  32.   while True do
  33.     Sleep(10);
  34.  
  35.   DestroyWindow(ServiceWnd);
  36. end;
  37.  

With this code, application which send message to me freezing until I terminate my console application. It seems that my console don't send response correctly to SendMessage caller. If I add Application.ProcesMessages to my loop then it works fine but I must add LCL and interfaces to my simply console application. What ProcessMessages do that caller get response?

Regards
« Last Edit: June 11, 2011, 11:41:06 pm by Dibo »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Process Messages in console application
« Reply #1 on: June 11, 2011, 11:29:46 pm »
Place the caret over Application.ProcessMessages on code editor. Then use Alt + Up to find the declaration and Ctrl + Shift + Down to find the procedure of function.

(If I understand your question)
« Last Edit: June 12, 2011, 12:00:12 am by typo »

Dibo

  • Hero Member
  • *****
  • Posts: 1046
Re: [SOLVED] Process Messages in console application
« Reply #2 on: June 11, 2011, 11:41:22 pm »
I tried that but stopped on WidgetSet.AppProcessMessages which is abtract (I should search it in lazarus source). But I found solution. I must peek messages in loop by my self:
Code: Pascal  [Select]
  1.   while true do begin
  2.     if PeekMessage(Msg,0,0,0,0) then
  3.     begin
  4.       GetMessage(Msg,0,0,0);
  5.       TranslateMessage(Msg);
  6.       DispatchMessage(Msg);
  7.     end;
  8.     Sleep(10);
  9.   end;
  10.  
Sorry for false alarm, I am not familiar in console applications.

Regards