Conditional use


I have a flag in the registry called "ShowOptions" that can be "YES" or "NO". In the Main.pas unit of my project, I want to load (or use) another unit called "UOptions.pas" depending of the value in the the registry. I want that because the unit "UOptions.pas" refers to some DLL and these files are not necessarly on the client machine.

How can I do that ????
Who is Participating?

Improve company productivity with a Business Account.Sign Up

YensidModConnect With a Mentor Commented:
This question is PAQed and no points refunded (of 50)

Community Support Moderator
Instead of linking to the DLLs statically (function ... external ...), you should load the DLL dynamically at runtime, using LoadLibrary, GetProcAddress, FreeLibrary.
roioepAuthor Commented:
"UOptions" is a frame with controls. The form "UMain.pas" contains the UOptions frame. This frame use a lot of dll. I don't want to "use" the frame if the registry key says "NO".
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

roioepAuthor Commented:
What I want is to be able to use or not a unit and its components at runtime...
Let's clarify one point: You don't want to use the frame because the DLLs used by the frame may not be present on the target system.
So your real problem is the DLLs, not the frame.

The only way to conditionaly use a unit is to compile it into a runtime package which would be loaded or not dynamically at runtime. This may introduce you to a whole new set of problems, especially if you haven't done it before.

Why not attack the root of the problem? Redesign the frame unit so that it does not link the DLLs statically but loads them dynamically at runtime...
Just my 2c, of course.
TOndrej, you always say it's only your 2c, but you always give very useful 2c... :)
hello roloep, I may not compleatly understand your problem, but it seems like you do not want the Frame to be present if the boolean is False in the registry. Since a Frame is like a Form you might try this, click on the frame and then click "Delete" in the Edit menu, to remove it from your main form. You can look in the Project Menu "Options" an see on the 'Forms" Tab that the Main Form "Form1" is in "Auto-Create" side and the "Frame1" is on the "Availible" forms side. Now in your Main Forms OnCreate Event add this code

procedure TForm1.FormCreate(Sender: TObject);
{get the ShowOptions value}
if ShowOptions then
  Application.CreateForm(TFrame1, UOptions);
  UOptions.Parent := Self;
  UOptions.Visible := True;
  UOptions.Top := 88;
  UOptions.Left := 488;
  //UOptions.Color := clRed;
  {add any start up code here}

this will load your Form without the Frame if ShowOptions is False and the UOptions.pas won't be executed
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
Lukasz LachCommented:
No comment has been added lately (18 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:


Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --


EE Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.