| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================================= |
| 6 | ** |
| 7 | ** Source: test4.c |
| 8 | ** |
| 9 | ** Purpose: Positive test for OpenEventW. |
| 10 | ** |
| 11 | ** Dependencies: PAL_Initialize |
| 12 | ** PAL_Terminate |
| 13 | ** CreateEvent |
| 14 | ** CloseHandle |
| 15 | ** WaitForSingleObject |
| 16 | ** |
| 17 | ** Purpose: |
| 18 | ** |
| 19 | ** Test to ensure proper operation of the OpenEventW() |
| 20 | ** API by trying to open an event with a name that is |
| 21 | ** already taken by a non-event object. |
| 22 | ** |
| 23 | ** |
| 24 | **===========================================================================*/ |
| 25 | #include <palsuite.h> |
| 26 | |
| 27 | |
| 28 | |
| 29 | int __cdecl main( int argc, char **argv ) |
| 30 | |
| 31 | { |
| 32 | /* local variables */ |
| 33 | BOOL bRet = PASS; |
| 34 | DWORD dwLastError = 0; |
| 35 | HANDLE hMutex = NULL; |
| 36 | HANDLE hTestEvent = NULL; |
| 37 | LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL; |
| 38 | BOOL bInitialState = TRUE; |
| 39 | WCHAR wcName[] = {'I','m','A','M','u','t','e','x','\0'}; |
| 40 | LPWSTR lpName = wcName; |
| 41 | |
| 42 | |
| 43 | /* PAL initialization */ |
| 44 | if( (PAL_Initialize(argc, argv)) != 0 ) |
| 45 | { |
| 46 | return( FAIL ); |
| 47 | } |
| 48 | |
| 49 | /* create a mutex object */ |
| 50 | hMutex = CreateMutexW( lpSecurityAttributes, |
| 51 | bInitialState, |
| 52 | lpName ); |
| 53 | |
| 54 | if( hMutex == NULL ) |
| 55 | { |
| 56 | /* ERROR */ |
| 57 | Fail( "ERROR:%lu:CreateMutexW() call failed\n" , GetLastError() ); |
| 58 | } |
| 59 | |
| 60 | /* open a new handle to our event */ |
| 61 | hTestEvent = OpenEventW(EVENT_ALL_ACCESS, /* we want all rights */ |
| 62 | FALSE, /* no inherit */ |
| 63 | lpName ); |
| 64 | |
| 65 | if( hTestEvent != NULL ) |
| 66 | { |
| 67 | /* ERROR */ |
| 68 | Trace( "ERROR:OpenEventW() call succeeded against a named " |
| 69 | "mutex, should have returned NULL\n" ); |
| 70 | if( ! CloseHandle( hTestEvent ) ) |
| 71 | { |
| 72 | Trace( "ERROR:%lu:CloseHandle() call failed \n" , GetLastError() ); |
| 73 | } |
| 74 | bRet = FAIL; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | dwLastError = GetLastError(); |
| 79 | if( dwLastError != ERROR_INVALID_HANDLE ) |
| 80 | { |
| 81 | /* ERROR */ |
| 82 | Trace( "ERROR:OpenEventW() call failed against a named " |
| 83 | "mutex, but returned an unexpected result: %lu\n" , |
| 84 | dwLastError ); |
| 85 | bRet = FAIL; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* close the mutex handle */ |
| 91 | if( ! CloseHandle( hMutex ) ) |
| 92 | { |
| 93 | Trace( "ERROR:%lu:CloseHandle() call failed \n" , GetLastError() ); |
| 94 | bRet = FAIL; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* fail here if we weren't successful */ |
| 99 | if( bRet == FAIL ) |
| 100 | { |
| 101 | Fail( "" ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* PAL termination */ |
| 106 | PAL_Terminate(); |
| 107 | |
| 108 | /* return success or failure */ |
| 109 | return PASS; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | |