Solved

Find windows directory

Posted on 2002-05-03
8
160 Views
Last Modified: 2010-04-04
I nee some code to find the Windows/WinNT directory.

I tried this one but it's gives me an error :(

function GetWindowsDirectory(lpBuffer: PChar; uSize: UINT): UINT; stdcall;

Function GetWindowsDir:String;
VAR
 Buffer : Array[0..150] OF Char;
Begin
 FillChar(Buffer,SizeOf(Buffer),0);
 GetWindowsDirectory ( Buffer,sizeOf(Buffer));
 Result := StrPas(Buffer );
End;

Anyone else out ther with the good code?

Thanks,
Vincent
0
Comment
Question by:Vincentg
8 Comments
 
LVL 6

Accepted Solution

by:
DrDelphi earned 50 total points
ID: 6988533
Straight simple and to the point:




Function WindowsDirectory:string;
var WinDir:array[0..255] of char;
begin
  GetwindowsDirectory(Windir,255);
  Result:=WinDir;
end;



Good luck!!


0
 
LVL 4

Expert Comment

by:nestorua
ID: 6988647
HI,
Almost the same as DrDelphi:

function GetWinDir: string;
var lpBuffer: PChar;
       uSize: UINT;
begin
  uSize:=MAX_PATH;
  GetMem(lpBuffer, uSize);
  GetWindowsDirectory(lpBuffer, uSize);
  Result:=String(lpBuffer);
  FreeMem(lpBuffer);
end;

Sincerely,
Nestorua.
0
 
LVL 7

Expert Comment

by:Motaz
ID: 6988714
This is my version:

var
  WinDir: PChar;
begin
  WinDir:= StrAlloc(100);
  GetWindowsDirectory(WinDir, 100);
  Label1.Caption:= WinDir;
  StrDispose(WinDir);
end;

btw. No need to declare GetWindowsDirectory header, just make sure that Windows unit is included in uses clause.
Motaz
0
 
LVL 2

Expert Comment

by:dMa
ID: 6988790
and this is my version :
(ok it works only on 99% )

'c:\'

heheheheheehehe
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.

 
LVL 8

Expert Comment

by:Cesario
ID: 6990575
change your code :

Function GetWindowsDir:String;
VAR
  Buffer : Array[0..150] OF Char;
Begin
  FillChar(Buffer,SizeOf(Buffer),0);
  GetWindowsDirectory ( @Buffer,sizeOf(Buffer));
  Result := StrPas(Buffer );
End;

Best Regards

Cesario
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6991065
function GetWindowsDir: string;
begin
  SetLength(Result, MAX_PATH{260});
  SetLength(Result, GetWindowsDirectory(PChar(Result), MAX_PATH));
end;

:)
0
 

Author Comment

by:Vincentg
ID: 6994307
Thanks a lot :)
All others also many thanks for replying.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7006547
Cheers.
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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 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

22 Experts available now in Live!

Get 1:1 Help Now