Интернет решения от доктора Боба | страница 27
2.2.3. Переменные среды
Стандартное CGI приложение должно анализировать переменные среды для определения метода передачи и размера посылаемой информации через стандартный ввод. Для получения списка переменных среды я всегда использую простой компонент, который я написал очень давно и компилирую его с помощью директив условной компиляции, как в Дельфи 1, так и в Дельфи 2.
> unit TBDosEnv;
> interface
> uses
> SysUtils, WinTypes, WinProcs, Classes;
> type
> TBDosEnvironment = class(TComponent)
> public
> { Public class declarations (override) }
> constructor Create(AOwner: TComponent); override;
> destructor Destroy; override;
> private
> { Private field declarations }
> FDosEnvList: TStringList;
> protected
> { Protected method declarations }
> function GetDosEnvCount: Word;
> public
> { Public interface declarations }
> function GetDosEnvStr(Const Name: String): String;
> { This function is a modified version of the GetEnvVar function that
> appears in the WinDos unit that comes with Delphi. This function's
> interface uses Pascal strings instead of null-terminated strings.
> }
> property DosEnvCount: Word read GetDosEnvCount;
> property DosEnvList: TStringList read FDosEnvList;
> end;
> implementation
> constructor TBDosEnvironment.Create(AOwner: TComponent);
> var P: PChar;
> i: Integer;
> begin
> inherited Create(AOwner);
> FDosEnvList := TStringList.Create;
> {$IFDEF WIN32}
> P := GetEnvironmentStrings;
> {$ELSE}
> P := GetDosEnvironment; { Win API }
> {$ENDIF}
> i := 0;
> while P^ <> #0 do
> begin
> Inc(i);
> FDosEnvList.Add(StrPas(P));
> Inc(P, StrLen(P)+1) { Fast Jump to Next Var }
> end;
> end{Create};
> destructor TBDosEnvironment.Destroy;
> begin
> FDosEnvList.Free;
> FDosEnvList := nil;
> inherited Destroy
> end{Destroy};
> function TBDosEnvironment.GetDosEnvCount: Word;
> begin
> Result := 0;
> if Assigned(FDosEnvList) then Result := FDosEnvList.Count
> end{GetDosEnvCount};
> function TBDosEnvironment.GetDosEnvStr(Const Name: String): String;
> var i: Integer;
> Tmp: String;
> begin
> i := 0;
> Result := '';
> if Assigned(FDosEnvList) then while i
> begin
> Tmp := FDosEnvList[i];
> Inc(i);
> if Pos(Name,Tmp) = 1 then
> begin
> Delete(Tmp,1,Length(Name));
> if Tmp[1] = '=' then
> begin
> Delete(Tmp,1,1);
> Result := Tmp;