Link to home
Start Free TrialLog in
Avatar of paraskafley
paraskafley

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of simonet
simonet
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paraskafley
paraskafley

ASKER

I am exporting the function. Wii try as you say.
Paras
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
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

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


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
No I am not using the function after calling freelibrary.
Will try to locate exactly where the exception occurs.

Regards
Paras
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>
thanks a lot. Alex