BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Getting ADO events in a DLL???

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder Databases (ADO)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Tue Oct 17, 2006 8:31 pm    Post subject: Getting ADO events in a DLL??? Reply with quote



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
Back to top
Clayton Arends
Guest





PostPosted: Tue Oct 17, 2006 9:16 pm    Post subject: Re: Getting ADO events in a DLL??? Reply with quote



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
Back to top
Guest






PostPosted: Wed Oct 18, 2006 8:10 am    Post subject: Re: Getting ADO events in a DLL??? Reply with quote



Thanks for the response it was very helpful, however this was only a
partial success. After implementing the code above the
OnWillConnect event handler was excuted, but the OnConnectComplete
eventhandle did not. I then tried adding an Event handler for
OnWillExecute and this did not execute either.

Could it be that EventHandlers that are triggered by Windows messages
are not being executed (whereas perhaps the OnWillConnect is called as
soon as the TADOConnection.Open() method is called).

Is there something I need to do to tell windows to send the ADO
messages to this class? Or am I missing something else..


Thanks
Sean
Back to top
Guest






PostPosted: Wed Oct 18, 2006 8:10 am    Post subject: Re: Getting ADO events in a DLL??? Reply with quote

Thanks for the response it was very helpful, however this was only a
partial success. After implementing the code above the
OnWillConnect event handler was excuted, but the OnConnectComplete
eventhandle did not. I then tried adding an Event handler for
OnWillExecute and this did not execute either.

Could it be that EventHandlers that are triggered by Windows messages
are not being executed (whereas perhaps the OnWillConnect is called as
soon as the TADOConnection.Open() method is called).

Is there something I need to do to tell windows to send the ADO
messages to this class? Or am I missing something else..


Thanks
Sean
Back to top
Guest






PostPosted: Wed Oct 18, 2006 1:44 pm    Post subject: Re: Getting ADO events in a DLL??? Reply with quote

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
Back to top
Clayton Arends
Guest





PostPosted: Wed Oct 18, 2006 8:29 pm    Post subject: Re: Getting ADO events in a DLL??? Reply with quote

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
Back to top
Guest






PostPosted: Thu Oct 19, 2006 1:34 am    Post subject: Re: Getting ADO events in a DLL??? Reply with quote

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder Databases (ADO) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.