|
|
发表于 2007-2-28 09:53:00
|
显示全部楼层
Re:修正的第三版 非静态类成员函数指针 解决方案
我以前用的是下面的这种方法
namespace xlibplus
{
template < class TRet, class T >
inline TRet ThisCall0( T func, LPVOID pThis )
{
TRet lpRet;
__asm mov ecx, pThis;
//__asm push eax
__asm call dword ptr[func];
__asm mov lpRet, eax
return lpRet;
}
template < class TRet, class T, class Tp1 >
inline TRet ThisCall1( T func, LPVOID pThis, Tp1 lpParam1 )
{
TRet lpRet;
__asm push dword ptr[lpParam1]
__asm mov ecx, pThis;
//__asm push eax
__asm call dword ptr[func];
__asm mov lpRet, eax
return lpRet;
}
template < class TRet, class T, class Tp1, class Tp2 >
inline TRet ThisCall2( T func, LPVOID pThis, Tp1 lpParam1, Tp2 lpParam2 )
{
TRet lpRet;
__asm push dword ptr[lpParam2]
__asm push dword ptr[lpParam1]
__asm mov ecx, pThis;
//__asm push eax
__asm call dword ptr[func];
__asm mov lpRet, eax
return lpRet;
}
template < class TRet, class T, class Tp1, class Tp2, class Tp3 >
inline TRet ThisCall3( T func, LPVOID pThis, Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3 )
{
TRet lpRet;
__asm push dword ptr[lpParam3]
__asm push dword ptr[lpParam2]
__asm push dword ptr[lpParam1]
__asm mov ecx, pThis;
//__asm push eax
__asm call dword ptr[func];
//__asm push eax
__asm mov lpRet, eax
return lpRet;
}
template < class TRet, class T, class Tp1, class Tp2, class Tp3, class Tp4 >
inline TRet ThisCall4( T func, LPVOID pThis, Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3, Tp4 lpParam4 )
{
TRet lpRet;
__asm push dword ptr[lpParam4]
__asm push dword ptr[lpParam3]
__asm push dword ptr[lpParam2]
__asm push dword ptr[lpParam1]
__asm mov ecx, pThis;
//__asm push eax
__asm call dword ptr[func];
__asm mov lpRet, eax
return lpRet;
}
template < class TRet, class T, class Tp1, class Tp2, class Tp3, class Tp4, class Tp5 >
inline TRet ThisCall5( T func, LPVOID pThis, Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3, Tp4 lpParam4, Tp5 lpParam5 )
{
TRet lpRet;
__asm push dword ptr[lpParam5]
__asm push dword ptr[lpParam4]
__asm push dword ptr[lpParam3]
__asm push dword ptr[lpParam2]
__asm push dword ptr[lpParam1]
__asm mov ecx, pThis;
//__asm push eax
__asm call dword ptr[func];
__asm mov lpRet, eax
return lpRet;
}
template < class T >
inline LPVOID F2P( T func )
{
__asm mov eax, dword ptr[func];
}
template< class TRet = LPVOID >
class xThisCallCustom
{
typedef xThisCallCustom<TRet> xThisClass;
public:
xThisCallCustom()
{
m_lpThis = NULL;
m_lpFunc = NULL;
}
xThisCallCustom(const xThisClass & thiscall )
{
m_lpThis = thiscall.m_lpThis;
m_lpFunc = thiscall.m_lpFunc;
}
template <class T>
xThisCallCustom(LPVOID lpThis, T func)
{
m_lpThis = lpThis;
m_lpFunc = F2P(func);
}
LPVOID getThisPtr()const{ return m_lpThis;}
VOID setThisPtr( LPVOID lpThis ){ m_lpThis = lpThis;}
LPVOID getFuncPtr()const{ return m_lpFunc;}
template <class T> VOID setFuncPtr( T func ){ m_lpFunc = F2P( func );}
TRet call()
{
if( !Assigned() )return NULL;
return ThisCall0<TRet>( m_lpFunc, m_lpThis );
}
template < class Tp1 >
TRet call( Tp1 lpParam1 )
{
if( !Assigned() )return NULL;
return ThisCall1<TRet>( m_lpFunc, m_lpThis, lpParam1 );
}
template < class Tp1, class Tp2 >
TRet call( Tp1 lpParam1, Tp2 lpParam2 )
{
if( !Assigned() )return NULL;
return ThisCall2<TRet>( m_lpFunc, m_lpThis, lpParam1, lpParam2 );
}
template < class Tp1, class Tp2, class Tp3 >
TRet call( Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3 )
{
if( !Assigned() )return NULL;
//LPVOID lpRet = NULL;
return ThisCall3<TRet>( m_lpFunc, m_lpThis, lpParam1, lpParam2, lpParam3 );
//__asm pop lpRet;
//return lpRet;
}
template < class Tp1, class Tp2, class Tp3, class Tp4 >
TRet call( Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3, Tp4 lpParam4 )
{
if( !Assigned() )return NULL;
return ThisCall4<TRet>( m_lpFunc, m_lpThis, lpParam1, lpParam2, lpParam3, lpParam4 );
}
template < class Tp1, class Tp2, class Tp3, class Tp4, class Tp5 >
TRet call( Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3, Tp4 lpParam4, Tp5 lpParam5 )
{
if( !Assigned() )return NULL;
return ThisCall5<TRet>( m_lpFunc, m_lpThis, lpParam1, lpParam2, lpParam3, lpParam4, lpParam5 );
}
template < class TTHIS >
TRet callWithThis(TTHIS * pThis)
{
if( !Assigned() )return NULL;
return ThisCall0<TRet>( m_lpFunc, pThis );
}
template < class TTHIS, class Tp1 >
TRet callWithThis( TTHIS * pThis, Tp1 lpParam1 )
{
if( !Assigned() )return NULL;
return ThisCall1<TRet>( m_lpFunc, pThis, lpParam1 );
}
template < class TTHIS, class Tp1, class Tp2 >
TRet callWithThis( TTHIS * pThis, Tp1 lpParam1, Tp2 lpParam2 )
{
if( !Assigned() )return NULL;
return ThisCall2<TRet>( m_lpFunc, pThis, lpParam1, lpParam2 );
}
template < class TTHIS, class Tp1, class Tp2, class Tp3 >
TRet callWithThis( TTHIS * pThis, Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3 )
{
if( !Assigned() )return NULL;
return ThisCall3<TRet>( m_lpFunc, pThis, lpParam1, lpParam2, lpParam3 );
}
template < class TTHIS, class Tp1, class Tp2, class Tp3, class Tp4 >
TRet callWithThis( TTHIS * pThis, Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3, Tp4 lpParam4 )
{
if( !Assigned() )return NULL;
return ThisCall4<TRet>( m_lpFunc, pThis, lpParam1, lpParam2, lpParam3, lpParam4 );
}
template < class TTHIS, class Tp1, class Tp2, class Tp3, class Tp4, class Tp5 >
TRet callWithThis( TTHIS * pThis, Tp1 lpParam1, Tp2 lpParam2, Tp3 lpParam3, Tp4 lpParam4, Tp5 lpParam5 )
{
if( !Assigned() )return NULL;
return ThisCall5<TRet>( m_lpFunc, pThis, lpParam1, lpParam2, lpParam3, lpParam4, lpParam5 );
}
xThisClass & operator =( const xThisClass & thiscall )
{
this->m_lpFunc = thiscall.m_lpFunc;
this->m_lpThis = thiscall.m_lpThis;
return *this;
}
BOOL Assigned(){ return (m_lpFunc != NULL);}
protected:
LPVOID m_lpThis;
LPVOID m_lpFunc;
};
typedef xThisCallCustom<> xThisCall;
typedef xThisCallCustom<> xEventCall;
typedef xThisCallCustom<int> xCompareCall;
};
|
|