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)
Re: General activex dll multithreading clarification
Originally Posted by The trick
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.
Re: General activex dll multithreading clarification
Originally Posted by smkperu
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.
' // 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
' // 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.
' // 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
' // 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.
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 threadwithout using callbacks of standard exe.