Solved

Find windows directory

Posted on 2002-05-03
8
164 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

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

'c:\'

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

735 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