Recent

Author Topic: Call to Windows API  (Read 14152 times)

Laurie

  • New Member
  • *
  • Posts: 19
Call to Windows API
« on: June 30, 2009, 03:59:42 am »
I'm porting code from Delphi 3.
This line (which compiles in Delphi 3) :
    while PeekMessage(M, 0, 0, 0, PM_Remove) do....
where M is declared as: Var     M : tagMSG;

gives the following error:
URLock.pas(55,22) Error: Call by var parameters have to match exactly: Got "MSG" expected "tagMSG"

My unit uses   LCLIntf and LCLType {for tagMSG}.

This is driving me mad.
Can anyone help?
Cheers,
Laurie.

icebit

  • New Member
  • *
  • Posts: 20
Re: Call to Windows API
« Reply #1 on: June 30, 2009, 04:08:52 am »
How PeekMessage procedure parameters are defined ? Maybe you can cast M to MSG, like:

... PeekMessage(MSG(M),0,0,0,PM_Remove) ...

 :)

Laurie

  • New Member
  • *
  • Posts: 19
Re: Call to Windows API
« Reply #2 on: June 30, 2009, 04:16:36 am »
Casting with either MSG(M) or tagMSG(M) causes exactyl the same error!!!

eg while PeekMessage(MSG(M),0,0,0,pm_remove)....

I'm losing hair here.

Laurie.

picstart

  • Full Member
  • ***
  • Posts: 236
Re: Call to Windows API
« Reply #3 on: June 30, 2009, 04:23:31 am »
did you try {$ delphi} instead of {$ objfpc} in your unit.

icebit

  • New Member
  • *
  • Posts: 20
Re: Call to Windows API
« Reply #4 on: June 30, 2009, 05:05:04 am »
TWidgetSet.PeekMessage

Declaration
Source position: winapih.inc line 184

public function TWidgetSet.PeekMessage(
  var lpMsg: TMsg;
  Handle: HWND;
  wMsgFilterMin: UINT;
  wMsgFilterMax: UINT;
  wRemoveMsg: UINT
):Boolean; virtual;
 
Well, is a good habit to look on lazarus help first  :D

So declare M as TMSG, and try again

Laurie

  • New Member
  • *
  • Posts: 19
Re: Call to Windows API
« Reply #5 on: June 30, 2009, 10:03:01 am »
Yes picstart, I tried {$ delphi} and {$MODE Delphi}.
Yes icebit, I checked help first and declared M as both TMSG and tagMSG. I've also cast M as these types as well.
Still no luck....all help appreciated.
Laurie.

Here is procedure if anyone can help...I call it whilst waiting for a locked record to become unlocked.

uses
  LCLIntf, LCLType {for tagMSG}, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls,  ErrorStuf, ExtCtrls, LResources, Buttons;   

Procedure Breathe;  //Allow other pending tasks to run
var
  M: TMsg; //but also tried  M : tagMSG;
begin
  while PeekMessage(M, 0, 0, 0, PM_Remove) do
  begin
    if M.Message = wm_Quit then Halt(255); //Terminate;
    TranslateMessage(M);
    DispatchMessage(M);
  end;
  FRLock.Timer1.Enabled := true;
  MessageBeep($FFFFFFFF{-1});
  repeat //wait until timer finishes
    Application.ProcessMessages;
  until FRLock.Timer1.Enabled = false;
  //put a wait or delay here
end;

tech-pro

  • Full Member
  • ***
  • Posts: 173
    • Tech-Pro.net
Re: Call to Windows API
« Reply #6 on: June 30, 2009, 11:24:25 am »
And what error message do you get when you compile this version?

I searched the LCL source code for an example of calling PeekMessage and found one like this:

while PeekMessage(AMessage, HWnd(nil), 0, 0, PM_REMOVE) do

where AMessage is declared as TMsg.
Julian

Laurie

  • New Member
  • *
  • Posts: 19
Re: Call to Windows API
« Reply #7 on: June 30, 2009, 11:30:20 am »
Hi Julian

Here is the error:  Just the same.

UtestAPI.pas(44,30) Error: Call by var parameters have to match exactly: Got "MSG" expected "tagMSG"

If you wich to compile it add this prior to procedure declaration:
CONST
  pm_remove = 1;   //for delphi is in Windows.pm_remove 

I'm not sure where to go from here....

Laurie

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Call to Windows API
« Reply #8 on: June 30, 2009, 12:23:33 pm »
Instead of the PeekMessage loop, why not just call Application.ProcessMessages?

Laurie

  • New Member
  • *
  • Posts: 19
Re: Call to Windows API
« Reply #9 on: June 30, 2009, 02:01:36 pm »
Hi Vincent

Yes that will compile.  It would defeat the purpose of the peekmessage() loop, which is to exit/abort if a WM_Quit message is in the queue.

More importantly it does not explain why the compiler claims that peekmessage(M,0,0,0,1) {where M is type TMSG or type tagMSG} is an error.  I'm starting to suspect that this is a bug.

Any thoughts?
Laurie.

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Call to Windows API
« Reply #10 on: June 30, 2009, 02:25:47 pm »
Well, I fear other thing may go wrong if you start using your own message loop.

The error is caused by the fact that PeekMessage from the LCLIntf unit is using TMsg from the LCLType unit, while in your program you declared a TMsg from the message unit. Either reorder the units in the usage clause or explicitly qualify the type with a unit name: LCLType.TMsg.

I wonder where you will try to get TranslateMessage and DispatchMessage. Windows unit? If so beware of further naming conflict with the LCLType unit.


Laurie

  • New Member
  • *
  • Posts: 19
Re: Call to Windows API
« Reply #11 on: June 30, 2009, 02:36:33 pm »
Hi Vincent...
 
BINGO...you got it!

I qualified the type with
    M: LCLtype.TMSG;    {instead of M: TMSG}

and it compiled.

Phew...I was worried that it was a bug.
You are also correct about translateMessage() and dispatchMessage().  I'll have to re-think this.
Many thanks for your help Vincent.

Laurie.

Laurie

  • New Member
  • *
  • Posts: 19
Re: Call to Windows API
« Reply #12 on: July 01, 2009, 12:10:26 am »
I'm getting the hang if Lazarus but perhaps I still need a bit of prompting.
I'm trying to call the Windows API function LockFile
It is defined in func.inc as:
function LockFile(hFile:HANDLE; dwFileOffsetLow:DWORD; dwFileOffsetHigh:DWORD; nNumberOfBytesToLockLow:DWORD; nNumberOfBytesToLockHigh:DWORD):WINBOOL; external 'kernel32' name 'LockFile';

But how do I use func.inc ?
I've done {$I func.inc} where the include path is: C:\lazarus\fpc\2.0.4\source\rtl\win32\wininc

Including it gives the error when LockFile is encountered:
URLock.pas(126,6) Error: Identifier not found "LockFile"

All help appreciated.

Laurie.

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Call to Windows API
« Reply #13 on: July 01, 2009, 08:14:21 am »
LockFile is declared in the windows unit.

 

TinyPortal © 2005-2018