Results 1 to 9 of 9

Thread: General activex dll multithreading clarification

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2021
    Posts
    144

    General activex dll multithreading clarification

    Hello Trick,

    I wrote a simple multithreaded activex dll which can create its private objects in the new thread.
    I can understand that it works when set to apartment threaded.
    But I don't understand how it is able to create dll private objects in the new thread even when set to single threaded model without crashing.
    Project attached. actx dll(myactsingle1) and std exe(myactsingle1use)

    Thanks
    Attached Files Attached Files

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Dec 2021
    Posts
    144

    Re: General activex dll multithreading clarification

    Hello Trick,

    What is the mistake I have to correct in actxdllwithapt_threaded1 https://www.vbforums.com/attachment....5&d=1696428326
    so that it can work similar to myactsingle1 of https://www.vbforums.com/attachment....7&d=1696260349 project in General-activex-dll-multithreading-clarification above.


    Thanks
    Last edited by smkperu; Oct 5th, 2023 at 10:27 AM.

  3. #3

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2021
    Posts
    144

    Re: General activex dll multithreading clarification

    Quote Originally Posted by The trick View Post
    You shouldn't create objects from single-threaded dll in different threads.
    Hello Trick,
    Ok.
    Still what I did'nt understand is that myactsingle1 of https://www.vbforums.com/attachment....7&d=1696260349 project in General-activex-dll-multithreading-clarification runs without crash even in single threaded mode.How it is possible.

    When compared both actxdllwithapt_threaded1 https://www.vbforums.com/attachment....5&d=1696428326 and myactsingle1 of https://www.vbforums.com/attachment....7&d=1696260349 are similar activex projects using same logic
    and single threaded.I compared both project files (.vbp) and could not notice any difference.

    Thanks
    Last edited by smkperu; Oct 6th, 2023 at 12:48 AM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2021
    Posts
    144

    Re: General activex dll multithreading clarification

    Quote Originally Posted by smkperu View Post
    Hello Trick,
    Ok.
    Still what I did'nt understand is that myactsingle1 of https://www.vbforums.com/attachment....7&d=1696260349 project in General-activex-dll-multithreading-clarification runs without crash even in single threaded mode.How it is possible.

    When compared both actxdllwithapt_threaded1 https://www.vbforums.com/attachment....5&d=1696428326 and myactsingle1 of https://www.vbforums.com/attachment....7&d=1696260349 are similar activex projects using same logic
    and single threaded.I compared both project files (.vbp) and could not notice any difference.

    Thanks
    Hello Trick,

    After deep debugging I identified the problem is

    using App object in Form1.frm of actxdllwithapt_threaded project at https://www.vbforums.com/attachment....5&d=1696428326.

    Private Sub Form_Load()
    Me.Caption = App.ThreadID
    End Sub


    I corrected it by using GetCurrentThreadId API instead of App.ThreadID ie., by not using App object.

    Is there any way to use the App object directly in new thread without crash in activex dll(single threaded).


    Thanks

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2021
    Posts
    144

    Re: General activex dll multithreading clarification

    Quote Originally Posted by smkperu View Post
    Hello Trick,

    After deep debugging I identified the problem is

    using App object in Form1.frm of actxdllwithapt_threaded project at https://www.vbforums.com/attachment....5&d=1696428326.

    Private Sub Form_Load()
    Me.Caption = App.ThreadID
    End Sub


    I corrected it by using GetCurrentThreadId API instead of App.ThreadID ie., by not using App object.

    Is there any way to use the App object directly in new thread without crash in activex dll(single threaded).


    Thanks
    Hello Trick,

    1. it is possible to access App object in new thread

    in standard exe in any new thread when runtime is initialized in ThreadProc as follows from modMultithreading2.bas using vbCreateThread in https://www.vbforums.com/showthread....n-Standart-EXE

    Code:
    ' // Initialize runtime for new thread and run procedure
    Private Function ThreadProc( _
                     ByVal pParameter As Long) As Long
        Dim cExpSrv     As IUnknown
        Dim bIsInIDE    As Boolean
        Dim tClsId      As tCurGUID
        Dim tIID        As tCurGUID
        Dim tThreadData As tThreadData
        Dim hHeap       As Long
        Dim pNewHeader  As Long
        
        Debug.Assert MakeTrue(bIsInIDE)
        
        If Not bIsInIDE Then
            Set cExpSrv = CreateIExprSrvObj(0, 4, 0)
        End If
        
        CoInitialize ByVal 0&
        
        hHeap = GetProcessHeap()
        
        TlsSetValue lTlsSlot, ByVal pParameter
        
        GetMem8 ByVal pParameter, tThreadData
    
        If bIsInIDE Then
            FakeMain
        Else
            
            pNewHeader = CreateVBHeaderCopy()
            
            If pNewHeader Then
            
                tIID.c2 = 504403158265495.5712@
    
                VBDllGetClassObject hModule, 0, pNewHeader, tClsId, tIID, 0
    
                ' // Becasue of a header will be used by MSVBVM60 in DllMain (with DLL_THREAD_DETACH)
                ' // we can't free it now. To avoid memory leak we will free it later
                
            End If
            
        End If
        
        GetMem8 ByVal pParameter, tThreadData
        
        ThreadProc = tThreadData.lpParameter
        
        HeapFree hHeap, 0, pParameter
    
        CoUninitialize
        
    End Function

    2. it is possible to access App object in new thread as follows

    Code:
     // Initialize App object
            lUnused = App.ThreadID
    in standard dll in any new thread when we use as follows from modDllInitialize.bas in https://www.vbforums.com/attachment....7&d=1578834223

    Code:
    ' // Initilaize the runtime
    Public Function InitializeRuntimeForProject( _
                    ByVal hInstance As Long, _
                    ByVal bIsFirst As Boolean) As Boolean
        Dim pNewHeader  As Long
        Dim tClsId      As tCurGUID
        Dim tIID        As tCurGUID
        Dim lUnused     As Long
        
        pNewHeader = VBDll.TlsGetValue(mlTlsSlot)
        
        ' // Check if the module already initialized
        If pNewHeader Then
    
            InitializeRuntimeForProject = True
            Exit Function
            
        End If
        
        VBDll.CoInitialize ByVal 0&
        
        If mpVbHeader = 0 Then
            
            ' // Search for VBHeader (EXEPROJECTINFO)
            mpVbHeader = SearchForVbHeader(hInstance)
            If mpVbHeader = 0 Then Exit Function
            
            ' // Modify header
            ModifyVBHeader
            
        End If
        
        ' // Create the new copy of headers for new instance
        pNewHeader = CreateVBHeaderCopy()
        
        ' // Save it
        VBDll.TlsSetValue mlTlsSlot, ByVal pNewHeader
    
        If pNewHeader = 0 Then
            Exit Function
        End If
        
        ' // IID_IUnknown
        tIID.c2 = 504403158265495.5712@
        
        ' // Call CThreadPool::InitDllAccess
        VBDll.VBDllGetClassObject hInstance, 0, pNewHeader, tClsId, tIID, 0
        
        If bIsFirst Then
            ' // Initialize App object
            lUnused = App.ThreadID
        End If
        
        InitializeRuntimeForProject = True
        
    End Function
    When it is possible to use App object in new thread in 1. standard exe and 2. standard dll is there any similar method to make App object work in new thread in activex dll(single threaded).


    Thanks
    Last edited by smkperu; Oct 12th, 2023 at 06:37 AM.

  7. #7
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,538

    Re: General activex dll multithreading clarification

    where is modMultiThreading.Initialize?
    can't find this code

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2021
    Posts
    144

    Re: General activex dll multithreading clarification

    Quote Originally Posted by smkperu View Post
    Hello Trick,

    1. it is possible to access App object in new thread

    in standard exe in any new thread when runtime is initialized in ThreadProc as follows from modMultithreading2.bas using vbCreateThread in https://www.vbforums.com/showthread....n-Standart-EXE

    Code:
    ' // Initialize runtime for new thread and run procedure
    Private Function ThreadProc( _
                     ByVal pParameter As Long) As Long
        Dim cExpSrv     As IUnknown
        Dim bIsInIDE    As Boolean
        Dim tClsId      As tCurGUID
        Dim tIID        As tCurGUID
        Dim tThreadData As tThreadData
        Dim hHeap       As Long
        Dim pNewHeader  As Long
        
        Debug.Assert MakeTrue(bIsInIDE)
        
        If Not bIsInIDE Then
            Set cExpSrv = CreateIExprSrvObj(0, 4, 0)
        End If
        
        CoInitialize ByVal 0&
        
        hHeap = GetProcessHeap()
        
        TlsSetValue lTlsSlot, ByVal pParameter
        
        GetMem8 ByVal pParameter, tThreadData
    
        If bIsInIDE Then
            FakeMain
        Else
            
            pNewHeader = CreateVBHeaderCopy()
            
            If pNewHeader Then
            
                tIID.c2 = 504403158265495.5712@
    
                VBDllGetClassObject hModule, 0, pNewHeader, tClsId, tIID, 0
    
                ' // Becasue of a header will be used by MSVBVM60 in DllMain (with DLL_THREAD_DETACH)
                ' // we can't free it now. To avoid memory leak we will free it later
                
            End If
            
        End If
        
        GetMem8 ByVal pParameter, tThreadData
        
        ThreadProc = tThreadData.lpParameter
        
        HeapFree hHeap, 0, pParameter
    
        CoUninitialize
        
    End Function

    2. it is possible to access App object in new thread as follows

    Code:
     // Initialize App object
            lUnused = App.ThreadID
    in standard dll in any new thread when we use as follows from modDllInitialize.bas in https://www.vbforums.com/attachment....7&d=1578834223

    Code:
    ' // Initilaize the runtime
    Public Function InitializeRuntimeForProject( _
                    ByVal hInstance As Long, _
                    ByVal bIsFirst As Boolean) As Boolean
        Dim pNewHeader  As Long
        Dim tClsId      As tCurGUID
        Dim tIID        As tCurGUID
        Dim lUnused     As Long
        
        pNewHeader = VBDll.TlsGetValue(mlTlsSlot)
        
        ' // Check if the module already initialized
        If pNewHeader Then
    
            InitializeRuntimeForProject = True
            Exit Function
            
        End If
        
        VBDll.CoInitialize ByVal 0&
        
        If mpVbHeader = 0 Then
            
            ' // Search for VBHeader (EXEPROJECTINFO)
            mpVbHeader = SearchForVbHeader(hInstance)
            If mpVbHeader = 0 Then Exit Function
            
            ' // Modify header
            ModifyVBHeader
            
        End If
        
        ' // Create the new copy of headers for new instance
        pNewHeader = CreateVBHeaderCopy()
        
        ' // Save it
        VBDll.TlsSetValue mlTlsSlot, ByVal pNewHeader
    
        If pNewHeader = 0 Then
            Exit Function
        End If
        
        ' // IID_IUnknown
        tIID.c2 = 504403158265495.5712@
        
        ' // Call CThreadPool::InitDllAccess
        VBDll.VBDllGetClassObject hInstance, 0, pNewHeader, tClsId, tIID, 0
        
        If bIsFirst Then
            ' // Initialize App object
            lUnused = App.ThreadID
        End If
        
        InitializeRuntimeForProject = True
        
    End Function
    When it is possible to use App object in new thread in 1. standard exe and 2. standard dll is there any similar method to make App object work in new thread in activex dll(single threaded).


    Thanks
    Hello Trick,

    I got access to the App object in new thread in activex dll(single threaded) by just accessing it once in main thread in the same activex dll just as you showed in 2.standard dll.Thank you.

    Having solved my App object problem can you tell me what are the
    situations where activex dll(single threaded) crashes if we don't use
    activex dll(apartment threaded) in new thread thru code.

    Thanks

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2021
    Posts
    144

    Re: General activex dll multithreading clarification

    Hello Trick,

    In a activex dll in new thread we can create private objects of std exe (in which the activex dll is loaded) by using callbacks thru InitCurrentThreadAndCallFunction of modMultithreading2.bas in std exe.

    How can we create the private objects of std exe in the loaded activex dll in its new thread without using callbacks of standard exe.

    Thanks

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