Recent

Author Topic: Global variables in a Lazarus service (TDaemon)  (Read 554 times)

TyneBridges

  • Full Member
  • ***
  • Posts: 143
    • Personal blog
Global variables in a Lazarus service (TDaemon)
« on: August 18, 2020, 06:06:50 pm »
I'm creating a Windows service with a lot of help from forum members. I need to pass a value returned by the DaemonMapper to DaemonUnit1. I understand that any variable declared with var in the Interface section should be global in scope. However, no matter whether I put the
Code: Pascal  [Select][+][-]
  1. var NMsg: String;
in the Interface of DaemonMapperUnit1 or in the same section of DaemonUnit1, I get "Error: identifier not found" when I try to compile.

I understand that it should be in whichever unit is initialised first, but I've tried it in both and it works in neither. Any ideas?

Thanks.
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

lucamar

  • Hero Member
  • *****
  • Posts: 3440
Re: Global variables in a Lazarus service (TDaemon)
« Reply #1 on: August 18, 2020, 06:35:16 pm »
This is very basic so excuse me if you already knew, but ...

You have to add the unit in which it's declared to any other unit who uses it. You do this by adding the unit's name to the uses  clause:

Code: Pascal  [Select][+][-]
  1. unit unit1;
  2. interface
  3.   var somevar: string;
  4. implementation
  5. end.
  6.  
  7. ----------
  8.  
  9. unit unit2;
  10.  
  11. interface
  12.   {... whatever ...}
  13.  
  14. implementation
  15.  
  16. uses unit1;
  17. procedure SomeProc;
  18. begin
  19.   somevar := 'soemthing or other';
  20. end;
  21.  
  22. end.

Do note that "global in scope" in this case just means it's global in the unit in which it's declared (that's the "scope") as opossed to a function-local var  or a variable (or const/type) declared only in the implementation section.

Declaring something in the "interface" allows you to refer to it elsewhere (i.e. other units) but you must declare "elsewhere" where it should be searched for, in the uses clause.
« Last Edit: August 18, 2020, 06:41:53 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

TyneBridges

  • Full Member
  • ***
  • Posts: 143
    • Personal blog
Re: Global variables in a Lazarus service (TDaemon)
« Reply #2 on: August 18, 2020, 08:03:10 pm »
This is very basic so excuse me if you already knew, but ...

You have to add the unit in which it's declared to any other unit who uses it. You do this by adding the unit's name to the uses  clause:

Thanks! I used to know that but it's so long since I started any new projects that I'm having to re-learn some of the basic stuff. I should have realised, but I'm grateful for your help.
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

lucamar

  • Hero Member
  • *****
  • Posts: 3440
Re: Global variables in a Lazarus service (TDaemon)
« Reply #3 on: August 18, 2020, 08:13:33 pm »
Glad to be of help :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2603
  • Compiler Developer
Re: Global variables in a Lazarus service (TDaemon)
« Reply #4 on: August 19, 2020, 10:33:12 am »
I'm creating a Windows service with a lot of help from forum members. I need to pass a value returned by the DaemonMapper to DaemonUnit1. I understand that any variable declared with var in the Interface section should be global in scope. However, no matter whether I put the
Code: Pascal  [Select][+][-]
  1. var NMsg: String;
in the Interface of DaemonMapperUnit1 or in the same section of DaemonUnit1, I get "Error: identifier not found" when I try to compile.

What exactly are you trying to forward? Maybe there is a better method to do this than a global variable.

TyneBridges

  • Full Member
  • ***
  • Posts: 143
    • Personal blog
Re: Global variables in a Lazarus service (TDaemon)
« Reply #5 on: August 19, 2020, 05:09:56 pm »
I'm creating a Windows service with a lot of help from forum members. I need to pass a value returned by the DaemonMapper to DaemonUnit1. I understand that any variable declared with var in the Interface section should be global in scope. However, no matter whether I put the
Code: Pascal  [Select][+][-]
  1. var NMsg: String;
in the Interface of DaemonMapperUnit1 or in the same section of DaemonUnit1, I get "Error: identifier not found" when I try to compile.

What exactly are you trying to forward? Maybe there is a better method to do this than a global variable.

Thanks. I was trying to get the value returned by the service in a control event to my log file created in the other unit. It turned out that I had the control event code in the wrong unit anyway (see my logon/logoff service thread) and even simplified code has other problems...
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

 

TinyPortal © 2005-2018