VB6 API For CoInitializeEx and CoUninitialize of ole32.dll

Option Explicit
'Satisfying API For CoInitializeEx and CoUninitialize of ole32.dll
'Explained and MSDN referenced.
' Elad Karako

Private Enum COINIT_HRESULT
    S_OK = &H0&  'The COM library was initialized successfully on this thread.
    S_FALSE = &H1& 'The COM library is already initialized on this thread.
    RPC_E_CHANGED_MODE = &H80010106   'A previous call to CoInitializeEx specified the concurrency model for this thread as multithread apartment (MTA). This could also indicate that a change from neutral-threaded apartment to single-threaded apartment has occurred.
    E_INVALIDARG = &H80070057 'One or more arguments are invalid.
    E_OUTOFMEMORY = &H80000002 'Ran out of memory.
    E_UNEXPECTED = &H80041316 'The task XML contains an unexpected node.
End Enum

'COINIT Enumeration
'Determines the concurrency model used for incoming calls to objects created by this thread. This concurrency model can be either apartment-threaded or multi-threaded.
'see http://msdn.microsoft.com/en-us/library/ms678505(v=VS.85).aspx
'
'YOU SHOULD ONLY USE COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED.
Private Enum COINIT
  COINIT_APARTMENTTHREADED = &H2 'Initializes the thread for apartment-threaded object concurrency.
  COINIT_MULTITHREADED = &H0 'Initializes the thread for multi-threaded object concurrency.
  COINIT_DISABLE_OLE1DDE = &H4 'Disables DDE for OLE1 support.
  COINIT_SPEED_OVER_MEMORY = &H8 'Trade memory for speed.
End Enum

'CoInitializeEx Function
'Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.
'see http://msdn.microsoft.com/en-us/library/ms695279(v=VS.85).aspx
Private Declare Function CoInitializeEx Lib "ole32.dll" ( _
    ByVal pvReserved As Long, _
    ByVal dwCoInit As Long) As COINIT_HRESULT

'CoUninitialize Function
'Closes the COM library on the current thread, unloads all DLLs loaded by the thread, frees any other resources that the thread maintains, and forces all RPC connections on the thread to close.
'see http://msdn.microsoft.com/en-us/library/ms688715(v=VS.85).aspx
Private Declare Function CoUninitialize Lib "ole32.dll" ()


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


'CoInitializeEx Function
'Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.
'see http://msdn.microsoft.com/en-us/library/ms695279(v=VS.85).aspx
Public Sub do_CoInitializeEx(Optional c As COINIT = COINIT_APARTMENTTHREADED, Optional bDebugResult As Boolean = False)
    Dim coihr As COINIT_HRESULT
    coihr = CoInitializeEx(ByVal 0&, c)
    If (bDebugResult) Then MsgBox toString(coihr)
    DoEvents
End Sub

'CoUninitialize Function
'Closes the COM library on the current thread, unloads all DLLs loaded by the thread, frees any other resources that the thread maintains, and forces all RPC connections on the thread to close.
'see http://msdn.microsoft.com/en-us/library/ms688715(v=VS.85).aspx
Public Sub do_CoUninitialize()
    CoUninitialize
    DoEvents
End Sub

'ment for debuging the result of CoInitializeEx Function,
'strings are from WinError.h and MSDN
Private Function toString(ByVal coihr As COINIT_HRESULT) As String
    If (coihr = HRESULT.S_OK) Then
        HRESULT_toString = "S_OK: The COM library was initialized successfully on this thread."
    ElseIf (coihr = HRESULT.S_FALSE) Then
        HRESULT_toString = "S_FALSE: The COM library is already initialized on this thread."
    ElseIf (coihr = HRESULT.RPC_E_CHANGED_MODE) Then
        HRESULT_toString = "RPC_E_CHANGED_MODE: A previous call to CoInitializeEx specified the concurrency model for this thread as multithread apartment (MTA). This could also indicate that a change from neutral-threaded apartment to single-threaded apartment has occurred."
    ElseIf (coihr = HRESULT.E_INVALIDARG) Then
        HRESULT_toString = "E_INVALIDARG: One or more arguments are invalid."
    ElseIf (coihr = HRESULT.E_OUTOFMEMORY) Then
        HRESULT_toString = "E_OUTOFMEMORY: Ran out of memory."
    ElseIf (coihr = HRESULT.E_UNEXPECTED) Then
        HRESULT_toString = "E_UNEXPECTED: The task XML contains an unexpected node."
    Else
        HRESULT_toString = CStr(coihr) & "(?) Unknown."
    End If
End Function

Leave a Reply