Calling CoInitialize( ) in a new thread crashes vb6 application 
Author Message
 Calling CoInitialize( ) in a new thread crashes vb6 application

Hi,

I created another thread from the main program/in response to a button even.
I tried to call CoInitialize( 0 ), but the app crashes pointing to some code
in MSVBM....

Is this not allowed or am i missing something ? Anywork arounds ?

Thanks for your time,
Raj

SAMPLE code:

Module1:

        Private Declare Function OleInitialize Lib "ole32.dll" (ByVal
lpreserved1 As Long) As Long
        Private Declare Sub OleUnInitialize Lib "ole32.dll" ()
        Private Declare Function CoInitializeEx Lib "ole32.dll" (ByVal
lpreserved1 As Long, apartment As Long) As Long
        Private Declare Function CoUninitialize Lib "ole32.dll" ()
        Private Declare Function CloseHandle Lib "kernel32.dll" (handleval
As Long) As Long

        Public Function ProcessThread(ByRef param As Long) As Long
        On Error GoTo errorexception

            CoInitialize (0)

            MsgBox "In thread"

            CoUninitialize

            Exit Function

        errorexception:

            MsgBox Err.Description

        End Function

Form1.frm

    Private Declare Function CreateThread Lib "kernel32" (lpThreadAttributes
As SECURITY_ATTRIBUTES, ByVal dwStackSize As Long, ByVal lpStartAddress As
Long,             lpParameter As Any, ByVal dwCreationFlags As Long,
lpThreadId As Long) As Long
    Private Type SECURITY_ATTRIBUTES
            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long
    End Type

    Private Declare Function OleInitialize Lib "ole32.dll" (ByVal
lpreserved1 As Long) As Long
    Private Declare Sub OleUninitialize Lib "ole32.dll" ()
    Private Declare Function CoInitializeEx Lib "ole32.dll" (ByVal
lpreserved1 As Long, apartment As Long) As Long
    Private Declare Function CoUninitialize Lib "ole32.dll" ()
    Private Declare Function CloseHandle Lib "kernel32.dll" (handleval As
Long) As Long

    Public Sub ProcessEmailRules()
    On Error GoTo errorexception

     '   OleInitialize (0)
        CoInitialize (0)

        Dim security As SECURITY_ATTRIBUTES
        security.nLength = 12
        security.bInheritHandle = 0
        security.lpSecurityDescriptor = 0

        Dim retval As Long
        retval = CreateThread(security, 0, AddressOf ProcessThread, 0, 0, 0)
        If (retval = 0) Then
            Debug.Print "Failed to create the email processing thread"

        End If

        CloseHandle (retval)

        '    OleUnInitialize
        CoUninitialize

        Debug.Print "Process email returns"
        Exit Sub

    errorexception:

        MsgBox "Err: " + Err.Description
       End Sub

    Private Sub Command1_Click()
        ProcessThread
    End Sub



Sat, 11 Oct 2003 16:17:37 GMT  
 Calling CoInitialize( ) in a new thread crashes vb6 application
Calling CreateThread in a Visual Basic program is not a good/safe thing to
do.  VB was not designed for free threading and that creating your own
threads using CreateThread API is not recommend and can have unforeseen
consequences. The supported method for creating a thread to execute
different tasks in VB is to create an ActiveX exe and set the Threading
Options on the project so that you can create objects from the exe that
will be created on their own threads.

 Two good places to get information on creating threads with VB:
1. Chapter 11, Threads and Synchronization, in {*filter*} Visual Basic by
Bruce McKinney
2. The Article "A Thread to Visual Basic" by Daniel Appleman located at
WWW.desaware.com  The above article and section in {*filter*} do give some
information on why working with CreateThread in VB is difficult.

Another execellent resource to consult is Matt Curland's book "Advanced
Visual Basic 6" which has a very good section on threading and VB.  You can
also find his old VBPJ articles ( from June 2000 I think it was ) on the
subject.

Hope this helps,

===========================
Gray McDonald
Microsoft Developer Support
===========================



Sat, 11 Oct 2003 22:29:39 GMT  
 Calling CoInitialize( ) in a new thread crashes vb6 application
Hi,

Thank you. It sure does help.

Rajasekar S



Quote:
> Calling CreateThread in a Visual Basic program is not a good/safe thing to
> do.  VB was not designed for free threading and that creating your own
> threads using CreateThread API is not recommend and can have unforeseen
> consequences. The supported method for creating a thread to execute
> different tasks in VB is to create an ActiveX exe and set the Threading
> Options on the project so that you can create objects from the exe that
> will be created on their own threads.

>  Two good places to get information on creating threads with VB:
> 1. Chapter 11, Threads and Synchronization, in {*filter*} Visual Basic by
> Bruce McKinney
> 2. The Article "A Thread to Visual Basic" by Daniel Appleman located at
> WWW.desaware.com  The above article and section in {*filter*} do give some
> information on why working with CreateThread in VB is difficult.

> Another execellent resource to consult is Matt Curland's book "Advanced
> Visual Basic 6" which has a very good section on threading and VB.  You
can
> also find his old VBPJ articles ( from June 2000 I think it was ) on the
> subject.

> Hope this helps,

> ===========================
> Gray McDonald
> Microsoft Developer Support
> ===========================



Mon, 13 Oct 2003 17:52:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. new thread for a specific function call or an entire object

2. "CoInitialize not called"

3. Is CoInitialize/OleInitialize called automatically in .Net?

4. Automation error The application called an interface that was marshalled for another thread

5. msInet system error CoInitialize has not been called?

6. vb6 activex ocx components used in multi-threaded client application

7. Add new component in Win 2000 - if rights=Restricted users crashes VB6

8. Add new component in Win 2000 - if rights=Restricted users crashes VB6

9. Postmessage api call crashes application on NT 4.0

10. Postmessage api call crashes application on NT 4.0

11. Application.Lock call crashes DLL with Win2k

12. Opening a form in a new thread, appart from the Apps main thread

 

 
Powered by phpBB® Forum Software