|
-
Oct 12th, 2023, 06:20 AM
#1
Thread Starter
Addicted Member
Re: General activex dll multithreading clarification
 Originally Posted by smkperu
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.
-
Oct 13th, 2023, 07:37 AM
#2
Thread Starter
Addicted Member
Re: General activex dll multithreading clarification
 Originally Posted by smkperu
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|