Solved

how to make function to work?

Posted on 1998-01-23
8
187 Views
Last Modified: 2010-04-04
I have this code for finding the windows system directory

var
WinDirLen: Integer;
WinDir: PChar;
dir : string;
begin
   GetMem(WinDir, 255);
   WinDirLen := GetSystemDirectory(WinDir,255);
   dir := Windir;
   FreeMem(WinDir);

and use the 'dir' like this  
   Image1.Picture.Bitmap.LoadFromfile(dir + \1.bmp);

But I need this code for finding the windows system dir many times. writing to every event that needs it makes my code large so I need to put it in a function or procedure so I only need to call it once.

I tried a few times, but I don't seem to get it to work. Can someone please fill it in for me. I give the maximum of points, I only ask that the function or procedure is complete so that I don't have to add anything. and then show me how to call it using the Image1.Picture.Bitmap.Loadfromfile example
thanks
0
Comment
Question by:hellfire052497
[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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Accepted Solution

by:
efortier earned 200 total points
ID: 1357334
Hellfire,

Here is a complete function working in Delphi:

function GetWinSysDir: string;
var
  WinDir: array[0..255] of char;
begin
  if GetSystemDirectory(WinDir,255)=0 then
    result:=''
  else  
    result:=strpas(WinDir);
end;

you don't really need to allocate any memory, as the WinDir is already allocated 255 bytes!

Then, simply enter:

Image1.picture.loadfromfile(GetWinSysDir+'\image.bmp');

if GetWinSysDir returns an empty string(ie: ''), it means that the windows\system directory could not be found.

Eric
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357335
If your code works then this one should work:

Function WinSysDir: String;
var
  WinDirLen: Integer;
  WinDir: PChar;
  dir : string;
begin
   GetMem(WinDir, 255);
   WinDirLen := GetSystemDirectory(WinDir,255);
   Result := Windir;
   FreeMem(WinDir);
end;

and then you call
Image1.Picture.Bitmap.LoadFromfile(WinSysDir + '\1.bmp')

Good luck,
Matvey
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357336
Hmmm, it seemes that while I was writing my commment for the Q I wasn't the only one...:)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:efortier
ID: 1357337
Under Winnt, if you don;t check the returned length of GetSystemDirectory or Getwindowsdirectory you might get introuble.

inany case, it's a good idea to check the return of ANY such functions!

If you use GetMem, you should always use:

 try
   GetMem(Windir,255);
   ... the code ...
 finally
   Freemem(Windir);
 end
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1357338
Since it seems hellfire has to read that value lots of times in his(her) program, wouldn't it be less time and resource consuming to store that value into a variable once for all?

This requires a few minor changes.
0
 

Author Comment

by:hellfire052497
ID: 1357339
As I stated I need it many times, so if there is even a faster way. please show me.

(response on comment from julio)
0
 
LVL 4

Expert Comment

by:d003303
ID: 1357340
for such purposes I have created a unit with helper procedures that also provide information about the system the program is running in. All information that cannot change while the program is running (Windows version, Windows directory, etc) are initialized in the unit startup code and stored in variables in the interface part.
e.g.

unit Helpers;

interface

var WindowsDirectory,
    WindowsSystemDirectory : string;

implementation

uses Windows;

procedure InitGlobals;
var PStrBuffer : PChar;
begin
  GetMem(PStrBuffer, MAX_PATH);
  try
   GetWindowsDirectory(PStrBuffer, MAX_PATH);
   WindowsDirectory := string(PStrBuffer);
   GetSystemDirectory(PStrBuffer, MAX_PATH);
   WindowsSystemDirectory := string(PStrBuffer);
  finally
    FreeMem(PStrBuffer, MAX_PATH);
  end;
end;

initialization
  InitGlobals;
end.

Take the Helpers unit in your uses clause, and you can use the WindowsDirectory and WindowsSystemDirectory without any further API calls.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1357341
one thing I forgot, GetMem and FreeMem should use MAX_PATH + 1 for the allocation size (to include the terminating zero character).
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month6 days, 16 hours left to enroll

622 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