obtain class member function address? 
Author Message
 obtain class member function address?

Hi,

Does anybody know how can I get class member function address with VC++ 5.0.

Code sample:

    class MyClass
    {
       ...
    private:
       ...
       DWORD WINAPI MyFunction (LPVOID);
       ...
    }

I need to get address of MyFunction. Is it posible in VC?



Mon, 25 Jun 2001 03:00:00 GMT  
 obtain class member function address?

Quote:
> Hi,

> Does anybody know how can I get class member function address with VC++
5.0.

> Code sample:

>     class MyClass
>     {
>        ...
>     private:
>        ...
>        DWORD WINAPI MyFunction (LPVOID);
>        ...
>     }

> I need to get address of MyFunction. Is it posible in VC?

You would get it the same way you would for any other, pretty much. For
example, if cl is a MyClass instance, then &cl.MyFunction, would get you the
address. However, it seems that you are writing MyFunc to be some sort of
CALLBACK functions, and for that, you'll have to make it static. NOrmal
member functions have a hidden parameter in the stack (the this pointer),
and thus cannot be usually used for callbacks.

--
Tomas Restrepo

http://members.xoom.com/trestrep/



Mon, 25 Jun 2001 03:00:00 GMT  
 obtain class member function address?
Dmitri,

Try this...

This will give you the address in p; however, I can't call the function!

DWORD (WINAPI MyClass::*p)(void) = MyClass::MyFunction;

Are you passing the address as the thread procedure to a new thread? This is
the only way we can think of using the adress once we have it.

Dave Smith                              Chris Smith
MCSE, MCT, MVP

Quote:

>Hi,

>Does anybody know how can I get class member function address with VC++
5.0.

>Code sample:

>    class MyClass
>    {
>       ...
>    private:
>       ...
>       DWORD WINAPI MyFunction (LPVOID);
>       ...
>    }

>I need to get address of MyFunction. Is it posible in VC?



Mon, 25 Jun 2001 03:00:00 GMT  
 obtain class member function address?
    Since the member function you are seeking the address of is private, you
will not be able to get it without supplying some means within the class to
get its pointer.  This would require a public member that would return the
pointer to you.  I'm not sure of your particular application, but it would
seem that allowing one to retrieve the pointer to a private member somewhat
defeats the purpose of making it private.

    Now, I would not encourage doing the following, but below is a console
app code snippet which shows how you can do what you are trying to
accomplish:

----------------------- cut here -----------------

#include <iostream>
#include <windows.h>

class CTest;

typedef DWORD (WINAPI CTest::*PPFUNC)(LPVOID);

class CTest {
public:
         PPFUNC GetMyFuncPtr();
private:
         DWORD WINAPI MyFunction(LPVOID);

Quote:
};

PPFUNC
CTest::GetMyFuncPtr()
{
         return(MyFunction);

Quote:
}

DWORD
CTest::MyFunction(
LPVOID)
{
         return(42);

Quote:
}

void main()
{
    CTest        test;
    PPFUNC    pFunc=test.GetMyFuncPtr();

    std::cout << (test.*pFunc)(&test) << std::endl;

Quote:
}

----------------------- cut here -----------------

Joe

-------------------------------


Quote:
>Hi,

>Does anybody know how can I get class member function address with VC++
5.0.

>Code sample:

>    class MyClass
>    {
>       ...
>    private:
>       ...
>       DWORD WINAPI MyFunction (LPVOID);
>       ...
>    }

>I need to get address of MyFunction. Is it posible in VC?



Mon, 25 Jun 2001 03:00:00 GMT  
 obtain class member function address?
NOTE: The following reply was mailed directly to me. Please do not do this.
We need to keep the thread here in the public forum so all can benefit.

Hi, Dave!

Quote:
-----Original Message-----

Newsgroups: microsoft.public.vc.language
Date: January 7, 1999 9:48 AM
Subject: Re: obtain class member function address?

>Dmitri,

>Try this...

>This will give you the address in p; however, I can't call the function!

>DWORD (WINAPI MyClass::*p)(void) = MyClass::MyFunction;

It's great, just great!!!  it realy works!!!

>Are you passing the address as the thread procedure to a new thread? This
is
>the only way we can think of using the adress once we have it.

Yes, I want to pass this address to the new thread, but I have problems when
I'm trying to pass this pointer to CreateThread() function because of type
conversion. Do you have any expirience in this? How can I pass this pointer
correctly? Will it be working at all?

>Dave Smith                              Chris Smith
>MCSE, MCT, MVP

Bye,
Dmitri.


>>Hi,

>>Does anybody know how can I get class member function address with VC++
>5.0.

>>Code sample:

>>    class MyClass
>>    {
>>       ...
>>    private:
>>       ...
>>       DWORD WINAPI MyFunction (LPVOID);
>>       ...
>>    }

>>I need to get address of MyFunction. Is it posible in VC?



Mon, 25 Jun 2001 03:00:00 GMT  
 obtain class member function address?
Dmitri,

To do this you should really declare the method as static. If the method
needs access to the other members of the class (i.e., the non-static
members), then send the address of a class instance as the thread parameter.
The code works, but does not give access to the other class members.

/***************************************************************************
****/
#include <windows.h>
#include <iostream.h>

class MyClass
{
public:
 static DWORD WINAPI func(void*) {cout << "Succeeded..." << endl;
          return 0;}

Quote:
};

int main(int argc, char * argv[])
{
 DWORD (WINAPI *p)(void*) = MyClass::func;

 DWORD tid;
 HANDLE hThread = CreateThread(NULL, 0, p, NULL, 0, &tid);
 WaitForSingleObject(hThread, INFINITE);
 return 0;

Quote:
}

Dave Smith
MCSE, MCT, MVP


Quote:
>Dmitri,

>Try this...

>This will give you the address in p; however, I can't call the function!

>DWORD (WINAPI MyClass::*p)(void) = MyClass::MyFunction;

>Are you passing the address as the thread procedure to a new thread? This
is
>the only way we can think of using the adress once we have it.

>Dave Smith                              Chris Smith
>MCSE, MCT, MVP


>>Hi,

>>Does anybody know how can I get class member function address with VC++
>5.0.

>>Code sample:

>>    class MyClass
>>    {
>>       ...
>>    private:
>>       ...
>>       DWORD WINAPI MyFunction (LPVOID);
>>       ...
>>    }

>>I need to get address of MyFunction. Is it posible in VC?



Mon, 25 Jun 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Addressing to members of surrounding class from nested class

2. Passing the address of a member function to a C function

3. Function to obtain local IP Address?

4. Obtaining a PCI address vs a virtual address?

5. Obtaining physical memory address from virtual memory address?

6. CDocument class member addresses

7. Passing C++ Class Member Function to as a C Callback Function Parameter

8. class member function as callback function

9. Using class member function as a thread function

10. How to use member function of a C++ class as a callback function

11. Using class member function as a thread function

12. HELP WANTED About Member Function Addressing!

 

 
Powered by phpBB® Forum Software