473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Class member method

Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.
Thanks in advance for help.
Regards.


Jul 22 '05 #1
12 2389
"MacFly" <ma*******@wp.pl> wrote in message
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to class variables, but don't know how to do it ?
I still receive errors when I try to use it.


This is off-topic, since the you're using a number of
platform-specific macros. A good group to post to would be
microsoft.public.dotnet.languages.vc.

However, I think you will find that you can make the function a static
member without error.

Jonathan
Jul 22 '05 #2
On the contrary, this is not at all off topic.

The basic question is how to associate an instance of a class through c-style
callbacks.

I'm not sure about the specifics of the callback, but it looks like the
pvUserContext could be used to hold the instance of your class (cast as a void
pointer). If that is the case, then simply pass in the instance of your class
to the routine that initially takes the pvUserContext variable, then in the
context of the DirectPlayMessageHandler, simply cast the pvUserContext to a
pointer of your class type and go from there.

Something like this

Initialize_Function((void *)&your_class_instance, DirectPlayMessageHandler, /*
etc */);

// ...

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)
{
your_class_type * pointer_to_your_class_instance = (your_class_type
*)pvUserContext;
pointer_to_your_class_instance->method1(); // or whatever methods you want to
call
// etc.
}

Get it?
Jonathan Turkanis wrote:

"MacFly" <ma*******@wp.pl> wrote in message
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access

to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.


This is off-topic, since the you're using a number of
platform-specific macros. A good group to post to would be
microsoft.public.dotnet.languages.vc.

However, I think you will find that you can make the function a static
member without error.

Jonathan

Jul 22 '05 #3
"Julie" <ju***@aol.com> wrote in message
news:40***************@aol.com...
On the contrary, this is not at all off topic.

The basic question is how to associate an instance of a class through c-style callbacks.
I think the OP's problem was that he or she wanted to give the
callback function access priviledges by making it a member, but was
getting errors because win32 callback functions can't be a non-static
members. That's why I said it's off topic.

Your example involved the code

HRESULT WINAPI DirectPlayMessageHandler
( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
{
// ...
pointer_to_your_class_instance->method1();
// ..
}

What if method1 is private? That's the problem the OP was trying to
solve.
Get it?


I think so; do you?

Jonathan
Jul 22 '05 #4

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c1*************@ID-216073.news.uni-berlin.de...
"Julie" <ju***@aol.com> wrote in message
news:40***************@aol.com...
On the contrary, this is not at all off topic.

The basic question is how to associate an instance of a class through c-style
callbacks.


I think the OP's problem was that he or she wanted to give the
callback function access priviledges by making it a member, but was
getting errors because win32 callback functions can't be a non-static
members. That's why I said it's off topic.


I don't think access privileges have anything to do with it. The problem is
the general one of how to get a callback function to belong to an instance
of a class when its type is that of an ordinary external or static function.
This cannot be done directly by making the function a non-static member,
since the function won't be called in the context of a class instance.
Your example involved the code

HRESULT WINAPI DirectPlayMessageHandler
( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
{
// ...
pointer_to_your_class_instance->method1();
// ..
}

What if method1 is private? That's the problem the OP was trying to
solve.


I don't think so. Obviously, if method1 is private you would not call it.
You'd call another function that is public, or you'd make method1 public so
you can call it.
Get it?


I think so; do you?


No. Before worrying about access rights, you have to get at the class
instance from the external function in the first place. I believe the OP
only wants to know how to somehow get into a non-static member function when
the callback is called.

DW

Jul 22 '05 #5

"David White" <no@email.provided> wrote in message
news:Er******************@nasal.pacific.net.au...

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c1*************@ID-216073.news.uni-berlin.de...
"Julie" <ju***@aol.com> wrote in message
news:40***************@aol.com...
On the contrary, this is not at all off topic.

The basic question is how to associate an instance of a class through c-style
callbacks.


I think the OP's problem was that he or she wanted to give the
callback function access priviledges by making it a member, but was getting errors because win32 callback functions can't be a non-static members. That's why I said it's off topic.


I don't think access privileges have anything to do with it. The

problem is the general one of how to get a callback function to belong to an instance of a class when its type is that of an ordinary external or static function. This cannot be done directly by making the function a non-static member, since the function won't be called in the context of a class

instance.

Okay, both of these are well known problems with familiar solutions.
It's just a matter of figuring out what the OP meant.

What struck me about the original message is that the OP wanted the
callback function to access 'class variables'. That's why I focused on
access privileges. On the other hand, the OP may have been trying to
solve the more basic problem 'how do I get ahold of a class instance'?

I think all of us who have responded to the thread know how to solve
both problems. Only the OP knows what was originally intended.

Jonathan
Jul 22 '05 #6

"MacFly" <ma*******@wp.pl> wrote in message
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.


Bad news, it can't be a class member method, it must be a free function.

Good news, but see that pvUserContent parameter? It could be a pointer to
your object. Assuming your class is called MyClass then

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)
{
return ((MyClass*)pvUserContent)->DirectPlayMessageHandler(dwMessageId,
pMsgBuffer);
}

Now all you have to do is set things up so that pvUserContext points to the
object you are interested in, but that is a function of the WIN32 API so off
topic here.

This is covered in the FAQ (not very well tho' IMHO)

http://www.parashift.com/c++-faq-lit...o-members.html

john
Jul 22 '05 #7
Thanks for all replies and I'm sorry I didn't explain my problem more
precisely.

I just wanted to have access to class variables in DirectPlayMessageHandler
method, so I wanted to be class member function. Now I know it has to be
static.

But I'm still confused about the procedure of associating an instance of a
class described by Julie. what is the difference between
"your_class_instance" and "your_class_type" ??

How should it look like in my case:

class CConnection
{
.....
};

The function I use to initialize:
hr = m_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) )

so what should be put in the place of NULL ?

Regards.

Użytkownik "MacFly" <ma*******@wp.pl> napisał w wiadomości
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.
Thanks in advance for help.
Regards.

Jul 22 '05 #8
To John Harrison:
I guess we posted our messages at the same time :) So I read yours after I'd
posted mine.

Użytkownik "John Harrison" <jo*************@hotmail.com> napisał w
wiadomości news:c1*************@ID-196037.news.uni-berlin.de...

"MacFly" <ma*******@wp.pl> wrote in message
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.

Bad news, it can't be a class member method, it must be a free function.

Good news, but see that pvUserContent parameter? It could be a pointer to
your object. Assuming your class is called MyClass then

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)
{
return

((MyClass*)pvUserContent)->DirectPlayMessageHandler(dwMessageId, pMsgBuffer);
}

Now all you have to do is set things up so that pvUserContext points to the object you are interested in, but that is a function of the WIN32 API so off topic here.

This is covered in the FAQ (not very well tho' IMHO)

http://www.parashift.com/c++-faq-lit...o-members.html

john

Jul 22 '05 #9
your_class_type is a placeholder for the class type, in this case CConnection.

your_class_instance is a placeholder for an instance of CConnection. I don't
know what you called it in your code.

Here is an example:

class CConnection
{
private:
HRESULT DirectPlayMessageHandler(DWORD dwMessageId, PVOID pMsgBuffer);

public:
static HRESULT WINAPI Thunk_DirectPlayMessageHandler(PVOID pvUserContext,
DWORD dwMessageId, PVOID pMsgBuffer)
{
return ((CConnection *)pvUserContext)->DirectPlayMessageHandler(dwMessageId,
pMsgBuffer);
}
// etc.
};

// in your code somewhere
CConnection * connection = new CConnection();

hr = m_pDP->Initialize((void *)&connection,
CConnection::Thunk_DirectPlayMessageHandler, 0 );
//etc.

Here's how it works:

Add to your class a static thunk method with the same signature as the DPMH but
that is static. All that this method does is simply take ther user context,
cast it to the type of your class, and then call a non-static method on that
class.

When you initialize, you pass in the address of the instance of your class that
you want to act on, connection in this case, and the address of the thunk
function.

Now, when the code is executed, the internal workings calls the thunk function
w/ the instance of your class, which is cast and makes the method call on the
full member DirectPlayMessageHandler, which has full access to all class
methods/data.

Dig?

MacFly wrote:

Thanks for all replies and I'm sorry I didn't explain my problem more
precisely.

I just wanted to have access to class variables in DirectPlayMessageHandler
method, so I wanted to be class member function. Now I know it has to be
static.

But I'm still confused about the procedure of associating an instance of a
class described by Julie. what is the difference between
"your_class_instance" and "your_class_type" ??

How should it look like in my case:

class CConnection
{
....
};

The function I use to initialize:
hr = m_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) )

so what should be put in the place of NULL ?

Regards.

Użytkownik "MacFly" <ma*******@wp.pl> napisał w wiadomości
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.
Thanks in advance for help.
Regards.

Jul 22 '05 #10
I think I understand but there is still one thing which I have to handle
with.
When you initialize, you pass in the address of the instance of your class that you want to act on, connection in this case, and the address of the thunk
function.
I need this function to initialize object:
IDirectPlay8Peer* m_pDP;
which is other class member. What is more I want initializing that obejct in
other class method:

HRESULT CConnection::InitializeDirectPlay()
{
....
m_pDP->Initialize((NULL, DirectPlayMessageHandler, 0 ) )
....
}

So how can I pass in the address of the instance class ?

Uzytkownik "Julie" <ju***@aol.com> napisal w wiadomosci
news:40***************@aol.com... your_class_type is a placeholder for the class type, in this case CConnection.
your_class_instance is a placeholder for an instance of CConnection. I don't know what you called it in your code.

Here is an example:

class CConnection
{
private:
HRESULT DirectPlayMessageHandler(DWORD dwMessageId, PVOID pMsgBuffer);

public:
static HRESULT WINAPI Thunk_DirectPlayMessageHandler(PVOID pvUserContext,
DWORD dwMessageId, PVOID pMsgBuffer)
{
return ((CConnection *)pvUserContext)->DirectPlayMessageHandler(dwMessageId, pMsgBuffer);
}
// etc.
};

// in your code somewhere
CConnection * connection = new CConnection();

hr = m_pDP->Initialize((void *)&connection,
CConnection::Thunk_DirectPlayMessageHandler, 0 );
//etc.

Here's how it works:

Add to your class a static thunk method with the same signature as the DPMH but that is static. All that this method does is simply take ther user context, cast it to the type of your class, and then call a non-static method on that class.

When you initialize, you pass in the address of the instance of your class that you want to act on, connection in this case, and the address of the thunk
function.

Now, when the code is executed, the internal workings calls the thunk function w/ the instance of your class, which is cast and makes the method call on the full member DirectPlayMessageHandler, which has full access to all class
methods/data.

Dig?

MacFly wrote:

Thanks for all replies and I'm sorry I didn't explain my problem more
precisely.

I just wanted to have access to class variables in DirectPlayMessageHandler method, so I wanted to be class member function. Now I know it has to be
static.

But I'm still confused about the procedure of associating an instance of a class described by Julie. what is the difference between
"your_class_instance" and "your_class_type" ??

How should it look like in my case:

class CConnection
{
....
};

The function I use to initialize:
hr = m_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) )

so what should be put in the place of NULL ?

Regards.

Użytkownik "MacFly" <ma*******@wp.pl> napisał w wiadomości
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to class variables, but don't know how to do it ?
I still receive errors when I try to use it.
Thanks in advance for help.
Regards.

Jul 22 '05 #11
Ok, I've solved :) Thank you all for help!

Użytkownik "MacFly" <ma*******@wp.pl> napisał w wiadomości
news:c1**********@nemesis.news.tpi.pl...
Hi everyone,

HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)

I want that method to be class member method so it could have access to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.
Thanks in advance for help.
Regards.

Jul 22 '05 #12
> I need this function to initialize object:
IDirectPlay8Peer* m_pDP;
which is other class member. What is more I want initializing that obejct in
other class method:
HRESULT CConnection::InitializeDirectPlay()
{
....
m_pDP->Initialize(this, Thunk_DirectPlayMessageHandler, 0);
....
}
MacFly wrote:
I think I understand but there is still one thing which I have to handle
with.
When you initialize, you pass in the address of the instance of your class

that
you want to act on, connection in this case, and the address of the thunk
function.


I need this function to initialize object:
IDirectPlay8Peer* m_pDP;
which is other class member. What is more I want initializing that obejct in
other class method:

HRESULT CConnection::InitializeDirectPlay()
{
....
m_pDP->Initialize((NULL, DirectPlayMessageHandler, 0 ) )
....
}

So how can I pass in the address of the instance class ?

Uzytkownik "Julie" <ju***@aol.com> napisal w wiadomosci
news:40***************@aol.com...
your_class_type is a placeholder for the class type, in this case

CConnection.

your_class_instance is a placeholder for an instance of CConnection. I

don't
know what you called it in your code.

Here is an example:

class CConnection
{
private:
HRESULT DirectPlayMessageHandler(DWORD dwMessageId, PVOID pMsgBuffer);

public:
static HRESULT WINAPI Thunk_DirectPlayMessageHandler(PVOID pvUserContext,
DWORD dwMessageId, PVOID pMsgBuffer)
{
return ((CConnection

*)pvUserContext)->DirectPlayMessageHandler(dwMessageId,
pMsgBuffer);
}
// etc.
};

// in your code somewhere
CConnection * connection = new CConnection();

hr = m_pDP->Initialize((void *)&connection,
CConnection::Thunk_DirectPlayMessageHandler, 0 );
//etc.

Here's how it works:

Add to your class a static thunk method with the same signature as the

DPMH but
that is static. All that this method does is simply take ther user

context,
cast it to the type of your class, and then call a non-static method on

that
class.

When you initialize, you pass in the address of the instance of your class

that
you want to act on, connection in this case, and the address of the thunk
function.

Now, when the code is executed, the internal workings calls the thunk

function
w/ the instance of your class, which is cast and makes the method call on

the
full member DirectPlayMessageHandler, which has full access to all class
methods/data.

Dig?

MacFly wrote:

Thanks for all replies and I'm sorry I didn't explain my problem more
precisely.

I just wanted to have access to class variables in DirectPlayMessageHandler method, so I wanted to be class member function. Now I know it has to be
static.

But I'm still confused about the procedure of associating an instance of a class described by Julie. what is the difference between
"your_class_instance" and "your_class_type" ??

How should it look like in my case:

class CConnection
{
....
};

The function I use to initialize:
hr = m_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) )

so what should be put in the place of NULL ?

Regards.

Użytkownik "MacFly" <ma*******@wp.pl> napisał w wiadomości
news:c1**********@nemesis.news.tpi.pl...
> Hi everyone,
>
> HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
> dwMessageId, PVOID pMsgBuffer)
>
> I want that method to be class member method so it could have access to > class variables, but don't know how to do it ?
> I still receive errors when I try to use it.
>
>
> Thanks in advance for help.
> Regards.
>
>
>
>

Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
1
by: Jing You | last post by:
hi every one, I have got some confused problem when I try to write some custom object by javascript. Look at the example code here: <BODY> <script language="jscript">
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
5
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Frans Bouma | last post by:
Hi, I have a serious problem with VB.NET and a DataTable derived class and I can't figure out how to solve it. I have implemented it in C# where it works perfectly, but I can't port one...
5
by: Ben | last post by:
Hi, i defined a function in the base class 'ford' and the same function (with different output) in subclass "peugeot". I first put 'Overridable function' in the base class and 'Overrides...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.