Multithreading issues related to an ActiveX control
Hi All,
I use an activex control in my dll code. The control has 3 functions:
Initialise
Uninitialise
long Verify(LPCTSTR id, LPCTSTR password)
I have a wrapper class in my dll project to call the verify function of
activex control.
=====================================
#import "c:/applicationpath/interface.dll"
using namespace XXX;
class CVerifyUser
{
public:
long VerifyUser(CString csUserID, CString csPassword)
{
BSTR bstrUserID = csUserID.AllocSysString();
BSTR bstrPassword = csPassword.AllocSysString();
try
{
HRESULT hr;
hr = m_pVerifyUser->raw_VerifyUser( &bstrUserID, &bstrPassword, &lRet);
}
//...
}
long Uninitialize()
{
if (m_pVerifyUser != NULL) {
m_pVerifyUser.Release();
}
CoUninitialize();
}
long Initialize()
{
CoInitialize(NULL);
//...
hr = m_pVerifyUser.CreateInstance("XXX.UserVerification");
}
CVerifyUser();
virtual ~CVerifyUser();
private:
_UserVerificationPtr m_pVerifyUser;
};
I have a global instance of this wrapper class:
CVerfyUser g_objVerifyUser;
VOID Initialize()
{
g_objVerifyUser.Initialize();
}
VOID Uninitialize()
{
g_objVerifyUser.Uninitialize();
}
=============================
My DLL exports a function for the Verify function.
BOOL Validate(LPCTSTR UserID, LPCSTR Password)
{
long ret = g_objVerifyUser.VerifyUser(UserID, Password);
return ret == 1L; // 1 is success, else failure
}
I have a wrapper class in my dll project to implement the verify function
for the main application which calls the exported Verify function of my dll
which in turn calls the verify function of the activex control.
My dll code runs in the gui thread of the main application. I have no chance
to change main application code. So far this code was working fine but
recently it started failed in some cases. We have found out that the main
application sometimes calls the exported function of my DLL from a thread
different from the main thread. In that case activex control returns a
failure code for the correct "UserID" and "Pasword" parameters.
They expect me to fix this issue in my DLL code rather than making sure tha
main app calls it in GUI thread.
What is the best way to do it for me?
I tried several ways and the best I have accomplished so far is to modify my
exported function like this:
BOOL Validate(LPCTSTR UserID, LPCSTR Password)
{
// old code - start
//long ret = g_objVerifyUser.VerifyUser(UserID, Password);
// old code - end
DWORD thisThdId = ::GetCurrentThreadId();
long ret = 0;
if (thisThdId == g_dwMainThdId)
{
// this takes miliseconds to return
ret = g_objVerifyUser.VerifyUser(UserID, Password); // as usual
}
else
{
// this thread is different from the main thread
// this part takes 5 seconds exactly
CVerifyUser objVerifyUser;
objVerifyUser.Initialize();
ret = objVerifyUser.VerifyUser(UserID, Password);
objVerifyUser.Uninitialize();
}
return ret == 1L; // 1 is success, else failure
}
This modified function works OK by returning corect value for the correct
parameters though the else part takes a long time. But the main app still
doesn't like it. The longish part takes exactly 5 seconds which I suspect is
somehow related to a setting value in the main app. They told me that there
is a setting for this case to wait for the return value and it is exactly 5
seconds.
Any suggestions for more effective ways? Thank you for any advise.
(I usually read this news group and there are a lot of experts here but if
there is any other (better) group to ask this question let me know.)
--
Aslan