Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find windows directory

Posted on 2002-05-03
8
Medium Priority
?
167 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 200 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
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!

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

670 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