Recent

Author Topic: my programs crash  (Read 2414 times)

turunk

  • New member
  • *
  • Posts: 9
my programs crash
« on: February 07, 2025, 01:19:49 pm »
my programs always crash if i type few keys or do few mouse actions.
i have lazarus 3.8 windows 10 64 bit.


Code: Pascal  [Select][+][-]
  1. program keymouse;
  2.  
  3. {$APPTYPE GUI}
  4.  
  5. uses windows;
  6.  
  7. var classw1:WndClassEx;
  8.     Regwin:Integer;
  9.     Hndw:HWnd;
  10.     dc:HDC;
  11.     WT,WCN:PChar;
  12.     x,y,wid,hth:Integer;
  13.     ps:TPaintStruct;
  14.     bp:HDC;
  15.     color0,color1,color2,color3,color4:hbrush;
  16.     rect0,rect1,rect2,rect3,rect4:trect;
  17.     msg:tagmsg;
  18.  
  19.   Function Wndproc(HWnd1:qword;m:longword;w:int64;l:int64):LResult;stdcall;
  20.   Begin
  21.     wndproc:=DefWindowProc(HWnd1,m,w,l);
  22.   End;
  23.  
  24. Begin
  25.     x:=100;y:=200;wid:=300;hth:=200;
  26.     classw1.cbSize:=sizeof(classw1);
  27.     classw1.style:=CS_HREDRAW Or CS_VREDRAW;
  28.     classw1.lpfnWndProc:=Addr(wndproc);
  29.     classw1.cbclsExtra:=0;
  30.     classw1.cbWndExtra:=0;
  31.     classw1.hInstance:=GetModuleHandle(nil);;
  32.     classw1.hIcon:=LoadIcon(0, IDI_APPLICATION);
  33.     classw1.hCursor:=0;
  34.     classw1.hbrBackground:=(14 + 1);
  35.     classw1.lpszMenuName:=NiL;
  36.     classw1.lpszClassName:='class Window'#0;
  37.     classw1.hIconSm:=LoadIcon(classw1.hInstance, IDI_APPLICATION);
  38.     Regwin:=RegisterClassEx(classw1);
  39.     WT:='My Program'#0;
  40.     WCN:='My window class'#0;
  41.     hndw:=CreateWindowEx(
  42.       0,
  43.       classw1.lpszClassName,
  44.       WT,
  45.       WS_POPUP,
  46.       X,Y,wid,hth,
  47.       0,0,GetModuleHandle(NiL),NiL);
  48.     ShowWindow(hndw,sw_show);
  49.     UpdateWindow(hndw);
  50.  
  51.     DC:=GetDC(hndw);
  52.     PS.hdc:=DC;
  53.     PS.ferase:=false;
  54.     PS.rcpaint.left:=0;
  55.     PS.rcpaint.top:=0;
  56.     PS.rcpaint.right:=wid;
  57.     PS.rcpaint.bottom:=hth;
  58.     BP:=BeginPaint(hndw,PS);
  59.  
  60.     rect0.Left:=0;
  61.     rect0.right:=wid;
  62.     Rect0.top:=0;
  63.     Rect0.bottom:=hth;
  64.     color0:=CreateSolidBrush(((30*256)+30)*256+30);
  65.     fillrect(DC,rect0,color0);
  66.  
  67.     rect1.Left:=0;
  68.     rect1.right:=40;
  69.     rect1.top:=0;
  70.     rect1.bottom:=40;
  71.     color1:=CreateSolidBrush(((240*256)+0)*256+0);
  72.     rect2.left:=41;
  73.     rect2.right:=80;
  74.     rect2.top:=0;
  75.     rect2.bottom:=40;
  76.     color2:=CreateSolidBrush(((0*256)+0)*256+240);
  77.     rect3.Left:=81;
  78.     rect3.right:=120;
  79.     rect3.top:=0;
  80.     rect3.bottom:=40;
  81.     color3:=CreateSolidBrush(((0*256)+240)*256+0);
  82.     rect4.Left:=121;
  83.     rect4.right:=160;
  84.     rect4.top:=0;
  85.     rect4.bottom:=40;
  86.     color4:=CreateSolidBrush(((0*256)+240)*256+240);
  87.  
  88.     msg.hwnd:=hndw;
  89.     repeat
  90.       {keyboard 1 or 2:}
  91.       if getasynckeystate($31)<>0 then begin fillrect(DC,rect0,color0);fillrect(DC,rect1,color1);end;
  92.       if getasynckeystate($32)<>0 then begin fillrect(DC,rect0,color0);fillrect(DC,rect2,color2);end;
  93.       {mouse left or right:}
  94.       If PeekMessage(msg,hndw,$201,$201,PM_REMOVE) Then Begin fillrect(DC,rect0,color0);fillrect(DC,rect3,color3);end;
  95.       If PeekMessage(msg,hndw,$204,$204,PM_REMOVE) Then Begin fillrect(DC,rect0,color0);fillrect(DC,rect4,color4);end;
  96.     until getasynckeystate(65)<>0; {exit with keyboard A}
  97.  
  98.     DeleteObject(color1);DeleteObject(color2);DeleteObject(color3);DeleteObject(color4);
  99.     DeleteObject(bp);
  100.     closewindow(hndw);
  101.     unregisterclass(wcn,0);
  102. end.
  103.  
[/size]

BlueIcaro

  • Hero Member
  • *****
  • Posts: 818
    • Blog personal
Re: my programs crash
« Reply #1 on: February 07, 2025, 05:06:21 pm »
Hello, I tested your code under w11/64bits, using Lazarus 4.0RC2 and runs fine.
/BlueIcaro

Bart

  • Hero Member
  • *****
  • Posts: 5538
    • Bart en Mariska's Webstek
Re: my programs crash
« Reply #2 on: February 07, 2025, 06:06:34 pm »
When I run your code I get a window (gray, no title bar).
If I click on it, a green rectange appears.
After that, any input will give me the "This program does not seem to response" error.

Bart

turunk

  • New member
  • *
  • Posts: 9
Re: my programs crash
« Reply #3 on: February 07, 2025, 11:22:37 pm »
thank you, bart and blueIcaro.
you can click with right and press number 1 and 2, and sometimes i reach 3 and 6 and 8 keys to press,
and than the error message appears. but what code does make this happen? i had this errors since 3 years, but why
is win 11 in advantage? does all to be compiled in win 11? if i have win 10, why it is not useful for elder win's?

jamie

  • Hero Member
  • *****
  • Posts: 6836
Re: my programs crash
« Reply #4 on: February 08, 2025, 07:59:00 pm »
I don't see the message pump window, which is needed.
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 5080
Re: my programs crash
« Reply #5 on: February 08, 2025, 08:55:05 pm »
That code is a _disaster_.

What is it the program is supposed to do ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

turunk

  • New member
  • *
  • Posts: 9
Re: my programs crash
« Reply #6 on: February 08, 2025, 10:56:26 pm »
440bx,
the program shows that it crashes after some key press or mouse clicks.
jamie,
i never heard pump.

jamie

  • Hero Member
  • *****
  • Posts: 6836
Re: my programs crash
« Reply #7 on: February 09, 2025, 01:22:58 pm »
440bx,
the program shows that it crashes after some key press or mouse clicks.
jamie,
i never heard pump.
I bet u haven't.
U need to go into a loop call8ng get message translate message and dispatch message

Have fun
The only true wisdom is knowing you know nothing

turunk

  • New member
  • *
  • Posts: 9
Re: my programs crash
« Reply #8 on: February 10, 2025, 08:43:46 pm »
are this 3 lines, at the end of the program, where nobody wants to have this,
because why i have to do the work for windows, to get informations i need
not anymore, because my program has to finish?
for whom i shall translate what? so what is getasynckeystate and peekmessage for?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10924
  • Debugger - SynEdit - and more
    • wiki
Re: my programs crash
« Reply #9 on: February 10, 2025, 11:25:56 pm »
Doesn't crash here, and I have Win 10 64 bit too.

Though Lazarus 4.99 and FPC 3.2.3 (the latter may potentially maybe make a diff, but not to likely).

Interesting though that it does work. If I am not mistaken you only pulling mouse-click  and key messages. Then what happens to all other messages, the queue must be filling up? But then haven't done much raw Win API stuff. So maybe Win is supposed to deal with that, maybe, maybe not?

turunk

  • New member
  • *
  • Posts: 9
Re: my programs crash
« Reply #10 on: February 11, 2025, 04:26:42 am »
i do not understand, lazarus 3.8 is since 19.01.2025
do i really have to do all the messages for windows, and at least of program?
i tried all messages on diverse methods, but the program reacts on nothing anymore.

440bx

  • Hero Member
  • *****
  • Posts: 5080
Re: my programs crash
« Reply #11 on: February 11, 2025, 04:37:23 am »
do i really have to do all the messages for windows, and at least of program?
A GUI program, such as yours, should read messages from the message queue and dispatch them to the appropriate window.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

turunk

  • New member
  • *
  • Posts: 9
Re: my programs crash
« Reply #12 on: February 11, 2025, 12:34:29 pm »
Code: Pascal  [Select][+][-]
  1.     Msg.hwnd:=win1;
  2.     While GetMessage(Msg,win1,0,0) Do
  3.  
means it loops only if the first message is not 0,
and then all further are not 0.
when comes my msg's?
and why at the end of program?
and i said the program reacts on nothing anymore.
and for what i shall make all messages, which i am not
interested in?

440bx

  • Hero Member
  • *****
  • Posts: 5080
Re: my programs crash
« Reply #13 on: February 11, 2025, 12:55:55 pm »
Code: Pascal  [Select][+][-]
  1.     Msg.hwnd:=win1;
  2.     While GetMessage(Msg,win1,0,0) Do
  3.  
means it loops only if the first message is not 0,
and then all further are not 0.
when comes my msg's?
and why at the end of program?
and i said the program reacts on nothing anymore.
and for what i shall make all messages, which i am not
interested in?
You need to read a book that answers basic Windows programming questions such as those.  If you are familiar with C, I recommend Petzold classic "Programmng Windows" (5th edition)

If you want to see how a basic Windows GUI program is put together using FPC, search these forums for "WinAPI", you'll find examples coded in Pascal/FPC (but the reader is expected to know and understand the basics of Windows programming which, no offense, you don't seem to grasp, therefore my recommendation of Petzold's book.)

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

turunk

  • New member
  • *
  • Posts: 9
Re: my programs crash
« Reply #14 on: February 13, 2025, 01:48:49 am »
is the forum not replacing the book?
was that not simply enough, that everybody know how to code this?
with what must the queue filled up and for whom?
i ask there only for mouse and keyboard, but the program crashes.
this problem is not solved, and why?
and why the window-handle needs informations, when the
code-part shall know keyboard and mouse instead all?
what can be messaged around no participiant parts of what ever,
so that it not crash?

 

TinyPortal © 2005-2018