Cast pb

V

Vincent RICHOMME

Hi,

I am trying to convert a C project into C++. This project is used to
hook some system calls on a Windows CE platform.


Here is the class definition:

class CHookBase
{
public:

CHookBase(DWORD dwSysCallAddr);
~CHookBase(void);

virtual void SetHook() = 0;
virtual void InitHook();



private:
const DWORD FIRST_METHOD;
const DWORD CURRENT_METHOD;
const int APICALL_SCALE;
const int HANDLE_SHIFT;
const DWORD METHOD_MASK;
const DWORD HANDLE_MASK;


BOOL m_bMode;
DWORD m_dwPerm;
CINFO** m_SystemAPISets;

DWORD m_Tmp;
DWORD m_ApiSet;
DWORD m_Method;
PFNVOID m_pfnOrgFunc;
};




#include "hookbase.h"
class CCreateFileHook : public CHookBase
{
public:

//----------------------- SIGNATURE OF FUNCTIONS TO
HOOK--------------------//
typedef HANDLE t_CreateFile
(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);

CCreateFileHook(DWORD dwSysCallAddr);
~CCreateFileHook(void);



static HANDLE _CreateFileHook( LPCTSTR lpFileName, DWORD
dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);


virtual void SetHook();

};

and here is the implementation:



/*static*/
HANDLE CCreateFileHook::_CreateFileHook( LPCTSTR lpFileName, DWORD
dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES
lpSecurityAttributes, DWORD dwCreationDisposition, DWORD
dwFlagsAndAttributes, HANDLE hTemplateFile)
{
HANDLE H = ((t_CreateFile*) m_pfnOrgFunc
)(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);
DWORD Err = GetLastError();

SetLastError(Err);
return H;
}

However when I compile I got the following message :
CCreateFileHook.cpp(21) : error C2440: 'type cast' : cannot convert from
'' to 't_CreateFile (__cdecl *)'
The expression being converted is not valid


In C this cast is allowed .........

For info :

HANDLE is a void*
PFNVOID is defined as a pointer to function returning void and taking 0
parameter (=typedef void(PFNVOID*)(void)

How can I fix that ?
 
T

Thomas J. Gritzan

Vincent said:
Hi,

I am trying to convert a C project into C++. This project is used to
hook some system calls on a Windows CE platform.
[snip much code]

What about a minimal example?
However when I compile I got the following message :
CCreateFileHook.cpp(21) : error C2440: 'type cast' : cannot convert from
'' to 't_CreateFile (__cdecl *)'
The expression being converted is not valid


In C this cast is allowed .........

For info :

HANDLE is a void*
PFNVOID is defined as a pointer to function returning void and taking 0
parameter (=typedef void(PFNVOID*)(void)

How can I fix that ?

- Show us what line 21 is.
- Try to avoid the cast at all.
- Try
typedef void (*PFNVOID)(void);
or
typedef void (*PFNVOID)();
instead.

static HANDLE _CreateFileHook( /* ... */ );

Don't use leading underscore for identifiers, they are reserved by the
implementation.
 
M

mlimber

Vincent said:
Hi,

I am trying to convert a C project into C++. This project is used to
hook some system calls on a Windows CE platform.


Here is the class definition:

class CHookBase
{
public:

CHookBase(DWORD dwSysCallAddr);
~CHookBase(void);

This should probably be virtual. See also
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4.
virtual void SetHook() = 0;
virtual void InitHook();



private:
const DWORD FIRST_METHOD;
const DWORD CURRENT_METHOD;
const int APICALL_SCALE;
const int HANDLE_SHIFT;
const DWORD METHOD_MASK;
const DWORD HANDLE_MASK;


BOOL m_bMode;
DWORD m_dwPerm;
CINFO** m_SystemAPISets;

DWORD m_Tmp;
DWORD m_ApiSet;
DWORD m_Method;
PFNVOID m_pfnOrgFunc;
};




#include "hookbase.h"
class CCreateFileHook : public CHookBase
{
public:

//----------------------- SIGNATURE OF FUNCTIONS TO
HOOK--------------------//
typedef HANDLE t_CreateFile
(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);

CCreateFileHook(DWORD dwSysCallAddr);
~CCreateFileHook(void);



static HANDLE _CreateFileHook( LPCTSTR lpFileName, DWORD

All identifiers beginning with an underscore and a capital letter are
reserved for the implementation. Better change this.
dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);


virtual void SetHook();

};

and here is the implementation:



/*static*/
HANDLE CCreateFileHook::_CreateFileHook( LPCTSTR lpFileName, DWORD
dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES
lpSecurityAttributes, DWORD dwCreationDisposition, DWORD
dwFlagsAndAttributes, HANDLE hTemplateFile)
{
HANDLE H = ((t_CreateFile*) m_pfnOrgFunc

Where is m_pfnOrgFunc defined? What is its type?
)(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile);
DWORD Err = GetLastError();

SetLastError(Err);
return H;
}

However when I compile I got the following message :
CCreateFileHook.cpp(21) : error C2440: 'type cast' : cannot convert from
'' to 't_CreateFile (__cdecl *)'
The expression being converted is not valid

You'll note the empty quotes. Apparently the compiler doesn't know what
type it's converting from.
In C this cast is allowed .........

Maybe, but prefer C++-style casts since they're clearer and less
dangerous. Better still, avoid casting at all.
For info :

HANDLE is a void*
PFNVOID is defined as a pointer to function returning void and taking 0
parameter (=typedef void(PFNVOID*)(void)

How can I fix that ?

First of all, read the guidelines about posting problematic code:

http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Reduce this down to a simpler problem, and you'll either find your
answer or you can post it for a better response.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top