Thread View: borland.public.cppbuilder.database.ado
5 messages
5 total messages
Started by s.kenwrick
Tue, 17 Oct 2006 15:31
Getting ADO events in a DLL???
Author: s.kenwrick
Date: Tue, 17 Oct 2006 15:31
Date: Tue, 17 Oct 2006 15:31
24 lines
1664 bytes
1664 bytes
I am writing a DLL that dynamically creates an ADO connection something like this: TADOConnection * ADOp; CoInitialize(NULL); TADOConnection * ADOp = new TADOConnection(NULL); I then go on to populated the properties of the ADOp and call the open method, and this all works well. But how do I recieve and process the ADO events like OnConnectionComplete, OnWillConnect etc.? Somehow I need to associate an event handler with the dynamically created Connection object and I don't know how to do this.. any help would be appreciated... Regards Sean Kenwrick
Re: Getting ADO events in a DLL???
Author: Clayton Arends
Date: Tue, 17 Oct 2006 16:16
Date: Tue, 17 Oct 2006 16:16
52 lines
2409 bytes
2409 bytes
Since all event handlers are "closures" (a pointer to a method in a class)
you will need an object that handles your ADOp connection and houses the
event handlers. Something like this should work for you:
class TMyDllClass
{
private:
TADOConnection* ADOp;
void __fastcall ADOpConnectComplete(TADOConnection* Connection,
const _di_Error Error, TEventStatus &EventStatus);
void __fastcall ADOpWillConnect(TADOConnection* Connection,
WideString &ConnectionString, WideString &UserID,
WideString &Password, TConnectOption &ConnectOptions,
TEventStatus &EventStatus);
public:
TMyDllClass();
~TMyDllClass();
};
TMyDllClass::TMyDllClass()
{
OleInitialize(NULL);
ADOp = new TADOConnection(NULL);
ADOp->OnConnectComplete = &ADOpConnectComplete;
ADOp->OnWillConnect = &ADOpWillConnect;
}
TMyDllClass::~TMyDllClass()
{
OleUninitialize();
}
void __fastcall TMyDllClass::ADOpConnectComplete(TADOConnection*
Connection,
const _di_Error Error, TEventStatus &EventStatus)
{
// do whatever
}
void __fastcall TMyDllClass::ADOpWillConnect(TADOConnection* Connection,
WideString &ConnectionString, WideString &UserID,
WideString &Password, TConnectOption &ConnectOptions,
TEventStatus &EventStatus)
{
// do whatever
}
HTH,
- Clayton
Re: Getting ADO events in a DLL???
Author: s.kenwrick
Date: Wed, 18 Oct 2006 08:44
Date: Wed, 18 Oct 2006 08:44
23 lines
2240 bytes
2240 bytes
Thanks for the response it was very helpful, however it was only a partial success. The OnWillConnect event handler was triggered, but not the OnConnectComplete event handler. In fact I found that all event handlers that are triggered by actual windows messages are not being called. It turns out that the reason for this is that I am calling the DLL functions from a background thread. I have a CBUILDER GUI application that loads a set of DLLs then spawns a second thread, and it is this second thread that is calling the DLL functions that attempts to create the ADOConnection etc.. If I run your code from the main thread it all works fine, if I run your code from the other thread it does not trigger the event handlers (I guess that the windows messages are being directed to the main thread and not the secondary thread). Is there a way I can tell VCL to direct messages to the secondary thread and thus trigger these event handlers? Thanks Sean
Re: Getting ADO events in a DLL???
Author: Clayton Arends
Date: Wed, 18 Oct 2006 15:29
Date: Wed, 18 Oct 2006 15:29
11 lines
1476 bytes
1476 bytes
I'm not sure of the solution. I have read up a bit on this problem via some Google Groups searches and did not find anything conclusive. However, first thing you can try is to put the following in your DLL's initialization code. Application->Handle = GetActiveWindow(); If that doesn't work you may have to start a new thread in borland.public.cppbuilder.nativeapi so that someone else more knowledgable on this particular subject can help. - Clayton
Re: Getting ADO events in a DLL???
Author: s.kenwrick
Date: Wed, 18 Oct 2006 20:34
Date: Wed, 18 Oct 2006 20:34
42 lines
2645 bytes
2645 bytes
Solved it!
I fixed the problem by creating an invisible FORM from the
initialization code when the DLL is loaded from the main VCL thread.
In this form I added a message handler by adding code in the public
section like this:
#define TM_ADOCONN (WM_APP + 401)
class TForm1 : public TForm
{
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall F_ADOConn(TMessage & WMsg);
void __fastcall ADOpConnectComplete(TADOConnection* Connection,
const _di_Error Error, TEventStatus &EventStatus);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(TM_ADOCONN, TMessage, F_ADOConn);
END_MESSAGE_MAP(TForm)
};
Then from my background thread I set up a structure with all the
appropriate information and send a message to the F_ADOConn() function
with the LParam as a pointer to this structure using the PostMessage()
function. The F_ADOConn() message handler then gets called from the
main VCL thread and I can create the ADOConnection object, Override the
OnConnectComplete event handler, setup the parameters and open the
connections.
The ADOConnection Event handlers are then triggered as expected from
the main VCL thread.
Its simply a matter now of ensuring that everything is thread safe....
Thanks
Sean
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads