I think better OO solution is to create wrapper method around the external function.

function getenv(const Name: PChar): PChar; cdecl; external 'c';

TEnvironment = class(TObject)
public
 function GetEnvironment(const AName: String): String;
end;

function TEnvironment.GetEnvironment(const AName: String): String;
begin
 Result := String(getenv(PChar(AName)));
end;

Tomas Stejskal

Amir Aavani wrote:

dear friends,
I know that if want to use an external variable/procedure in fpc, i should use the following code:

function GetEnvironment (const Name: PChar): PChar; cdecl; external 'c' name 'getenv';

but how could i add for example GetEnvironment function to a class (TEnvironment)!
Is there any good (OO) way?

Is it a good idea to use a following method:

TGetEnvironmentFunction= function : PChar;
TEnvironment= class (TObject)
private
 ...
public
 GetEnvironment: TGetEnvironment;
procedure SetGetEnvironmentFunction (AFunction: TGetEnvironmentFunction);
 ...
end;

procedure TEnvironment.SetGetEnvironmentFunction (AFunction: TGetEnvironmentFunction);
begin
 GetEnvironment:= AFunction;
end;



_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to