| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#include <windows.h> |
| 46 |
#include <objbase.h> |
| 47 |
|
| 48 |
#include "portaudio.h" |
| 49 |
#include "pa_util.h" |
| 50 |
#include "pa_debugprint.h" |
| 51 |
|
| 52 |
#include "pa_win_coinitialize.h" |
| 53 |
|
| 54 |
|
| 55 |
#if (defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) && !defined(_WIN32_WCE) && !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)) |
| 56 |
#pragma comment( lib, "ole32.lib" ) |
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#define PAWINUTIL_COM_INITIALIZED (0xb38f) |
| 62 |
#define PAWINUTIL_COM_NOT_INITIALIZED (0xf1cd) |
| 63 |
|
| 64 |
|
| 65 |
PaError PaWinUtil_CoInitialize( PaHostApiTypeId hostApiType, PaWinUtilComInitializationResult *comInitializationResult ) |
| 66 |
{ |
| 67 |
HRESULT hr; |
| 68 |
|
| 69 |
comInitializationResult->state = PAWINUTIL_COM_NOT_INITIALIZED; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP) |
| 80 |
hr = CoInitialize(0); |
| 81 |
#else |
| 82 |
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); |
| 83 |
#endif |
| 84 |
if( FAILED(hr) && hr != RPC_E_CHANGED_MODE ) |
| 85 |
{ |
| 86 |
PA_DEBUG(("CoInitialize(0) failed. hr=%d\n", hr)); |
| 87 |
|
| 88 |
if( hr == E_OUTOFMEMORY ) |
| 89 |
return paInsufficientMemory; |
| 90 |
|
| 91 |
{ |
| 92 |
char *lpMsgBuf; |
| 93 |
FormatMessage( |
| 94 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 95 |
NULL, |
| 96 |
hr, |
| 97 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 98 |
(LPTSTR) &lpMsgBuf, |
| 99 |
0, |
| 100 |
NULL |
| 101 |
); |
| 102 |
PaUtil_SetLastHostErrorInfo( hostApiType, hr, lpMsgBuf ); |
| 103 |
LocalFree( lpMsgBuf ); |
| 104 |
} |
| 105 |
|
| 106 |
return paUnanticipatedHostError; |
| 107 |
} |
| 108 |
|
| 109 |
if( hr != RPC_E_CHANGED_MODE ) |
| 110 |
{ |
| 111 |
comInitializationResult->state = PAWINUTIL_COM_INITIALIZED; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
comInitializationResult->initializingThreadId = GetCurrentThreadId(); |
| 119 |
} |
| 120 |
|
| 121 |
return paNoError; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
void PaWinUtil_CoUninitialize( PaHostApiTypeId hostApiType, PaWinUtilComInitializationResult *comInitializationResult ) |
| 126 |
{ |
| 127 |
if( comInitializationResult->state != PAWINUTIL_COM_NOT_INITIALIZED |
| 128 |
&& comInitializationResult->state != PAWINUTIL_COM_INITIALIZED ){ |
| 129 |
|
| 130 |
PA_DEBUG(("ERROR: PaWinUtil_CoUninitialize called without calling PaWinUtil_CoInitialize\n")); |
| 131 |
} |
| 132 |
|
| 133 |
if( comInitializationResult->state == PAWINUTIL_COM_INITIALIZED ) |
| 134 |
{ |
| 135 |
DWORD currentThreadId = GetCurrentThreadId(); |
| 136 |
if( comInitializationResult->initializingThreadId != currentThreadId ) |
| 137 |
{ |
| 138 |
PA_DEBUG(("ERROR: failed PaWinUtil_CoUninitialize calling thread[%d] does not match initializing thread[%d]\n", |
| 139 |
currentThreadId, comInitializationResult->initializingThreadId)); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
CoUninitialize(); |
| 144 |
|
| 145 |
comInitializationResult->state = PAWINUTIL_COM_NOT_INITIALIZED; |
| 146 |
} |
| 147 |
} |
| 148 |
} |