Solved

how to make function to work?

Posted on 1998-01-23
8
176 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

806 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