Using HANDLE in different class & thread

Discussion in 'C++' started by Donos, Oct 18, 2007.

  1. Donos

    Donos Guest

    Hi

    I have a HANDLE to an Event, like this..

    HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL);

    This is running in one thread in one class. For example we will call
    that class as "Class A"

    Now i want to use this HANDLE in another thread in another class to
    call SetEvent(h); This is "Class B"

    I tried creating a pointer to Class A and using it, But it gets an
    invalid HANDLE.

    Any idea how this can be done?
    Donos, Oct 18, 2007
    #1
    1. Advertising

  2. Donos wrote:
    > I have a HANDLE to an Event, like this..
    >
    > HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    >
    > This is running in one thread in one class. For example we will call
    > that class as "Class A"
    >
    > Now i want to use this HANDLE in another thread in another class to
    > call SetEvent(h); This is "Class B"
    >
    > I tried creating a pointer to Class A and using it, But it gets an
    > invalid HANDLE.
    >
    > Any idea how this can be done?


    This is not a C++ language question. This is a Windows programming
    question. Please ask Windows programming questions in the Windows
    programming newsgroup.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 19, 2007
    #2
    1. Advertising

  3. "Victor Bazarov" <> wrote in message
    news:ff8or7$u97$...
    > Donos wrote:
    >> I have a HANDLE to an Event, like this..
    >>
    >> HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    >>
    >> This is running in one thread in one class. For example we will call
    >> that class as "Class A"
    >>
    >> Now i want to use this HANDLE in another thread in another class to
    >> call SetEvent(h); This is "Class B"
    >>
    >> I tried creating a pointer to Class A and using it, But it gets an
    >> invalid HANDLE.
    >>
    >> Any idea how this can be done?

    >
    > This is not a C++ language question. This is a Windows programming
    > question. Please ask Windows programming questions in the Windows
    > programming newsgroup.



    V: It is a very fundamental C++ language question, even though it uses
    Windows terms. The OP clearly needs help with C++ concepts.

    Donos: The problem stems from a lack of understanding of the difference
    between a class and an object of that class. This is best approached by
    careful study of the first few chapters of any C++ book. Classes do not
    store any data, only objects do that.

    The handle is not stored in class A, it is stored in an object whose type is
    class A. Creating a pointer to Class A elsewhere does not magically make it
    point at the original A object. If object B wishes to access a member
    variable of object A then object B must have a pointer (or reference) to the
    original A object and this pointer must be initialized with the address of
    the A object. Also notice that h must be a member variable, not the
    automatic variable you show in your code.

    class A {
    public:
    HANDLE h;
    A::A() { h = CreateEvent(NULL, FALSE, FALSE, NULL);}
    };

    // some function in class B
    A* a = new A();
    HANDLE hcopy = a->h;

    --
    Scott McPhillips [VC++ MVP]
    Scott McPhillips [MVP], Oct 19, 2007
    #3
  4. Donos

    Andrew Guest

    Also, i think it's wrong to expose class internal HANDLE. That's
    breaking encapsulation.


    On Oct 19, 4:52 am, "Scott McPhillips [MVP]" <org-dot-mvps-at-
    scottmcp> wrote:
    > "Victor Bazarov" <> wrote in message
    >
    > news:ff8or7$u97$...
    >
    >
    >
    >
    >
    > > Donos wrote:
    > >> I have a HANDLE to an Event, like this..

    >
    > >> HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL);

    >
    > >> This is running in one thread in one class. For example we will call
    > >> that class as "Class A"

    >
    > >> Now i want to use this HANDLE in another thread in another class to
    > >> call SetEvent(h); This is "Class B"

    >
    > >> I tried creating a pointer to Class A and using it, But it gets an
    > >> invalid HANDLE.

    >
    > >> Any idea how this can be done?

    >
    > > This is not a C++ language question. This is a Windows programming
    > > question. Please ask Windows programming questions in the Windows
    > > programming newsgroup.

    >
    > V: It is a very fundamental C++ language question, even though it uses
    > Windows terms. The OP clearly needs help with C++ concepts.
    >
    > Donos: The problem stems from a lack of understanding of the difference
    > between a class and an object of that class. This is best approached by
    > careful study of the first few chapters of any C++ book. Classes do not
    > store any data, only objects do that.
    >
    > The handle is not stored in class A, it is stored in an object whose type is
    > class A. Creating a pointer to Class A elsewhere does not magically make it
    > point at the original A object. If object B wishes to access a member
    > variable of object A then object B must have a pointer (or reference) to the
    > original A object and this pointer must be initialized with the address of
    > the A object. Also notice that h must be a member variable, not the
    > automatic variable you show in your code.
    >
    > class A {
    > public:
    > HANDLE h;
    > A::A() { h = CreateEvent(NULL, FALSE, FALSE, NULL);}
    >
    > };
    >
    > // some function in class B
    > A* a = new A();
    > HANDLE hcopy = a->h;
    >
    > --
    > Scott McPhillips [VC++ MVP]- Hide quoted text -
    >
    > - Show quoted text -
    Andrew, Oct 19, 2007
    #4
  5. Scott McPhillips [MVP] wrote:
    > "Victor Bazarov" <> wrote in message
    > news:ff8or7$u97$...
    >> Donos wrote:
    >>> I have a HANDLE to an Event, like this..
    >>>
    >>> HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL);
    >>>
    >>> This is running in one thread in one class. For example we will call
    >>> that class as "Class A"
    >>>
    >>> Now i want to use this HANDLE in another thread in another class to
    >>> call SetEvent(h); This is "Class B"
    >>>
    >>> I tried creating a pointer to Class A and using it, But it gets an
    >>> invalid HANDLE.
    >>>
    >>> Any idea how this can be done?

    >>
    >> This is not a C++ language question. This is a Windows programming
    >> question. Please ask Windows programming questions in the Windows
    >> programming newsgroup.

    >
    >
    > V: It is a very fundamental C++ language question, even though it uses
    > Windows terms. The OP clearly needs help with C++ concepts.


    Might be. As I read it, the question was similar to "I create an event
    in thread 1, then I need to call SetEvent() for that event in thread 2,
    how do I do it? How the hell is that a fundamental C++ question?

    > Donos: The problem stems from a lack of understanding of the
    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    640
    Joe Smith
    Jul 22, 2004
  2. Aditi
    Replies:
    2
    Views:
    1,443
    Aditi
    Jun 24, 2005
  3. E11
    Replies:
    1
    Views:
    4,742
    Thomas Weidenfeller
    Oct 12, 2005
  4. Leon
    Replies:
    2
    Views:
    522
  5. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    445
    hanumizzle
    Oct 6, 2006
Loading...

Share This Page