Solved

how to make function to work?

Posted on 1998-01-23
8
183 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
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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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