Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using HANDLE in different class & thread

Reply
Thread Tools

Using HANDLE in different class & thread

 
 
Donos
Guest
Posts: n/a
 
      10-18-2007
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-18-2007
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


 
Reply With Quote
 
 
 
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      10-19-2007
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:ff8or7$u97$(E-Mail Removed)...
> 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]

 
Reply With Quote
 
Andrew
Guest
Posts: n/a
 
      10-19-2007
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" <(E-Mail Removed)> wrote in message
>
> news:ff8or7$u97$(E-Mail Removed)...
>
>
>
>
>
> > 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 -



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-19-2007
Scott McPhillips [MVP] wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:ff8or7$u97$(E-Mail Removed)...
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Package to handle table text render (handle space or tab betweenthe columns) ? =?ISO-8859-1?Q?KLEIN_St=E9phane?= Python 3 10-06-2006 08:46 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Possible to handle web requests without an ASPX page? i.e. have DLL handle request. jdlwright@shaw.ca ASP .Net 2 05-31-2005 05:42 PM
how to handle command line output(not terminal handle) Leon Python 2 11-04-2004 05:16 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM



Advertisments