Raised This Month: $51 Target: $400
 12% 

SourceHook noob question - simple example doesn't work as expect


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
binkm
New Member
Join Date: Jan 2009
Old 01-08-2009 , 05:25   SourceHook noob question - simple example doesn't work as expect
Reply With Quote #1

Greetings!
New to SourceHook so decided to start from scratch - make a simple test hooking virtual function following the description at http://wiki.alliedmods.net/SourceHook_Development

Expecting the output:
unhooked testfn
unhooked testfn
mytestfn
mytestfn
unhooked testfn
unhooked testfn
and get
unhooked testfn
unhooked testfn
unhooked testfn
unhooked testfn
unhooked testfn
unhooked testfn
Please point what points i'am missing

Code:
#include <SourceHook.h>
#include <sourcehook_impl.h>

using namespace SourceHook;

SourceHook::Impl::CSourceHookImpl g_SourceHook;
SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
int g_PLID = 0;

class Test
{
public:
    void test(void)
    {
        testfn();
    }
    virtual void testfn(void)
    {
        printf("unhooked testfn\n");
    }
};

SH_DECL_HOOK0_void(Test,testfn,SH_NOATTRIB,0);
SH_DECL_MANUALHOOK0_void(1,0,0,0);
Test ts1;

void mytestfn(void)
{
    printf("mytestfn\n");
    RETURN_META(MRES_SUPERCEDE);

}

int _tmain(int argc, _TCHAR* argv[])
{

    Test tst;
 
    ts1.testfn();
    tst.testfn();
    
    SH_ADD_MANUALHOOK(1, &ts1, SH_STATIC(mytestfn), false);
    SH_ADD_HOOK(Test,testfn,&tst,SH_STATIC(mytestfn),false);

    ts1.testfn();
    tst.testfn();

    SH_REMOVE_MANUALHOOK(1, &ts1, SH_STATIC(mytestfn), false);
    SH_REMOVE_HOOK(Test,testfn,&tst,SH_STATIC(mytestfn),false);

    ts1.testfn();
    tst.testfn();
    

    return 0;
}
binkm is offline
BAILOPAN
Join Date: Jan 2004
Old 01-08-2009 , 14:20   Re: SourceHook noob question - simple example doesn't work as expect
Reply With Quote #2

You aren't calling the function virtually. The compiler is smart enough to optimize your code to static calls.
__________________
egg
BAILOPAN is offline
binkm
New Member
Join Date: Jan 2009
Old 01-08-2009 , 14:23   Re: SourceHook noob question - simple example doesn't work as expect
Reply With Quote #3

Yes, thanks! I've just came to it myself digging into debugger
added test function and everything is ok

Code:
class Test
{
public:
    void test(void)
    {
        testfn();
    }
    virtual void testfn(void);
    
};
binkm is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 04:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode