Solved

how to make function to work?

Posted on 1998-01-23
8
175 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

17 Experts available now in Live!

Get 1:1 Help Now