Solved

DLL access violation

Posted on 1998-10-13
2
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 108
enhance the following code 3 42
scroll down TListBox component in Delphi 1 31
Using MMsystem To change audio input 1 20
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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