Solved

DLL access violation

Posted on 1998-10-13
2
397 Views
Last Modified: 2010-04-06
I am trying to put my routines into a dll.
If I do a simple thing in the dll (e.g. addition, etc)
then it works fine.But if I do a bit complex thing (e.g. passing a tquery object and making a query and returning the resultset) then it gives me the access violation error
at the third or fourth call.
Can that be a memory allocation problem.(a growing DLL cause access violation ) or any oyher thing.
any suggestion will be appreciated!
thanks in advance
0
Comment
Question by:guvencum
2 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1342735
Hi

Certain things cannot be done in a DLL.  E.g. you can't make a form, query, etc. in a DLL with the owner being your main form.

Perhaps you could give some code samples.

In general, passing VCL objects (forms, queries, etc.) from your main app to a DLL is OK, but they should be created and destroyed in your main app.

Cheers,
JB
0
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 100 total points
ID: 1342736
Hello,

I had the same problem. It's true that you cannot pass a query or database object from your main application to your DLL.

My solution was to have my own TDatabase object in my DLL as well as the TQuery. You don't need a form for this - just create the objects dynamically. The results of the query are passed by a TList to the main application.

This also solved another problem: I wanted to have BDE access from within a thread and I could not get this working (you need a second TSession...). The DLL routine can be called within a thread - it has it's own TSession it will work.

Hope you like this answere.

Beep, beep... Roadrunner
--------------------------------------
library CompanyData;

uses
  SysUtils,
  Classes,
  Windows,
  Dialogs,
  dbtables;

var
  DB     : TDatabase;

procedure GetData(ResultList:TList); stdcall; far;
var
  qry       : TQuery;
  NewItem   : TMyListItem;
begin
  if assigned(ResultList) then begin
    qry:=TQuery.Create(NIL);
    qry.DatabaseName:=DB.DatabaseName;
    qry.SQL.Text:=....
    qry.Open;

. move data into ResultList...

    qry.Free;
  end;
end;


procedure DLLHandler(Reason:integer);
begin
  case Reason of
    DLL_PROCESS_ATTACH : begin
                           DB:=TDatabase.Create(NIL);
                           DB.Name        :='dbCOMPANYS';
                           DB.LoginPrompt :=false;
                           DB.DatabaseName:='dbCOMPANYS';
                           DB.Params.Add('DATABASE NAME=dbCOMPANYS');
                           DB.AliasName   :='COMPANYS';
                           DB.Connected:=true;
                         end;
    DLL_PROCESS_DETACH : begin
                           DB.Connected:=false;
                           DB.Free;
                         end;
  end;
end;


exports
  GetData;

begin
  DLLProc:=@DLLHandler;
  DLLHandler(DLL_PROCESS_ATTACH); { Otherwise no initialization would be done }
end.

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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