none
Issue with OpenEvent RRS feed

  • Question

  • I 'm having 2 processes, one for creating Event and other for getting the event name.

    While using OpenEvent, I don't get the Event Handler properly. Sometime i get and sometime it's NULL. I do see the Event created properly with the valid handle. I could print the same name getting passed to OpenEvent call also.

    what could go wrong..?

    //Process 1 - OpenEvent process
    static HANDLE hBinEvent[100];
    void GetEventName(int a_Process_number, int eventcount)
    {
    char h_str[2] = { 0 };
    char h_EventName[20] = "Process_";
    int stringlength;
    stringlength = strlen(h_EventName);
    itoa(a_Process_number, h_str, 10);
    strcat(h_EventName, h_str);
    h_EventName[stringlength + 1] = '\0';
    do {
    hBinEvent[eventcount] = OpenEvent(SYNCHRONIZE, FALSE, LPCWSTR(h_EventName));
    printf((h_EventName));            \\ name gets printed properly - 'Process_0' and also for 'Process_1' 
    } while (NULL == hBinEvent[eventcount]);
    }

    int main(void)
    {
    GetEventName(1, 0);
    GetEventName(1, 1);
    return 0;
    }



    //Process 2 - CreateEvent process
    static HANDLE gBinEvent;
    void CreateEventName()
    {
    char g_str[2] = {0};
    char g_EventName[20] = "Process_";
    int stringlength;
    stringlength = LSL_strlen(g_EventName);
    LSL_itoa(a_Process_number, g_str, 10);
    LSL_strcat(g_EventName, g_str);
    g_EventName[stringlength + 1] = '\0';
    do{
    gBinEvent = CreateEvent(
    NULL,               
    FALSE,               
    FALSE,              
    LPCWSTR(g_EventName)             
    );
    printf((g_EventName));                   \\ name gets printed properly - 'Process_0' and also for 'Process_1' 
    } while (NULL == gBinEvent);
    }

    int main(void)
    {
    CreateEventName(0);
    CreateEventName(1);
    return 0;
    }

    Thursday, September 19, 2019 12:41 PM

Answers

  • If I make it like this, it works fine
    LPCWSTR("Process_1") and LPCWSTR("Process_0").

    These are coding errors.  Plain and simple.

    To see what you are really passing to CreateEvent and OpenEvent, use the debugger to view the  string pointed to by pwsz as follows -

    LPCWSTR pwsz = LPCWSTR("Process_1")

    It won't be "Process_1".

    If you remove the cast the compiler will issue an error.




    • Edited by RLWA32 Thursday, September 19, 2019 3:53 PM
    • Marked as answer by Harsha_MR Thursday, September 19, 2019 6:01 PM
    Thursday, September 19, 2019 3:13 PM
  • You need to l listen to RLWA32.  He is absolutely correct.  You cannot convert an ANSI string to a Unicode string by casting.  Consider the string "ABCD".  As an ANSI string, the hex values would be:

        41 42 43 44 00

    But L"ABCD" as a wide string is

        41 00 42 00 43 00 44 00 00 00

    If you pass "ABCD" to an API that expects Unicode, it will see two characters: 0x4241 and 0x4443, neither of which are valid Unicode characters.

    If you really want to use ANSI strings, and that's reasonable, then the easy solution is for you to call OpenEventA instead of OpenEvent.  Either that, or change the character set for your project from Unicode to MBCS.

    Here is a hard and fast rule:  >>> ANY TIME <<< you are forced to insert a cast, you need to stop and question what you are doing.  In virtually every case, casts are evil, and indicate that you have done something wrong.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Harsha_MR Thursday, September 19, 2019 6:01 PM
    Thursday, September 19, 2019 5:47 PM

All replies

  • Hello,

    if OpenEvent fails (return NULL), you should check with GetLastError why it fails. Tell us the error then.

    Regards, Guido

    Thursday, September 19, 2019 12:56 PM
  • In addition,

    read the doc of OpenEvent: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-openeventa

    You must create an event before you can call OpenEvent: "The function succeeds only if some process has already created the event by using the CreateEvent function"

    Is the event in the other process already created when you call OpenEvent?

    Again: check GetLastError.

    --

    By the way, there are some mistakes in your code:

    1. while OpenEvent returns NULL, you run in an endless loop.

    do {
      hBinEvent[eventcount] = OpenEvent(SYNCHRONIZE, FALSE, LPCWSTR(h_EventName));
      printf((h_EventName));            \\ name gets printed properly - 'Process_0' and also for 'Process_1' 
    } while (NULL == hBinEvent[eventcount]);

    So it must get != NULL to go on.

    2. you call CreateEventName with Parameters, but it's a function without Parameter list.

    Regards, Guido



    Thursday, September 19, 2019 1:04 PM
  • In addition to Guido Franzke's comments also consider -

    Since you are using narrow strings for g_EventName and h_EventName

    LPCWSTR(g_EventName) and LPCWSTR(h_EventName)
    

    are errors.  You cannot transform a narrow string into a wide string by casting.  So what is passed to CreateEvent and OpenEvent are NOT the names you think they are.

    And if the process that creates events terminates before the other process opens the events the event objects will no longer exist.  The system automatically closes handles upon process termination and since there are no other open handles to the event objects they will be destroyed by the system.

    Thursday, September 19, 2019 1:55 PM
  • To answer to Your comments, 
    1) I have made sure that Event in the other process is already created before calling OpenEvent
    2) I have removed the processing part in the sampple code(Process 1) posted here. 
    After getting Event names, I have other code written running in infinite loop.
    3) I'm checking for this - while (NULL == hBinEvent[eventcount]) since i need to process further only after getting EVENT Handle.
    4) In CreateEventName(), i'm actually having CreateEventName(int a_Process_number).
    Thursday, September 19, 2019 3:08 PM
  • If I make it like this, it works fine
    LPCWSTR("Process_1") and LPCWSTR("Process_0").
    But, I want to have passed through pointer since I have to decide on Process number at run time.

    I have removed the processing part in the sample code(Process 1) posted here. 
    After getting Event names, I have other code written running in infinite loop. 
    Event in the other process is already created before calling OpenEvent
    Thursday, September 19, 2019 3:08 PM
  • If I make it like this, it works fine
    LPCWSTR("Process_1") and LPCWSTR("Process_0").

    These are coding errors.  Plain and simple.

    To see what you are really passing to CreateEvent and OpenEvent, use the debugger to view the  string pointed to by pwsz as follows -

    LPCWSTR pwsz = LPCWSTR("Process_1")

    It won't be "Process_1".

    If you remove the cast the compiler will issue an error.




    • Edited by RLWA32 Thursday, September 19, 2019 3:53 PM
    • Marked as answer by Harsha_MR Thursday, September 19, 2019 6:01 PM
    Thursday, September 19, 2019 3:13 PM
  • You need to l listen to RLWA32.  He is absolutely correct.  You cannot convert an ANSI string to a Unicode string by casting.  Consider the string "ABCD".  As an ANSI string, the hex values would be:

        41 42 43 44 00

    But L"ABCD" as a wide string is

        41 00 42 00 43 00 44 00 00 00

    If you pass "ABCD" to an API that expects Unicode, it will see two characters: 0x4241 and 0x4443, neither of which are valid Unicode characters.

    If you really want to use ANSI strings, and that's reasonable, then the easy solution is for you to call OpenEventA instead of OpenEvent.  Either that, or change the character set for your project from Unicode to MBCS.

    Here is a hard and fast rule:  >>> ANY TIME <<< you are forced to insert a cast, you need to stop and question what you are doing.  In virtually every case, casts are evil, and indicate that you have done something wrong.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Harsha_MR Thursday, September 19, 2019 6:01 PM
    Thursday, September 19, 2019 5:47 PM