boardtc
asked on
Get windows temp dir(16-bit)
I use the follwoing routines to get the windows temp directory, however the 16-bit version hangs my machine, has anyone any ideas why? Thansk a lot, Tom.
{$IFDEF WIN32}
function GetWinTempDir: string;
var
pWinTempDir: array[0..255] of Char;
begin
GetTempPath(SizeOf(pWinTem pDir), pWinTempDir);
Result := AddSlashIfNeeded(StrPas(pW inTempDir) );
end;
{$ELSE}
{ debug - does not work }
function GetWinTempDir: string;
{ Function which retrieves the value of a DOS environment variable }
function GetEnvVar(const VarStr: string): string;
var
PEnv, PVarStr, PEquals: PChar;
begin
Result := '';
PVarStr := AllocateSpace(Length(VarSt r) + 1);
try
{ Copy pascal-style string into a null-terminated one }
StrPCopy(PVarStr, VarStr);
{ Get the DOS environment strings }
PEnv := GetDOSEnvironment;
{ Step through each individual environment string and look for
the one which is required }
while (PEnv^ <> #0) do begin
{ Find the position of '=' in the string }
PEquals := StrScan(PEnv, '=');
if (PEquals <> nil) then begin
if (StrLIComp(PEnv, PVarStr, (PEquals - PEnv)) = 0) then begin
{ Found it! }
Inc(PEquals, 1);
Result := StrPas(PEquals);
Break;
end;
end;
Inc(PEnv, StrLen(PEnv) + 1);
end;
finally
FreeSpace(PVarStr, Length(VarStr) + 1);
end;
end;
begin
GetEnvVar('TEMP');
end;
{$ENDIF}
{$IFDEF WIN32}
function GetWinTempDir: string;
var
pWinTempDir: array[0..255] of Char;
begin
GetTempPath(SizeOf(pWinTem
Result := AddSlashIfNeeded(StrPas(pW
end;
{$ELSE}
{ debug - does not work }
function GetWinTempDir: string;
{ Function which retrieves the value of a DOS environment variable }
function GetEnvVar(const VarStr: string): string;
var
PEnv, PVarStr, PEquals: PChar;
begin
Result := '';
PVarStr := AllocateSpace(Length(VarSt
try
{ Copy pascal-style string into a null-terminated one }
StrPCopy(PVarStr, VarStr);
{ Get the DOS environment strings }
PEnv := GetDOSEnvironment;
{ Step through each individual environment string and look for
the one which is required }
while (PEnv^ <> #0) do begin
{ Find the position of '=' in the string }
PEquals := StrScan(PEnv, '=');
if (PEquals <> nil) then begin
if (StrLIComp(PEnv, PVarStr, (PEquals - PEnv)) = 0) then begin
{ Found it! }
Inc(PEquals, 1);
Result := StrPas(PEquals);
Break;
end;
end;
Inc(PEnv, StrLen(PEnv) + 1);
end;
finally
FreeSpace(PVarStr, Length(VarStr) + 1);
end;
end;
begin
GetEnvVar('TEMP');
end;
{$ENDIF}
I think in 16 bit you shoud use GetTempDrive
ASKER
Ronit, I have played with that, that gives the drive - how would you suggest get the path? Tom.
How about this code....???
procedure TForm1.Button1Click(Sender : TObject);
var
Buffer : array[0..255] of char;
begin
GetTempPath(SizeOf(Buffer) , Buffer);
ShowMessage(Buffer);
end;
Hope this helps =)
Regards,
Viktor Ivanov
procedure TForm1.Button1Click(Sender
var
Buffer : array[0..255] of char;
begin
GetTempPath(SizeOf(Buffer)
ShowMessage(Buffer);
end;
Hope this helps =)
Regards,
Viktor Ivanov
Opps.......I didn't see your code....sorry...You've used exactly what I gave you..please reject my answer.. :(
Regards,
Viktor Ivanov
Regards,
Viktor Ivanov
Hi Tom,
I'm not sure, but isn't the GetTempPath API function also available in Win16?
This is some old code I found (similar to yours):
{$IFNDEF WIN32}
const MAX_PATH = 144;
{$ENDIF}
function GetTempDirectory: String;
var
TempDir: array[0..MAX_PATH] of Char;
begin
GetTempPath(MAX_PATH, @TempDir); // remember the @ operator
Result := StrPas(TempDir);
end;
/// John
I'm not sure, but isn't the GetTempPath API function also available in Win16?
This is some old code I found (similar to yours):
{$IFNDEF WIN32}
const MAX_PATH = 144;
{$ENDIF}
function GetTempDirectory: String;
var
TempDir: array[0..MAX_PATH] of Char;
begin
GetTempPath(MAX_PATH, @TempDir); // remember the @ operator
Result := StrPas(TempDir);
end;
/// John
Hey actually why not try it and see if it works..It's got a slash on the back so you don;t need ot worry about that..Just try if the simple way I gave you works...You've used so much of code..I wonder why???
Regards,
Viktor Ivanov
Regards,
Viktor Ivanov
Hi Tom,
Can you try this one? I don't have 16bit OS anymore :
function GetEnvVar(VarName: PChar): PChar;
var
L: Word;
P: PChar;
begin
L := StrLen(VarName);
P := GetDosEnvironment;
while P^ <> #0 do
begin
if (StrLIComp(P, VarName, L) = 0) and (P[L] = '=') then
begin
GetEnvVar := P + L + 1;
Exit;
end;
Inc(P, StrLen(P) + 1);
end;
GetEnvVar := nil;
end;
GetEnvVar('TEMP')
or/and GetEnvVar('TMP')
if returns nil --> not temp dir.
Regards, Zif.
Can you try this one? I don't have 16bit OS anymore :
function GetEnvVar(VarName: PChar): PChar;
var
L: Word;
P: PChar;
begin
L := StrLen(VarName);
P := GetDosEnvironment;
while P^ <> #0 do
begin
if (StrLIComp(P, VarName, L) = 0) and (P[L] = '=') then
begin
GetEnvVar := P + L + 1;
Exit;
end;
Inc(P, StrLen(P) + 1);
end;
GetEnvVar := nil;
end;
GetEnvVar('TEMP')
or/and GetEnvVar('TMP')
if returns nil --> not temp dir.
Regards, Zif.
ASKER
Thanks guys, I've used so much code tryiung to get around the fact that GetTempPath is not a Win16 call, Victornet - can you do it with less? I'll look at yuor suggestions later, Thanks, Tom.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
tom, tried my code??
Hi Tom, does it work?
Use GetTempFileName:
Create a new temporary file by using GetTempFileName and then find it's directory which is the Temp directory by using a function like ExtractFileDir.
See the following discussion:
http://x3.dejanews.com/getdoc.xp?AN=285436267&CONTEXT=904457882.687407192&hitnum=17&AH=1
Create a new temporary file by using GetTempFileName and then find it's directory which is the Temp directory by using a function like ExtractFileDir.
See the following discussion:
http://x3.dejanews.com/getdoc.xp?AN=285436267&CONTEXT=904457882.687407192&hitnum=17&AH=1
Did it helped?
ASKER
Kineng, I can't believe I overlooked that, thanks a million. Ronnit, thatns for the link, interesting stuff. Tom.