473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cast pb

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,lpSecurit yAttributes,dwCreationDisposition,dwFlagsAndAttrib utes,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 ?




Jul 25 '06 #1
2 2132
Vincent RICHOMME schrieb:
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.

--
Thomas
Jul 25 '06 #2
Vincent RICHOMME wrote:
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-lit....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,lpSecurit yAttributes,dwCreationDisposition,dwFlagsAndAttrib utes,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/ho...t.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

Jul 25 '06 #3

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

Similar topics

0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
3
by: Mike | last post by:
I am using MS-Access as a front end for my MS-SQL DB. I have a sql view that uses the following: SELECT TOP 100 PERCENT RECID, PATNUMBER AS , SVCCODE AS , QTY, PROF_CHRGS AS , AMOUNT,...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
6
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.