Using LoadLibrary & GetprocAddress How do I connect a form in a dll with a dataset from the main application itself?

Hi
Last time I received an answer for
"How do I connect a form in a dll with a dataset from the main application itself??"

Now the problem is I need to load the form dynamically at run time.
If I declare the function at design time and then build/run the app everything works perfectly. However if I load the dll at runtime and then call the function I get an Invalid Pointer Exception even though the function in the dll works and the form is connected to the datasource/dataset.
I am using code like this.

In the dll itself

function ShowCustForm(CustomerDataSource:TdataSource):Boolean;
begin
  Result :=false;
  frmCust := TfrmCust.Create(Nil);
  frmCust.DBGrid1.DataSource := CustomerDataSource;
  FrmCust.Show;
  Result := true;
end;

In the main App.
THandle = Integer;
TCustForm = function(CustDataSource:TDataSource) :Boolean;
Var
  myHandle : THandle;
  CustForm : TCustForm;


begin
  CustHandle := LoadLibrary('CustDll.dll');
  if handle <> 0 then
  begin
    @CustForm := GetProcAddress
(Myhandle,'ShowCustForm');
      if @CustForm <> nil then
      begin
        CustForm(DataSource1) ;
      end;
   end;
end;

Datasource1 is valid.(I checked.)

Can anyone please help.
Thanks in advance.

Regards
PAras
paraskafleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simonetCommented:
Are you making sure the function ShowCustForm is being exported by the DLL?

Also change your code in the main app to this:

begin
  myHandle := LoadLibrary('CustDll.dll');
  if myHandle > 32 then
  begin
    @CustForm := GetProcAddress
(myHandle,'ShowCustForm');
      if @CustForm <> nil then
      begin
        CustForm(DataSource1) ;
      end;
   end;
end;

You were using the wrong handle variables there.

Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
paraskafleyAuthor Commented:
I am exporting the function. Wii try as you say.
Paras
0
paraskafleyAuthor Commented:
Can anybody please help ?
I Tried it as Alex Suggested.It does work but another problem has cropped up.
I have been told that if I load a dll explictly, I need to unload it by calling freeLibrary.
Now when I unload the dll by calling freelibrary , I get an accessviolation Error !!! The debugger pops up showing registers, stacks and so on.
I thought that maybe it was due to the fact that the form hasn't been destroyed when the dll unloads. I tried explictly destroying the - form(if it exists) just before the Dll unloads (in the dll exitproc).
I also tried it by setting action = caFree in the form's onClose Event.
I still got the access violation error in the mainApp.
Now when I don't call FreeLibrary everything works.
Now the question is Do I have to Call FreeLibrary Explictly.
Thanks in advance.

Paras
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

simonetCommented:
It's better to unload the in the finalization section of the unit or in the form's OnDestroy event.

My favority way to load/unload libraries are:

Load:

Use the initialization section of the unit that declared the prototypes for its functions. I also keep a boolean var to make control whether the function has been loaded or not.

Unload:

Done in the finalization section of the unit mentioned above. The FreeLibrary call is only called if the DLL has been successfully loaded.


In your case, in order to unload the DLL, just add this line to the main form's OnDestory event:

FreeLibrary(myHandle);

Yours,

Alex

0
paraskafleyAuthor Commented:
I am unloading the DLL in formDestroy event of the unit that loads the dll.
I do use  boolean variables to keep track whether the loading of DLL was successful or not.
I am using FreeLibrary(myHandle);
Even then I get the exception.

Regards
Paras


0
simonetCommented:
Are you trying to use the imported function *after* the call to FreeLibrary?

Try stepping through the program (using the F8 key) to see exactly where the exception occurs. Maybe it's not in the FreeLibrary call.

Alex
0
paraskafleyAuthor Commented:
No I am not using the function after calling freelibrary.
Will try to locate exactly where the exception occurs.

Regards
Paras
0
freterCommented:
did you try compiling both the dll and the application using runtime packages? i had a problem similar to your, and after recompiling with runtime packages, everything was fine.

</freter>
0
paraskafleyAuthor Commented:
thanks a lot. Alex
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.