Solved

how to make function to work?

Posted on 1998-01-23
8
173 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

12 Experts available now in Live!

Get 1:1 Help Now