pa_win_coinitialize.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Microsoft COM initialization routines
  3. * Copyright (c) 1999-2011 Ross Bencina, Dmitry Kostjuchenko
  4. *
  5. * Based on the Open Source API proposed by Ross Bencina
  6. * Copyright (c) 1999-2011 Ross Bencina, Phil Burk
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files
  10. * (the "Software"), to deal in the Software without restriction,
  11. * including without limitation the rights to use, copy, modify, merge,
  12. * publish, distribute, sublicense, and/or sell copies of the Software,
  13. * and to permit persons to whom the Software is furnished to do so,
  14. * subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  23. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  24. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. /*
  28. * The text above constitutes the entire PortAudio license; however,
  29. * the PortAudio community also makes the following non-binding requests:
  30. *
  31. * Any person wishing to distribute modifications to the Software is
  32. * requested to send the modifications to the original developer so that
  33. * they can be incorporated into the canonical version. It is also
  34. * requested that these non-binding requests be included along with the
  35. * license above.
  36. */
  37. /** @file
  38. @ingroup win_src
  39. @brief Microsoft COM initialization routines.
  40. */
  41. #include <windows.h>
  42. #include <objbase.h>
  43. #include "portaudio.h"
  44. #include "pa_util.h"
  45. #include "pa_debugprint.h"
  46. #include "pa_win_coinitialize.h"
  47. #if (defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) && !defined(_WIN32_WCE) /* MSC version 6 and above */
  48. #pragma comment( lib, "ole32.lib" )
  49. #endif
  50. /* use some special bit patterns here to try to guard against uninitialized memory errors */
  51. #define PAWINUTIL_COM_INITIALIZED (0xb38f)
  52. #define PAWINUTIL_COM_NOT_INITIALIZED (0xf1cd)
  53. PaError PaWinUtil_CoInitialize( PaHostApiTypeId hostApiType, PaWinUtilComInitializationResult *comInitializationResult )
  54. {
  55. HRESULT hr;
  56. comInitializationResult->state = PAWINUTIL_COM_NOT_INITIALIZED;
  57. /*
  58. If COM is already initialized CoInitialize will either return
  59. FALSE, or RPC_E_CHANGED_MODE if it was initialised in a different
  60. threading mode. In either case we shouldn't consider it an error
  61. but we need to be careful to not call CoUninitialize() if
  62. RPC_E_CHANGED_MODE was returned.
  63. */
  64. hr = CoInitialize(0); /* use legacy-safe equivalent to CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) */
  65. if( FAILED(hr) && hr != RPC_E_CHANGED_MODE )
  66. {
  67. PA_DEBUG(("CoInitialize(0) failed. hr=%d\n", hr));
  68. if( hr == E_OUTOFMEMORY )
  69. return paInsufficientMemory;
  70. {
  71. char *lpMsgBuf;
  72. FormatMessage(
  73. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  74. NULL,
  75. hr,
  76. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  77. (LPTSTR) &lpMsgBuf,
  78. 0,
  79. NULL
  80. );
  81. PaUtil_SetLastHostErrorInfo( hostApiType, hr, lpMsgBuf );
  82. LocalFree( lpMsgBuf );
  83. }
  84. return paUnanticipatedHostError;
  85. }
  86. if( hr != RPC_E_CHANGED_MODE )
  87. {
  88. comInitializationResult->state = PAWINUTIL_COM_INITIALIZED;
  89. /*
  90. Memorize calling thread id and report warning on Uninitialize if
  91. calling thread is different as CoInitialize must match CoUninitialize
  92. in the same thread.
  93. */
  94. comInitializationResult->initializingThreadId = GetCurrentThreadId();
  95. }
  96. return paNoError;
  97. }
  98. void PaWinUtil_CoUninitialize( PaHostApiTypeId hostApiType, PaWinUtilComInitializationResult *comInitializationResult )
  99. {
  100. if( comInitializationResult->state != PAWINUTIL_COM_NOT_INITIALIZED
  101. && comInitializationResult->state != PAWINUTIL_COM_INITIALIZED ){
  102. PA_DEBUG(("ERROR: PaWinUtil_CoUninitialize called without calling PaWinUtil_CoInitialize\n"));
  103. }
  104. if( comInitializationResult->state == PAWINUTIL_COM_INITIALIZED )
  105. {
  106. DWORD currentThreadId = GetCurrentThreadId();
  107. if( comInitializationResult->initializingThreadId != currentThreadId )
  108. {
  109. PA_DEBUG(("ERROR: failed PaWinUtil_CoUninitialize calling thread[%d] does not match initializing thread[%d]\n",
  110. currentThreadId, comInitializationResult->initializingThreadId));
  111. }
  112. else
  113. {
  114. CoUninitialize();
  115. comInitializationResult->state = PAWINUTIL_COM_NOT_INITIALIZED;
  116. }
  117. }
  118. }