Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with IdHTTPServer in Lazarus
#1
I am writing for the first time in Lazarus Pascal, instead of Delphi (because it is for a Raspberry Pi).

I use the same code as in my Delphi Appllications, but this does not work in Lazarus:

Code:
TDebugLogEventHandlers = class
   
    // HTTP
    class procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    class procedure IdHTTPServer1Exception(AContext: TIdContext; AException: Exception);
   end;

I get a 'Wrong number of parameters...' on all these lines.

What is the Lazarus compatible format for this?
---
Bart Kindt
CEO and Developer
SARTrack Limited
New Zealand
www.sartrack.nz
Reply
#2
I use fpc_http_server in Web services Tool kit look in: https://forum.lazarus.freepascal.org/ind...ic=37285.0 
which uses Lazarus own web server component  instead works like magic fphttpserver
Reply
#3
(10-07-2021, 12:15 AM)Robert Gilland Wrote: I use fpc_http_server in Web services Tool kit look in: https://forum.lazarus.freepascal.org/ind...ic=37285.0 
which uses Lazarus own web server component  instead works like magic fphttpserver

Thanks,

I will look at it, but this is just one of the TCP connections I need to use in the application, I also have a raw TCP Server in use, same problem.
As I am familiar with Indy10 for many years, I would like to stick with it.

Thanks, Bart
---
Bart Kindt
CEO and Developer
SARTrack Limited
New Zealand
www.sartrack.nz
Reply
#4
(10-06-2021, 11:57 PM)BartKindt Wrote: I use the same code as in my Delphi Appllications, but this does not work in Lazarus:
...
I get a 'Wrong number of parameters...' on all these lines.

Those lines are just declarations, so you can't get such an error on them.  You must be getting errors when trying to use the TDebugLogEventHandlers class elsewhere in your code.  In particular, which {$mode} are you compiling in? I am assuming it is NOT {$mode delphi}. If so, then you need to use the @ address operator when assigning the event handlers. See Lazarus - why I can't assign event to run-time component?

For example:

Code:
type
  TDebugLogEventHandlers = class
  public
    class procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    class procedure IdHTTPServer1Exception(AContext: TIdContext; AException: Exception);
  end;

...

IdHTTPServer1.OnCommandGet := @TDebugLogEventHandlers.IdHTTPServer1CommandGet;
IdHTTPServer1.OnException := @TDebugLogEventHandlers.IdHTTPServer1Exception;

Reply
#5
Sorry, I forgot to add the code where it actually goes wrong.
And that is indeed at:
IdHTTPServer1.OnCommandGet := TDebugLogEventHandlers.IdHTTPServer1CommandGet;
IdHTTPServer1.OnException := TDebugLogEventHandlers.IdHTTPServer1Exception;

I already did try:
IdHTTPServer1.OnCommandGet := @TDebugLogEventHandlers.IdHTTPServer1CommandGet;
IdHTTPServer1.OnException := @TDebugLogEventHandlers.IdHTTPServer1Exception;

But now I got:
Error: Incompatible types: got "<class method type of procedure(TIdContext;TIdHTTPRequestInfo;TIdHTTPResponseInfo) of object;StdCall>" expected "<procedure variable type of procedure(TIdContext;TIdHTTPRequestInfo;TIdHTTPResponseInfo) of object;StdCall>"

So I compile for Linux, Raspberry Pi.
Yes, in "objfpc mode"

I got the compiling fixed by doing this:

var
DebugLogEventHandlers: TDebugLogEventHandlers;

IdHTTPServer1.OnCommandGet := @DebugLogEventHandlers.IdHTTPServer1CommandGet;

That fixed the compiling error.
But now I have major issues with the actual compiling of Indy10.
I will start a new post for this....
---
Bart Kindt
CEO and Developer
SARTrack Limited
New Zealand
www.sartrack.nz
Reply
#6
(10-07-2021, 03:00 AM)BartKindt Wrote: But now I got:
Error: Incompatible types: got "<class method type of procedure(TIdContext;TIdHTTPRequestInfo;TIdHTTPResponseInfo) of object;StdCall>" expected "<procedure variable type of procedure(TIdContext;TIdHTTPRequestInfo;TIdHTTPResponseInfo) of object;StdCall>"

Why is the calling convention set to StdCall? I would have expected the default Register instead.

In any case, try one of these workarounds:

Code:
IdHTTPServer1.OnCommandGet := TIdHTTPCommandEvent(@TDebugLogEventHandlers.IdHTTPServer1CommandGet);
IdHTTPServer1.OnException := TIdServerThreadExceptionEvent(@TDebugLogEventHandlers.IdHTTPServer1Exception);

Code:
var
  M: TMethod;
begin
  M.Data := TDebugLogEventHandlers;
  M.Code := @TDebugLogEventHandlers.IdHTTPServer1CommandGet;
  IdHTTPServer1.OnCommandGet := TIdHTTPCommandEvent(M);

  M.Data := TDebugLogEventHandlers;
  M.Code := @TDebugLogEventHandlers.IdHTTPServer1Exception;
  IdHTTPServer1.OnException := TIdServerThreadExceptionEvent(M);
end;

Code:
var
  OnCommandEvent: TIdHTTPCommandEvent;
  OnExceptionEvent: TIdServerThreadExceptionEvent;
begin
  TMethod(OnCommandEvent).Data := TDebugLogEventHandlers;
  TMethod(OnCommandEvent).Code := @TDebugLogEventHandlers.IdHTTPServer1CommandGet;
  IdHTTPServer1.OnCommandGet := OnCommandEvent;

  TMethod(OnExceptionEvent).Data := TDebugLogEventHandlers;
  TMethod(OnExceptionEvent).Code := @TDebugLogEventHandlers.IdHTTPServer1Exception;
  IdHTTPServer1.OnException := OnExceptionEvent;
end;

Or, you could simply compile the unit in {$mode Delphi}, in which case the original code without @ should work.

Reply
#7
You probably missed the end of my previous post:

Your solutions would probably work, but I did it this way, and is works fine now:

var
DebugLogEventHandlers: TDebugLogEventHandlers;

IdHTTPServer1.OnCommandGet := @DebugLogEventHandlers.IdHTTPServer1CommandGet;

It is just that Lazarus does not like a direct call to TDebugLogEventHandlers or @TDebugLogEventHandlers

Thanks Remy
---
Bart Kindt
CEO and Developer
SARTrack Limited
New Zealand
www.sartrack.nz
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)