code muncher
code muncher

Reputation: 1682

Windows Metro mode (C++): Waiting on semaphore (WaitForSingleObjectEx): failed with Access Denied [partially solved]

May be this is stupid question for you .. but it doesn't work for me! (New to WinRT .. may be windows too!) Just trying to create semaphore and trying to acquire that's it .. nothing fancy. But it fails with WAIT_FAILED: 5 (Access denied)

void MainPage::simple_Sema_test1()
{

    HANDLE p_sema;
    DWORD ret, err;
    p_sema = CreateSemaphoreEx(NULL, 1, MAX_LIMIT, L"sema1", 0, SEMAPHORE_MODIFY_STATE);
    if(p_sema == NULL)
        print_on_textbox("CreateSemaphoreEx Failed!\n");
    ret = WaitForSingleObjectEx(p_sema, 1000, TRUE);
    switch(ret){
    case WAIT_ABANDONED:
        print_on_textbox("WAIT_ABANDONED\n");
    case WAIT_IO_COMPLETION:
        print_on_textbox("WAIT_IO_COMPLETION\n");
    case WAIT_OBJECT_0:
        print_on_textbox("WAIT_OBJECT_0\n");
    case WAIT_TIMEOUT:
        print_on_textbox("WAIT_TIMEOUT\n");
    case WAIT_FAILED:
        print_on_textbox("WAIT_FAILED: " + (GetLastError()).ToString());
}

Edit <<< (Partially solved) Create Semaphore with SEMAPHORE_ALL_ACCESS

i.e.

*sema = CreateSemaphoreEx(NULL, initial, MAX_LIMIT, L"sema1", 0, SEMAPHORE_ALL_ACCESS);

If anyone knows why it didn't work with modify access then please explain!

Upvotes: 1

Views: 1759

Answers (1)

James McNellis
James McNellis

Reputation: 355207

To wait on a synchronization object, you must have SYNCHRONIZE access rights:

SYNCHRONIZE: The right to use the object for synchronization. This enables a thread to wait until the object is in the signaled state.

The SEMAPHORE_ALL_ACCESS value includes the SYNCHRONIZE flag.

Upvotes: 4

Related Questions