Click to See Complete Forum and Search --> : Syncronization between two DLL's
mwatkins
May 3rd, 2001, 05:13 PM
Hi,
I have recently developed an ActiveX DLL that is Apartment Threaded. In this COM component I make serveral API calls to a single threaded C++ DLL ( NOT A COM DLL ). My problem is this, whenever two functions of the single threaded DLL get called at the same time, the single threaded DLL hangs. Is there any availible function in VB that would allow me to syncronize the call to the single threaded DLL?
cksiow
May 3rd, 2001, 09:21 PM
one way I can think of is using a global variable in the module (might not work for Apartment Threaded ActiveX DLL, check it out).
say, u declare a variable called
Dim isIn as Boolean.
then u can set it to true when u make a call to the DLL. of course, u need to check the status of that variable before doing anything. if it's True, then u know that some other code is making a called to the DLL and you can just wait using Sleep API (timeout would be nice here).
HTH
cksiow
http://vblib.virtualave.net - share our codes
Cimperiali
May 4th, 2001, 04:55 AM
...Or you can use the "old" solution: a semaphore on file
ie:
have a field in a table in database
or a value in a ini file
or even a line in a sequential file
where you put "1" when starting a call and a "0" when ending
Before making a call, read the vale, and wait 'till it is 0 before executing the call...
If you have remote components, you may use a dll (vb, of course) to read/write the value on the machine where the component with Api is.
Special thanks to Lothar "the Great" Haensler, Tom Archer, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
mwatkins
May 4th, 2001, 09:17 AM
How about instead of a file, I use a registry entry? Do you think that this would be better or worse than the writing to a file?
P.S. Thanks for the replies - I thought of using a Global but I don't think that will work in an apartment threaded DLL. If I am not mistaken, there is no way to comunicate information between threads right?
Mike Watkins
CodeSprings, inc.
Quality Assurance
Cimperiali
May 7th, 2001, 02:25 AM
I am not used to write to registry: I do prefer to let it as it has as much as I can. May be in this case it could be a good solution, but you will modify often value in it... I think an external file should be fine, if you like the solution. However, let's read other replies, too...
Best regards,
Cesare Imperiali
Special thanks to Lothar "the Great" Haensler, Tom Archer, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
mwatkins
May 21st, 2001, 09:53 AM
I figured out how to do this effectively and for an ActiveX DLL and I thought I would share. I used the API calls to CreateMutex(), WaitForSingleObject() and RemoveMutex(). These functions are used for synchronizing the whole class. Ex:
'// Win32 API Functions for controlling thread flow
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
'////////////////////////////////////////////////////////////////////
'// Function: LockProc
'//
'// Purpose: Used to lock a procedure so as to not allow any
'// other external process access it while it is in
'// use.
'//
'// Paramaters: ProcName - String var to hold the name of the
'// calling procedure.
'//
'// Return: Long - Created Mutex Handle
'////////////////////////////////////////////////////////////////////
Private Function LockProc(ProcName As String) As Long
Dim MutexName As String
Dim Handle As Long
Dim sa As SECURITY_ATTRIBUTES
sa.nLength = 0
sa.bInheritHandle = 0
sa.lpSecurityDescriptor = 0
MutexName = "AMGold" & ProcName
Handle = CreateMutex(sa, False, MutexName)
WaitForSingleObject Handle, 50000
'// Now that the atom does not exist, create it to sync the init call
LockProc = Handle
End Function
'////////////////////////////////////////////////////////////////////
'// Sub: ReleaseProc
'//
'// Purpose: Releases the mutex that is specified by the passed
'// paramater
'//
'// Paramaters: hMutex - Handle to the mutex that was created
'// by the LockProc method. This call must
'// be made if the LockProc method was
'// called.
'////////////////////////////////////////////////////////////////////
Private Sub ReleaseProc(hMutex As Long)
ReleaseMutex hMutex
End Sub
I used the LockProc function to lock a procedure and then use the ReleaseProc function to release it. The DLL is then really single threaded but behaves as if it were multithreaded.
Thanks for all the replies,
Mike Watkins
CodeSprings, inc.
Quality Assurance
Cimperiali
May 22nd, 2001, 05:28 AM
Indeed much better than external slow access disk file! Thanks for sharing (only a pity I am out of votes)
Best regards,
Cesare Imperiali
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.