0

I'm trying to create a dll that contains a VCL data module - the idea being that various applications can all load the same dll and use the same database code.

The data module itself is tested ok as part of an application - I've copied the form over to my dll project.

So in the dll entry point method, I need to initialize the data module:

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
    //if I don't call this, I get an exception on initializing the data module
    CoInitialize(NULL);

    //initialize a standard VCL form; seems to works fine
    //I'm not using Application->CreateForm as I don't want the form to appear straight away
    if(!MyForm) MyForm = new TMyForm(Application);

    //this doesn't work - the thread seems to hang in the TDataModule base constructor?
    //I've also tried Application->CreateForm; same result
    if(!MyDataModule) MyDataModule = new TMyDataModule(Application);

}

I've also seen something about how I need to call Application->Initialize before creating the form but this doesn't seem to make any difference.

Any ideas?

Thanks

manlio
  • 18,345
  • 14
  • 76
  • 126
user1898153
  • 463
  • 6
  • 12
  • This may or may not be relevant: I can initialize an empty data module no problem. My actual data module contains an ADOConnection and an ADOQuery - as soon as I add these the problem starts – user1898153 Feb 27 '13 at 11:27
  • I had similar problem when using ADO components in threads. It required CoInitialize to be called. Don't know if it would help in your case but try.. – Tracer Feb 27 '13 at 19:49

1 Answers1

0

You really should not be doing very much work in your DllEntryPoint() at all. Certainly not calling CoInitialize(), anyway. It is not the DLL's responsibility to call that when loaded. It is the calling app's responsibility before loading the DLL.

You should either:

  1. export an additional function to initialize your DLL and then have the app it after loading the DLL (same for uninitialing the DLL before unloading it)

  2. don't create your TForm/TDataModule until the first time the DLL actually needs them.

  3. move your TForm/TDataModule into their own worker thread inside the DLL. In this case, you would then call CoIniitalize().

And in all cases, don't relay on the DLL's Application object to manage the lifetime of your TForm/TDataModule. Free them yourself instead before the DLL is unloaded.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770