Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DLL access violation

Posted on 1998-10-13
2
Medium Priority
?
401 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Screencast - Getting to Know the Pipeline
Suggested Courses

610 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