Solved

DLL access violation

Posted on 1998-10-13
2
393 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

11 Experts available now in Live!

Get 1:1 Help Now