Atozed Forums
Problem with IdHTTPServer in Lazarus - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Indy (https://www.atozed.com/forums/forum-8.html)
+--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html)
+--- Thread: Problem with IdHTTPServer in Lazarus (/thread-2520.html)



Problem with IdHTTPServer in Lazarus - BartKindt - 10-06-2021

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?


RE: Problem with IdHTTPServer in Lazarus - Robert Gilland - 10-07-2021

I use fpc_http_server in Web services Tool kit look in: https://forum.lazarus.freepascal.org/index.php?topic=37285.0 
which uses Lazarus own web server component  instead works like magic fphttpserver


RE: Problem with IdHTTPServer in Lazarus - BartKindt - 10-07-2021

(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/index.php?topic=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


RE: Problem with IdHTTPServer in Lazarus - rlebeau - 10-07-2021

(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;



RE: Problem with IdHTTPServer in Lazarus - BartKindt - 10-07-2021

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....


RE: Problem with IdHTTPServer in Lazarus - rlebeau - 10-07-2021

(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.


RE: Problem with IdHTTPServer in Lazarus - BartKindt - 10-07-2021

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