0

I have an app that works fine... Porting it to be implemented as a DLL. I have a datamodule that has my Database and TTable components on it... In Design mode, the Database is set to Active. Tables point to the database, they are set to active. I can right click on the tables, go the field editor, and see all the columns, so I know the structure/properties are set up fine....

The problem is at run time... It gives me an AV on this line...

 if MyDataModule.DB1.Connected = True then
  ShowMessage('Active')
   else 
 ShowMessage('Not Active');

I have seen hints on the web that there is something special that needs to be done to use a Datamodule inside a DLL, but I am not getting anything to work.

Specific error message is:

Access Violation at address 06D4E22E in module 'DocAssistCom.dll' Read of address 0000070'

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1009073
  • 3,160
  • 7
  • 40
  • 82
  • 3
    Are you sure which you are creating an instance to the datamodule before to use it? – RRUZ Dec 08 '11 at 18:20
  • 1
    as @RRUZ pointed out, be warned that datamodules(when added to the VCL Forms Application) are created automatically(if you look at project source), whereas when you create a dll, you need to manually create an instance of the datamodule before any calls to components on it can be made, i.e. accessing database –  Dec 08 '11 at 18:28
  • 1
    Please note for future reference: Anytime your fingers type the phrases `AV`, `access violation`, 'error`, or `exception`, the very next thing they should start typing is the **exact** error message you received, including any memory addresses or error codes. Saying "it gives me an AV" is absolutely useless for people trying to help you solve your problem; the error message can speed things up tremendously (even if it's meaningless to you), getting you a solution much faster. Thanks. :) – Ken White Dec 08 '11 at 18:33
  • Possibility... So I added MyDataModule := tdm_Text.Create(nil); to the initialization section. To test this, I also added a ShowMessage to the DataModule.OnCreate event. I do see this before business code ever gets called. – user1009073 Dec 08 '11 at 18:37

1 Answers1

4

You should verify that MyDaModule is indeed created, then that MyDataModule.DB1 is created as well before even trying to use MyDataModule.DB1.Connected.

if Assigned(MyDataModule) then
  if Assigned(MyDataModule.DB1) then
    if MyDataModule.DB1.Connected = True then
      ShowMessage('Active')
    else 
      ShowMessage('Not Active')
  else
    ShowMessage('MyDataModule.DB1 not assigned')
else
  ShowMessage('MyDataModule not assigned');

But they might still not be fully created and ready when you try to use them.

So, instead of spreading ShowMessage calls around, I would rather use OutputDebugstring and debug the DLL in the IDE to see the code path...

Note: I don't know which versions of Delphi and Windows you are working with, but be aware that TTable requires the BDE (which is quite deprecated nowadays)...

Francesca
  • 21,452
  • 4
  • 49
  • 90
  • DataModule is not being assigned.... yet I am getting a message from DataModule.OnCreate event. I have set the DM variable as a global variable, yet this .pas file cannot "see" it, yet it compiles fine. As to OutputDebugString, never heard of it... Good to know... – user1009073 Dec 08 '11 at 19:01
  • Solved: I had to manually create the Data Module, and then I also had to REMOVE the MyDataModule.pas file from the USES clause; some type of a scoping issue was creeping in as well.... – user1009073 Dec 08 '11 at 20:40
  • Were you reusing the same global MyDataModule variable name that was in the unit? – Marcus Adams Dec 08 '11 at 20:52