CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2001
    Posts
    6

    Syncronization between two DLL's

    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?



  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Syncronization between two DLL's

    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


  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Syncronization between two DLL's

    ...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.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    May 2001
    Posts
    6

    Re: Syncronization between two DLL's

    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

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Syncronization between two DLL's

    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.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    May 2001
    Posts
    6

    Re: Syncronization between two DLL's

    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

  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Your is a brillant solution

    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.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured