Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Dear Olaf,
I added a button to the ExternalControl activex control and added button1 control as follows:
Private Sub Command1_Click()
Dim hUser32 As Long
hUser32 = GetModuleHandle(StrPtr("user32"))
MsgBox "in button1"
End Sub
When I call from AxThreading exe and click on button1 of activex control the declared api GetModuleHandle works
like a charm without crash even if we don't use RemoveLastDllError module function.Very nice example.Thank you olaf,
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
If we use any api call with declare in active control project like when we click on a added button in ocx project and open it in exe I think project context for the ocx is also initialized automatically in the new thread and still we have to use your RemoveLastDllError module function to avoid crash due to API usage.Please clarify am I correct.
When you create an object using DllGetClassObject (the standard ActiveX way) it always initializes the context for current thread.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by jsvenu
I added a button to the ExternalControl activex control and added button1 control as follows:
Private Sub Command1_Click()
Dim hUser32 As Long
hUser32 = GetModuleHandle(StrPtr("user32"))
MsgBox "in button1"
End Sub
When I call from AxThreading exe and click on button1 of activex control the declared api GetModuleHandle works
like a charm without crash even if we don't use RemoveLastDllError module function.
Of course, because the example adheres to the "VB6-COM-STA threading-rules"
(implicitely, hidden from the User).
You can write basically the same thing (which adheres to these rules) also with the tricks Helper-module
(using AX-Dll-Classes instead of Ax-Exe-Classes) -
but then you'd have to learn, which of the provided "threading-helper-functionality" you'd have to avoid -
and which parts of it are "safe to use" (to not break those COM-threading-rules).
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
When I call from AxThreading exe and click on button1 of activex control the declared api GetModuleHandle works
like a charm without crash even if we don't use RemoveLastDllError module function.
Code:
Private Sub cmdCreateThread_Click()
CloseHandle vbCreateThread(0, 0, AddressOf ThreadProc, 0, 0, 0)
End Sub
Private Sub Form_Load()
Me.Caption = App.ThreadID
Controls.Add("TestOCX.ctlTest", "Ctl1").Visible = True
End Sub
Code:
Option Explicit
Sub Main()
modMultiThreading.Initialize
frmMain.Show vbModal
modMultiThreading.Uninitialize
End Sub
Public Function ThreadProc( _
ByVal l As Long) As Long
Dim cFrm As frmMain
Set cFrm = New frmMain
cFrm.Show vbModal
End Function
The buttons creates a thread with form with external OCX. It requires no administrative right (for EXE), no install (for EXE), shared global variables, etc.
Last edited by The trick; Jan 24th, 2020 at 11:15 AM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
you do like on thread:Controls.Add("TestOCX.ctlTest", "Ctl1").Visible = True
when i add usercontrol on form by my hand,not use"controls.add"。
createthread for load the form with usercontrol or ocx,it,s error。
my english is not good,i can't understand your mean。maybe i need down your code for test.
,someone tell me,can use method(EbLoadRunTime
EbCreateContext
EbSetContextWorkerThread)
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
// This method is called from main thread using proxy-stub layer
Public Function Initialize( _
ByRef sPath As String, _
ByRef sName As String) As Boolean
Set m_cPlugin = modUnregCOM.CreateObjectEx2(sPath, sPath, sName)
your method need load ocx?by clsid?
I want createthread ( address of test) only in stand exe,no need ocx,no need stand dll,no need com dll,no need activex exe.
my method code lines so much,i want to find a easy way,can you help me?
Code:
sub test()
dim f as form2
set f=new form2 ' a usercontrol in this form.
f.show 1
end sub
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by xiaoyao
I want createthread ( address of test) only in stand exe,
...no need ocx,no need stand dll,no need com dll,no need activex exe.
Why do you want to avoid using other (additional) libraries
(later shipping with your Exe, e.g. in a \Bin\-sub-folder)?
You are using VB6 after all - a high-level-language which has its strengths exactly there
(in glueing selfdescribing COM-Objects and COM-Controls together in a RADish way).
If it is because of "avoiding to make a proper installer" - then you should learn about regfree-COM mechanisms first.
VB6 was not made for "normal, free-threading"...
It supports a different kind of threading instead, which requires you to make use of ActiveX-Dlls
(or ActiveX-Exes), when you want to play things "by the (VB6)book" (to avoid instabilities).
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by xiaoyao
where can i download project
i want test add usercontrol on thread form by toolbar ,not by ocx,how can i do?
Controls.Add("TestOCX.ctlTest",
Dear xiaoyao,
Goto project ->components and browse for the .ocx file which you want to add to the project
and select open and then click ok.Automatically it will appear in tool bar which you can add to the form in the standard exe project.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by xiaoyao
Code:
Private Sub cmdCreateThread_Click()
CloseHandle vbCreateThread(0, 0, AddressOf ThreadProc, 0, 0, 0)
End Sub
Private Sub Form_Load()
Me.Caption = App.ThreadID
Controls.Add("TestOCX.ctlTest", "Ctl1").Visible = True
End Sub
Code:
Option Explicit
Sub Main()
modMultiThreading.Initialize
frmMain.Show vbModal
modMultiThreading.Uninitialize
End Sub
Public Function ThreadProc( _
ByVal l As Long) As Long
Dim cFrm As frmMain
Set cFrm = New frmMain
cFrm.Show vbModal
End Function
where is the project link for download?
Dear xiaoyao,
I am attaching the project.
You first build the ExternalControl.vbp activex project in the attachment folder and generate ExternalControl.ocx.
Then open the standard exe project(AxThreading.vbp) and add the ExternalControl.ocx thru project components to the tool bar and then add it to the form in the exe project and build it and run the compiled exe.Click on the button on the form.
regards,
JSVenu
Last edited by jsvenu; Jan 25th, 2020 at 10:54 AM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
// This method is called from main thread using proxy-stub layer
Public Function Initialize( _
ByRef sPath As String, _
ByRef sName As String) As Boolean
Set m_cPlugin = modUnregCOM.CreateObjectEx2(sPath, sPath, sName)
your method need load ocx?by clsid?
I want createthread ( address of test) only in stand exe,no need ocx,no need stand dll,no need com dll,no need activex exe.
my method code lines so much,i want to find a easy way,can you help me?
This is other project which i did for yokesee which shows the plugin-based concept with multithreading.
To create a thread you need to use this module. There are the examples of usage. You don't need any dependencies or dlls. This example just shows how to use an external OCX.
where can i download project
I've attached the one. The modMultiThreading module you can get if you go to the link i provided above.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by xiaoyao
you do like on thread:Controls.Add("TestOCX.ctlTest", "Ctl1").Visible = True
when i add usercontrol on form by my hand,not use"controls.add"。
createthread for load the form with usercontrol or ocx,it,s error。
my english is not good,i can't understand your mean。maybe i need down your code for test.
,someone tell me,can use method(EbLoadRunTime
EbCreateContext
EbSetContextWorkerThread)
for FreeThreader component for VB6 which uses above bold API in the same order for runtime initialisation in CreateMT and EnableEvents functions in assembly language in the file MULTITHREAD.Asm .
It is described as follows: VB6 does not natively support free threading, but with this ActiveX library you can finally create multiple threads and make them communicate, write delegate functions (similarly to VB.NET) and use Structured Exception Handling. The full source code (in VB6 and Assembler) and some sample projects are provided.
Regarding internal working and stability of this code you can take help from Trick since I have less knowledge in ASM and already he helped me a lot regarding multithreading and he can better understand undocumented API like this.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by The trick
This is other project which i did for yokesee which shows the plugin-based concept with multithreading.
To create a thread you need to use this module. There are the examples of usage. You don't need any dependencies or dlls. This example just shows how to use an external OCX.
I've attached the one. The modMultiThreading module you can get if you go to the link i provided above.
============== how to add usercontrols on forms object in thread?
(your project can add ocx,but can't support vb6 usercontrol)
Code:
Private obj As Object
Private Sub Command1_Click()
On Error GoTo ERR
Set obj = Me.Controls.Add("Project1.UserControl1", "UserControl1B")
obj.Visible = True
MsgBox obj.Width
Exit Sub
ERR:
MsgBox "ERR:" & ERR.Number & "," & ERR.Description
End Sub
sub test2
Dim ctlName As Control
Set ctlName = Form1.Controls.Add("Project1.UserControl1", "Text1", Form1)
ctlName.Visible = True
end sub
Last edited by xiaoyao; Jan 26th, 2020 at 08:48 PM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
how to add usercontrols on forms object in thread?
(your project can add ocx,but can't support vb6 usercontrol)
Seems it's the problem with the runtime. It registers the "Global Factory" thru CoRegisterClassObject from the main STA and when you create an usercontrol it calls CoGetClassObject. CoGetClassObject checks the registered Apartment ID i.e. you can't get this "Global Factory" from the different apartment. The same problem you'll see with ActiveX EXE project type. To avoid this you can for example use Load statement with a control array. Additionally you can set the Threading Model to Apartment (1) in VBHeader.dwThreadFlags when you create a VBHeader. In this case each thread (namely STA) will have its own HXMod internal object like in ActiveX DLL. Only in this case you need to performs additional cleaning like VBCanUnloadNow, etc.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
CoGetClassObject,maybe my chinese friend fix error by hook "CoGetClassObject",use asm set eip,But the code is too long.
the vb project down here,can you chang for sample?
thank you very much。 http://www.vbforums.com/showthread.p...t-UserControl)
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Just change this function as follows:
Code:
' // Create copy of VBHeader and other structures
' // The first four bytes contain thread ID. We use that ID to clean unused headers
Private Function CreateVBHeaderCopy() As Long
. . .
CopyMemory ByVal pHeader, ByVal pVBHeader, &H6A
' // ++++++++++++++++++++++++++++
' // to xiaoyao
' // Make Apartment threading model
' //
GetMem4 1&, ByVal pHeader + &H3C
' //
' // You should ensure extra cleaning like VBDllCanUnloadNow, etc.
' // ++++++++++++++++++++++++++++
' // Update strings offsets
CopyMemory pNames(0), ByVal pVBHeader + &H58, &H10
. . .
End Function
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by The trick
Just change this function as follows:
Code:
' // Create copy of VBHeader and other structures
' // The first four bytes contain thread ID. We use that ID to clean unused headers
Private Function CreateVBHeaderCopy() As Long
. . .
CopyMemory ByVal pHeader, ByVal pVBHeader, &H6A
' // ++++++++++++++++++++++++++++
' // to xiaoyao
' // Make Apartment threading model
' //
GetMem4 1&, ByVal pHeader + &H3C
' //
' // You should ensure extra cleaning like VBDllCanUnloadNow, etc.
' // ++++++++++++++++++++++++++++
' // Update strings offsets
CopyMemory pNames(0), ByVal pVBHeader + &H58, &H10
. . .
End Function
Code:
Public Function GETVBHeaderID() As Long '取VB头,全新的取VB头方法,速度比OPEN文件快得多
Dim lPtr As Long, lRet As Long, isvb As String, ll As Long, mdat(1033) As Byte
lPtr = VBGetModuleHandle(ByVal 0&)
isvb = StrConv("VB5!", vbFromUnicode)
Do
If ReadProcessMemory(-1, ByVal lPtr, mdat(0), 1034, ll) = 0 Then Exit Function
lRet = InStrB(mdat, isvb)
If lRet <> 0 Then Exit Do
lPtr = lPtr + 1024
Loop
GETVBHeaderID = lPtr + lRet - 1
End Function
SUB ThreadTest()
CreateIExprSrvObj 0, 4, 0
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED
Call VBDllGetClassObject(U1, U2, pVBHeader, pDummy, pIID, pDummy2)
msgbox "abc"
dim f as form2 'with usercontrol
set f=new form2
f.show 1
end sub
'in bas
public pVBHeader as long ,U1 as long
'in main form
sub test()
pVBHeader=GETVBHeaderID
U1=app.hInstance
createthread *,addressof ThreadTest,*
end sub
i use simple thread method, don't use "HeapAlloc",how to change my code?
HOW TO ADD YOUR CODE TO My project ( for support usercontrol)?
my project only use three lines code for createthread.
not use HeapAlloc,not use tls,not same like you.
Last edited by xiaoyao; Jan 27th, 2020 at 10:45 AM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by xiaoyao
Code:
Public Function GETVBHeaderID() As Long '取VB头,全新的取VB头方法,速度比OPEN文件快得多
Dim lPtr As Long, lRet As Long, isvb As String, ll As Long, mdat(1033) As Byte
lPtr = VBGetModuleHandle(ByVal 0&)
isvb = StrConv("VB5!", vbFromUnicode)
Do
If ReadProcessMemory(-1, ByVal lPtr, mdat(0), 1034, ll) = 0 Then Exit Function
lRet = InStrB(mdat, isvb)
If lRet <> 0 Then Exit Do
lPtr = lPtr + 1024
Loop
GETVBHeaderID = lPtr + lRet - 1
End Function
SUB ThreadTest()
CreateIExprSrvObj 0, 4, 0
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED
Call VBDllGetClassObject(U1, U2, pVBHeader, pDummy, pIID, pDummy2)
msgbox "abc"
dim f as form2 'with usercontrol
set f=new form2
f.show 1
end sub
'in bas
public pVBHeader as long ,U1 as long
'in main form
sub test()
pVBHeader=GETVBHeaderID
U1=app.hInstance
createthread *,addressof ThreadTest,*
end sub
i use simple thread method, don't use "HeapAlloc",how to change my code?
HOW TO ADD YOUR CODE TO My project ( for support usercontrol)?
my project only use three lines code for createthread.
not use HeapAlloc,not use tls,not same like you.
Dear xiaoyao,
With some guidance already I got from Trick I can tell you one thing that you use startup code(Here sub main) with code for avoiding re-entrancy in new thread since you are using the same vbheader in vbdllgetclassobject api in new thread.To solve this problem Trick modifies the existing vbheader and creates a new copy of this vbheader and in this new copy he is setting dwThreadFlags field to 1 as he explained.
May be as per Trick's quote
Seems it's the problem with the runtime. It registers the "Global Factory" thru CoRegisterClassObject from the main STA and when you create an usercontrol it calls CoGetClassObject. CoGetClassObject checks the registered Apartment ID i.e. you can't get this "Global Factory" from the different apartment. The same problem you'll see with ActiveX EXE project type. To avoid this you can for example use Load statement with a control array. Additionally you can set the Threading Model to Apartment (1) in VBHeader.dwThreadFlags when you create a VBHeader. In this case each thread (namely STA) will have its own HXMod internal object like in ActiveX DLL. Only in this case you need to performs additional cleaning like VBCanUnloadNow, etc.
Trick can tell if there is any other way like using Load statement with a control array without need for modifying VBHeader.dwThreadFlags to 1 so that using the same vbheader you can support usercontrol in form in a simple way by using submain containing code to avoid re-entrancy in new thread.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by jsvenu
Dear xiaoyao,
With some guidance already I got from Trick I can tell you one thing that you use startup code(Here sub main) with code for avoiding re-entrancy in new thread since you are using the same vbheader in vbdllgetclassobject api in new thread.To solve this problem Trick modifies the existing vbheader and creates a new copy of this vbheader and in this new copy he is setting dwThreadFlags field to 1 as he explained.
May be as per Trick's quote
Trick can tell if there is any other way like using Load statement with a control array without need for modifying VBHeader.dwThreadFlags to 1 so that using the same vbheader you can support usercontrol in form in a simple way by using submain containing code to avoid re-entrancy in new thread.
regards,
JSVenu
Code:
hModule = App.hInstance
lpVBHeader = GetVBHeader()
Public U1 As Long, U2 As Long, U3 As Long, G3 As Long, pDummy As Long, pDummy2 As Long, IsVb6 As Boolean
Public Declare Function VBDllGetClassObject2 Lib "msvbvm60.dll" Alias "VBDllGetClassObject" (g1 As Long, g2 As Long, ByVal g3_vbHeader As Long, REFCLSID As Long, REFIID As Any, ppv As Long) As Long
Sub Test2(ByVal lpParameter As Long)
CreateIExprSrvObj 0&, 4&, 0& 'VB6运行库初始化
QueryPerformanceCounter CPUv2
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED
Call VBDllGetClassObject2(hModule, U2, lpVBHeader, pDummy, Iid, pDummy2) 'GOOD
' Call VBDllGetClassObject(VarPtr(hModule), U2, lpVBHeader, pDummy, Iid, pDummy2)
MsgBox "NOW=" & Now & ",lpParameter=" & lpParameter, , "APP.TITLE=" & App.Title
Thread_Form_VSFlexGrid
vbCoUninitialize
End Sub
’in form
CreateThread ByVal 0&, ByVal 0&, AddressOf Test2, ByVal 345&, 0&, 0&
this method use windows api "CreateThread ",not use "vbCreateThread(**)"
"Thread_Form_VSFlexGrid" is a sub for show form with ocx(vsflexgrid or webbrowser ),it's ok,succesful;
but not support usercontrol
i like easy createthread methoad,Complete the function with a few lines of code。
If you're interested, you want to make a very simple way to call multithreaded methods together
Last edited by xiaoyao; Jan 27th, 2020 at 01:18 PM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by The trick
Just change this function as follows:
Code:
' // Create copy of VBHeader and other structures
' // The first four bytes contain thread ID. We use that ID to clean unused headers
Private Function CreateVBHeaderCopy() As Long
. . .
CopyMemory ByVal pHeader, ByVal pVBHeader, &H6A
' // ++++++++++++++++++++++++++++
' // to xiaoyao
' // Make Apartment threading model
' //
GetMem4 1&, ByVal pHeader + &H3C
' //
' // You should ensure extra cleaning like VBDllCanUnloadNow, etc.
' // ++++++++++++++++++++++++++++
' // Update strings offsets
CopyMemory pNames(0), ByVal pVBHeader + &H58, &H10
. . .
End Function
Code:
Private Sub ThreadProc(lpParameter As threadData)
CreateIExprSrvObj 0&, 4&, 0&
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED
TlsSetValue tlsIndex, lpParameter
VBDllGetClassObject VarPtrHModule, 0, PublpNewHdr, clsid, Iid, 0 '这句才是调用目标“函数”
HeapFree hHeap, 0, lpParameter
vbCoUninitialize
End Sub
public hModule as long
sub main()
*** more code for Initialize the thread info
hHeap = GetProcessHeap()
PublpNewHdr = HeapAlloc(hHeap, 0, &H6A)
hModule = App.hInstance
VarPtrHModule=VarPtr(hModule
sub main
this method use tls api for "vbCreateThread(**)"
i change for simple,use public var (VarPtrHModule=VarPtr(hModule))
i dont' khnow what bug will happend,i use the same "PublpNewHdr " in every thread.
in china ,Fewer and fewer people use VB6,one Programmer elite,he use hook api to fix for support "usercontrol"
The level is very high for you. Thank you for your help. I hope VB6 can be used for several years
you can send me a email## is @),i reply email to send the project to you
xiaoyaocode#163.com
Last edited by xiaoyao; Jan 27th, 2020 at 01:18 PM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
xiaoyao,
you can't "just use" the header because it has many things like for example global/static variables which are zeroed.
If you want to write me you can use Private Message here.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by Schmidt
Why do you want to avoid using other (additional) libraries
(later shipping with your Exe, e.g. in a \Bin\-sub-folder)?
You are using VB6 after all - a high-level-language which has its strengths exactly there
(in glueing selfdescribing COM-Objects and COM-Controls together in a RADish way).
If it is because of "avoiding to make a proper installer" - then you should learn about regfree-COM mechanisms first.
VB6 was not made for "normal, free-threading"...
It supports a different kind of threading instead, which requires you to make use of ActiveX-Dlls
(or ActiveX-Exes), when you want to play things "by the (VB6)book" (to avoid instabilities).
Olaf
if new thread support *.ocx ot vb6 usercontrols, it's also support createobject(*) , it's only com object like xmlhttp or adodb.stream.
ocx.and usercontrol that with ui,it's hard then com object.
why i use ocx? i think if webbrowser or usercontrol (more thread will use more cpu)
if every ocx run in every cpu, i think it will be run quickly。
buy webbrowser maybe only run in sta,maybe it can't write buffer file in disk on the same time(by more thread)?
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
use public vb header,tls,the same GetProcessHeap(),my code run successful,but i'm afraid,if more new thread run the same time in sub "ThreadProc",maybe read the same memory address,so errors will happend?
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by The trick
Seems it's the problem with the runtime. It registers the "Global Factory" thru CoRegisterClassObject from the main STA and when you create an usercontrol it calls CoGetClassObject. CoGetClassObject checks the registered Apartment ID i.e. you can't get this "Global Factory" from the different apartment. The same problem you'll see with ActiveX EXE project type. To avoid this you can for example use Load statement with a control array. Additionally you can set the Threading Model to Apartment (1) in VBHeader.dwThreadFlags when you create a VBHeader. In this case each thread (namely STA) will have its own HXMod internal object like in ActiveX DLL. Only in this case you need to performs additional cleaning like VBCanUnloadNow, etc.
Dear Trick,
When we use normal usercontrol with VBHeader.dwThreadFlags updated to 1 it works perfect.
But when we add usercontrol of type "Colorful Control" or "Control Events" and save as say xyz(xyz.ctl)
and try to load it at runtime to the form thru its button in new thread as follows:
Code:
Private Sub Command1_Click()
Dim x As Control
On Error GoTo err1
Controls.Add("FormThreading.xyz", "Ctl").Visible = True
Exit Sub
err1:
MsgBox "err:" & Err.Number & "," & Err.Description & "," & Err.Source
End Sub
I am getting the same following error
---------------------------
FormThreading
---------------------------
err:711,Invalid class string.
Looking for object with ProgID: FormThreading.xyz,FormThreading
---------------------------
OK
---------------------------
Why is this difference happening.Is there a way to solve the problem.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by The trick
You should uncheck "Remove Information About Unused Controls" checkbox in the project properties.
Dear Trick,
Thankyou . Now it works perfect after uncheck.
You tell me one thing that when I use
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED instead of CoInitialize ByVal 0& in
your modmultithreading ThreadProc as follows:
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
Dim COINIT_APARTMENTTHREADED As Long
COINIT_APARTMENTTHREADED = 1
Debug.Assert MakeTrue(bIsInIDE)
If Not bIsInIDE Then
Set cExpSrv = CreateIExprSrvObj(0, 4, 0)
End If
'CoInitialize ByVal 0&
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED
hHeap = GetProcessHeap()
TlsSetValue lTlsSlot, ByVal pParameter
GetMem8 ByVal pParameter, tThreadData
If bIsInIDE Then
FakeMain
Else
pNewHeader = CreateVBHeaderCopy()
where CreateVBHeaderCopy contains
GetMem4 1&, ByVal pHeader + &H3C
the new thread crashes,
If I comment the code
GetMem4 1&, ByVal pHeader + &H3C in CreateVBHeaderCopy and use
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED
Then no crash is there but the thread behaves like STA giving same error msg for usercontrol.
Can you tell me why the new thread crashes when we use both
GetMem4 1&, ByVal pHeader + &H3C in CreateVBHeaderCopy and CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED in ThreadProc
for FreeThreader component for VB6 which uses above bold API in the same order for runtime initialisation in CreateMT and EnableEvents functions in assembly language in the file MULTITHREAD.Asm .
It is described as follows: VB6 does not natively support free threading, but with this ActiveX library you can finally create multiple threads and make them communicate, write delegate functions (similarly to VB.NET) and use Structured Exception Handling. The full source code (in VB6 and Assembler) and some sample projects are provided.
Regarding internal working and stability of this code you can take help from Trick since I have less knowledge in ASM and already he helped me a lot regarding multithreading and he can better understand undocumented API like this.
regards,
JSVenu
Dear Trick,
Somewhere I read that ThunRTMain calls EbLoadRuntime.What does this Eb mean.
Can we use EbLoadRunTime, EbCreateContext, EbSetContextWorkerThread apis in pure vb6 for
runtime initialisation,write delegate functions (similarly to VB.NET) .
Can you show me a small threading example of their usage like you have shown for CreateIExprSrvObj, CoInitializeEx and vbdllgetclassobject apis.
Public U1 As Long, U2 As Long, U3 As Long, G3 As Long, pDummy As Long, pDummy2 As Long, IsVb6 As Boolean
Public Declare Function VBDllGetClassObject2 Lib "msvbvm60.dll" Alias "VBDllGetClassObject" (g1 As Long, g2 As Long, ByVal g3_vbHeader As Long, REFCLSID As Long, REFIID As Any, ppv As Long) As Long
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Can you tell me why the new thread crashes when we use both
GetMem4 1&, ByVal pHeader + &H3C in CreateVBHeaderCopy and
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED in ThreadProc
regards,
JSVenu
Please attach the project. I think CoInitializeEx has the bad declaration maybe because CoInitialize initializes STA.
Dear Trick,
Somewhere I read that ThunRTMain calls EbLoadRuntime.What does this Eb mean.
Can we use EbLoadRunTime, EbCreateContext, EbSetContextWorkerThread apis in pure vb6 for
runtime initialisation,write delegate functions (similarly to VB.NET) .
Eb means - Excel Basic.
You can use it in the restricted scenarios as i think. The solution depends on the task.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Dear Trick,
The trick;5448235]Please attach the project. I think CoInitializeEx has the bad declaration maybe because CoInitialize initializes STA.
I am attaching the project.
Please clarify why the new thread crashes on clicking Create Thread button when we use both
GetMem4 1&, ByVal pHeader + &H3C in CreateVBHeaderCopy and
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED in ThreadProc
Eb means - Excel Basic.
You can use it in the restricted scenarios as i think. The solution depends on the task.
Can you give me a small example of their (EbLoadRunTime, EbCreateContext, EbSetContextWorkerThread ) usage like we use vbdllgetclassobject api using declare.
For example we have
Declare Sub EbLoadRunTime Lib "msvbvm60" (ByVal BaseAddress As Long, ByVal BaseInit As Long)
But how to use EbLoadRunTime for runtime loading and EbCreateContext for creating context and setting context of any thread using EbSetContextWorkerThread .
regards,
JSVenu
Last edited by jsvenu; Jan 28th, 2020 at 10:55 PM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
As i expected the error in the declarations. From ObjBase.h
Code:
// COM initialization flags; passed to CoInitialize.
typedef enum tagCOINIT
{
COINIT_APARTMENTTHREADED = 0x2, // Apartment model
#if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM
// These constants are only valid on Windows NT 4.0
COINIT_MULTITHREADED = 0x0, // OLE calls objects on any thread.
COINIT_DISABLE_OLE1DDE = 0x4, // Don't use DDE for Ole1 support.
COINIT_SPEED_OVER_MEMORY = 0x8, // Trade memory for speed.
#endif // DCOM
} COINIT;
Whereas in your code COINIT_APARTMENTTHREADED = 1.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Originally Posted by The trick
As i expected the error in the declarations. From ObjBase.h
Code:
// COM initialization flags; passed to CoInitialize.
typedef enum tagCOINIT
{
COINIT_APARTMENTTHREADED = 0x2, // Apartment model
#if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM
// These constants are only valid on Windows NT 4.0
COINIT_MULTITHREADED = 0x0, // OLE calls objects on any thread.
COINIT_DISABLE_OLE1DDE = 0x4, // Don't use DDE for Ole1 support.
COINIT_SPEED_OVER_MEMORY = 0x8, // Trade memory for speed.
#endif // DCOM
} COINIT;
Whereas in your code COINIT_APARTMENTTHREADED = 1.
Dear Trick,
COINIT_APARTMENTTHREADED is already there in ThreadProc code as already mentioned post #72 as follow:
Code:
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
Dim COINIT_APARTMENTTHREADED As Long
COINIT_APARTMENTTHREADED = 1
Debug.Assert MakeTrue(bIsInIDE)
If Not bIsInIDE Then
Set cExpSrv = CreateIExprSrvObj(0, 4, 0)
End If
'CoInitialize ByVal 0&
CoInitializeEx ByVal 0&, ByVal COINIT_APARTMENTTHREADED
hHeap = GetProcessHeap()
TlsSetValue lTlsSlot, ByVal pParameter
GetMem8 ByVal pParameter, tThreadData
If bIsInIDE Then
FakeMain
Else
pNewHeader = CreateVBHeaderCopy()
If I use 1 for COINIT_APARTMENTTHREADED and use GetMem4 1&, ByVal pHeader + &H3C in CreateVBHeaderCopy I get crash when I click on Create Thread Button. Thankyou very much trick and I understood that I have to use 0 and 2 as value for STA and MTA.Now it works fine..
Eb means - Excel Basic.
You can use it in the restricted scenarios as i think. The solution depends on the task.
Can you give me a small example of their (EbLoadRunTime, EbCreateContext, EbSetContextWorkerThread ) usage like we use vbdllgetclassobject api using declare.
For example we have
Declare Sub EbLoadRunTime Lib "msvbvm60" (ByVal BaseAddress As Long, ByVal BaseInit As Long)
But how to use EbLoadRunTime for runtime loading and EbCreateContext for creating context and setting context of any thread using EbSetContextWorkerThread .
regards,
JSVenu
Last edited by jsvenu; Jan 29th, 2020 at 02:35 AM.
Re: using VbTrickThreading-master examples without the typelibs for Callback and Mar
Can you give me a small example of their (EbLoadRunTime, EbCreateContext, EbSetContextWorkerThread ) usage like we use vbdllgetclassobject api using declare.
For example we have
Declare Sub EbLoadRunTime Lib "msvbvm60" (ByVal BaseAddress As Long, ByVal BaseInit As Long)
But how to use EbLoadRunTime for runtime loading and EbCreateContext for creating context and setting context of any thread using EbSetContextWorkerThread .
I can’t come up with a practical situation when this can come in handy.