Solved

Find windows directory

Posted on 2002-05-03
8
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

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!

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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