Solved

Get windows temp dir(16-bit)

Posted on 1998-08-27
14
401 Views
Last Modified: 2012-05-04
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(pWinTempDir), pWinTempDir);
   Result := AddSlashIfNeeded(StrPas(pWinTempDir));
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(VarStr) + 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}
0
Comment
Question by:tomcorcoran
  • 3
  • 3
  • 3
  • +3
14 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1337938
I think in 16 bit you shoud use GetTempDrive
0
 

Author Comment

by:tomcorcoran
ID: 1337939
Ronit, I have played with that, that gives the drive - how would you suggest get the path? Tom.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1337940
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
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1337941
Opps.......I didn't see your code....sorry...You've used exactly what I gave you..please reject my answer.. :(

Regards,
Viktor Ivanov
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1337942
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
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1337943
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
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1337944
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tomcorcoran
ID: 1337945
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.
0
 
LVL 2

Accepted Solution

by:
kjteng earned 50 total points
ID: 1337946
1. You forgot to assign the result for GetWinTempDir
2. use strAlloc and strDispose

Admende code:


function GetWinTempDir: string;
      { Function which retrieves the value of a DOS environment variable }
      function GetEnvVar(const VarStr: string): string;
      var
         PEnv, PVarStr, PEquals: PChar;  s:string;
      begin
         Result := '';

         PVarStr := StrAlloc(Length(VarStr) + 1);
           s:= StrPas( pVarStr);
         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
            StrDispose(PVarStr);
         end;
      end;
   begin
      Result := GetEnvVar('TEMP');
   end;

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1337947
tom, tried my code??
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1337948
Hi Tom, does it work?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1337949
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
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1337950
Did it helped?
0
 

Author Comment

by:tomcorcoran
ID: 1337951
Kineng, I can't believe I overlooked that, thanks a million. Ronnit, thatns for the link, interesting stuff. Tom.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 92
code issue 8 99
Can Viruses spread while transferring Binary data with Winsock 2 66
Firemonkey webbrowser scrollbars ? 1 29
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now