On Wed, 23 Oct 1996 02:03:57 -0400 (EDT), John Michael Black jm_black@csunix1.lvc.edu wrote:
There's been a lot of getenvs going around here lately. :) Has anyone found a method that works yet? The last thing posted about it gave this:
Function GetEnv ( Name: __CString__ ): PChar; C;
GPC does not recognize PChar -- at least ours doesn't.
This could go on for weeks.... :)
Naah....
Take this:
(* * Demonstrate the conversion of "Pascal" string schemas to "C" style * strings and back, and calling an external "C" style function. * * J.J. van der Heijden j.j.vanderheijden@student.utwente.nl *)
Program EnvDemo;
type word = __unsigned__ integer; TString = string(256); { Pascal string schema } CString = __cstring__; { C style string }
{ Convert a "C" string to a "Pascal" string } function StrPas(Src: CString): TString; var S : TString; begin S := ''; if (Src <> NIL) then while ( (Src^ <> chr(0)) AND (length(S) < S.capacity)) do begin S := S + Src^; inc(Word(Src)); end; StrPas := S; end;
{ Convert a "Pascal" string to a "C" string } function StrPCopy(Dest: CString; Src: String): CString; var c: integer; p : CString; begin p := Dest; for c:=1 to length(Src) do begin p^ := Src[c]; inc(word(p)) end; p^ := chr(0); StrPCopy := Dest; end;
{ C library function prototype: char *getenv(const char *name); } function GetEnv(name : CString): CString; C;
var pName: CString;
begin getmem(pName, 256);
pName := StrPCopy(pName, 'USER'); writeln('Hello ', StrPas(GetEnv(pName)), ' !');
pName := StrPCopy(pName, 'PATH'); writeln('Your PATH is: ', StrPas(GetEnv(pName)));
freemem(pName, 256); end.
There are multiple ways to do this: for example, you can also declare a
type PChar = ^char;
and replace every instance of CString with PChar. Works great. Do not pass a CString argument as a "var" if it's declared as "const char *" in C; you will definately get a coredump. (in C too BTW)
I tested this with Linux and djgpp (dos) GPC, 1.2 prerelease. The GPC-1.1 may have troubles with the function returning a TString schema. Let me know if it doesn't work for you, though I can't imagine that.
JanJaap
--- "Nothing shocks me, I'm a scientist", Indiana Jones